locked
Incorrect syntax near the keyword 'if' RRS feed

  • Question

  • I'm receiving an error in my syntax when trying to set a date value based on a condition.

    But I receive a error message that says: "Incorrect syntax near the keyword 'if'"

    I want to set FinAssetInfo.DT to either '20090630' or '20091231'

    where FinAssetInfo.DT = if (select COUNT(*) from #PanelInformation where RESP_ID_R = 3032570 and RPNL_R = 8091) '20090630' else '20091231'


    chuckdawit


    • Edited by witdaj Wednesday, March 7, 2012 12:57 AM
    Wednesday, March 7, 2012 12:56 AM

Answers

  • You can not use IF in this context. Try

    where FinAssetInfo.DT = case when exists(select 1 from #PanelInformation

    where RESP_ID_R = 3032570 and RPNL_R = 8091) then '20090630' else '20091231' end



    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    • Edited by Naomi N Wednesday, March 7, 2012 1:00 AM
    • Proposed as answer by Nighting Liu Wednesday, March 7, 2012 1:08 AM
    • Marked as answer by witdaj Wednesday, March 7, 2012 1:33 AM
    Wednesday, March 7, 2012 1:00 AM