My database table has the following columns: A1, A2, A3, A4, A5, A6
I have to update these records and shift the values left whenever I come across a null column. The purpose is not to have null values between the columns that have a value starting from left. For example if:
A1 = NULL , A2 = 1 , A3 = 4, A4 = 5, A5 = 9, A6 = 8 I have to shift the values left so the result will be:
A1 = 1, A2 = 4 , A3 = 5, A4 = 9, A5 = 8, A6 = NULL So far I have come up with the following query but it is slow. Let me know if you can tweak the query to make it faster. One more thing, What if I do this in c#? Will it be faster if I loop through the Datarows there and update each row?
UPDATE myTable SET A5 = A6, A6 = NULL WHERE (A5 IS NULL) AND (NOT A6 IS NULL) UPDATE myTable SET A4 = A5, A5 = A6 WHERE (A4 IS NULL) AND (NOT A5 IS NULL) UPDATE myTable SET A3 = A4, A4 = A5, A5 = A6 WHERE (A3 IS NULL) AND (NOT A4 IS NULL) UPDATE myTable SET A2 = A3, A3 = A4, A4 = A5, A5 = A6 WHERE (A2 IS NULL) AND (NOT A3 IS NULL) UPDATE myTable SET A1 = A2, A2 = A3, A3 = A4, A4 = A5, A5 = A6 WHERE (A1 IS NULL) AND (NOT A2 IS NULL)