January 5, 2010 at 3:11 am
I am trying to do a simple select statement but am not sure if I can incorporate if-else clause within a select sql
Select col1,col2, <<if isnull(col3) then 'def value' else col3>> from tbl1
I want to do things like if col3 is null then insert a default value else report value of col3
Since this is for reporting, I do not have to modify the database to replace null values with defaults.
January 5, 2010 at 3:18 am
Please see the CASE statement in BOL (Books on line)
January 5, 2010 at 3:18 am
Have you tried the 'Case Statements'.
January 5, 2010 at 3:19 am
Any of these
Select col1,col2, COALESCE(col3,'def value') as col3 from tbl1
Select col1,col2, ISNULL(col3,'def value') as col3 from tbl1
Select col1,col2, CASE WHEN col3 IS NULL THEN 'def value' ELSE col3 END as col3 from tbl1
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537January 5, 2010 at 3:48 am
Thank you-I will try the Case statement.
Regards
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply