CipherIS
asked on
SQL IF STATEMENT
I'm trying to use an if statement in my SELECT clause and I am having issues. Here is an example:
Select field1, field2,
IF field3 is NULL
BEGIN
'TBD'
END
ELSE
BEGIN
field3
END AS myField
From myTable
Above will not execute. What is the fix?
Also field3 is a datefield and I want it to output as "Thu, 08/23/12".
Thanks
Select field1, field2,
IF field3 is NULL
BEGIN
'TBD'
END
ELSE
BEGIN
field3
END AS myField
From myTable
Above will not execute. What is the fix?
Also field3 is a datefield and I want it to output as "Thu, 08/23/12".
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@ged325
I used
select field1, field2, case when field3 is null then 'TBD' else field3 as MYfield, field4 from mytable Where blah blah blah
And I receive the below Error.
Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'AS'.
I used
select field1, field2, case when field3 is null then 'TBD' else field3 as MYfield, field4 from mytable Where blah blah blah
And I receive the below Error.
Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'AS'.
did you try mine ?
select field1, field2, case when field3 is null then 'TBD' else field3 end as MYfield, field4 from mytable Where blah blah blah
select field1, field2, case when field3 is null then 'TBD' else field3 end as MYfield, field4 from mytable Where blah blah blah
ASKER
@aneeshattingal - thanks - that helped me see i was missing the "END". Now I'm receiving this error.
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
field3 is a date field. Any idea how to handle?
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
field3 is a date field. Any idea how to handle?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thx
ASKER
Awesome
ISNULL( field3,'TBD') as MyField
From myTable