Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 CASE STATEMENT QUESTION

Author  Topic 

macsterling
Yak Posting Veteran

56 Posts

Posted - 2009-01-24 : 21:47:21

update table
set column = case
when another_column = '1' then 'A'
when another_column = '2' then 'B'
when another_column = '3' then 'C'
end

When another_column = something else, it puts null into column. I only want to change column when any of those three conditions are met

What do I put in the else statement to make it leave column alone

macsterling
Yak Posting Veteran

56 Posts

Posted - 2009-01-24 : 22:09:34
I figured it out - I had something commented out - Thanks anyway
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-25 : 04:45:50
not sure how you fixed it. but there are two ways
1.add else condition as else column
2. put a where condition where another_column in ('1','2','3')
Go to Top of Page

Kokkula
Starting Member

41 Posts

Posted - 2009-01-28 : 06:20:41

Try this

update table
set column = case
when another_column = '1' then 'A'
when another_column = '2' then 'B'
when another_column = '3' then 'C'
ELSE column
end

When another_column = something

--
Thanks,
Pavan
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-28 : 08:55:29
quote:
Originally posted by Kokkula


Try this

update table
set column = case
when another_column = '1' then 'A'
when another_column = '2' then 'B'
when another_column = '3' then 'C'
ELSE column
end

When another_column = something

--
Thanks,
Pavan


whats the purpose of last when?
Go to Top of Page

Kokkula
Starting Member

41 Posts

Posted - 2009-01-29 : 08:13:34
Thanks Visakh for pointing to it sgould be actually Where insted of when.

UPDATE table
SET column = CASE
WHEN another_column = '1' then 'A'
WHEN another_column = '2' then 'B'
WHEN another_column = '3' then 'C'
ELSE column
END
WHERE another_column = something <Where Condition>

--
Thanks,
Pavan
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-29 : 08:23:37
A WHERE clause will speed things up.
And you also with a WHERE don't need an ELSE part in the CASE statement.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -