February 8, 2012 at 12:31 pm
I have a table mytable that has columns like this:
A-ID, A-Name, B-ID, B-Name, C-ID, C-Name, D-ID, D-anme
I would do an update query to clean like this: for each ID, if it is null, then set A-Name to null.
I know I can do something like this:
Update mytable
set A-Name =null
where A-ID =Null.
Then
update mytable
set B-Name=Null
where B-ID=Null
....
But I need to write quite a few updates statement,
is there a way to combine them together to just use one update statement
Thanks
February 8, 2012 at 12:40 pm
Try this:
UPDATE mytable SET A-NAME = CASE WHEN A-ID IS NULL THEN NULL ELSE A-NAME end,
B-NAME = CASE WHEN B-ID IS NULL THEN NULL ELSE B-NAME end,
C-NAME = CASE WHEN C-ID IS NULL THEN NULL ELSE C-NAME end,
D-NAME = CASE WHEN D-ID IS NULL THEN NULL ELSE D-NAME end
February 8, 2012 at 12:42 pm
Something like this?
Update mytable
Set [A-Name]=CASE WHEN [A-ID] IS Null THEN null ELSE [A-Name] END,
[B-Name]=CASE WHEN [B-ID] IS Null THEN null ELSE [B-Name] END
...
As a side note: Ihope the column names are fake...
February 8, 2012 at 12:49 pm
Thanks, all,
will give a try. Yes, the table and columns name are fake. I just want to make it easier to demonstrate.
Thanks
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply