Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

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
SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Select field1, field2,
ISNULL( field3,'TBD') as MyField

From myTable
Avatar of CipherIS

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'.
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
@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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thx
Awesome