IIF Statment/CASE Statement

  • Hi All,

    I am converting some queries from Access to SQL, so that I can get some auto exports going... But the last peiece of the puzzle is missing...

    It is actually the first section of my select statement that is the issue:

    If(PhoneNumber) = ((ISNULL(LEFT(Q5, ISNULL(NULLIF(CHARINDEX('-', Q5, 0) -1, -1),LEN(Q5))),'')) & "%"),'','Check'))) As DialCodeCheck

    and the error I get is;

    Msg 156, Level 15, State 1, Line 1

    Incorrect syntax near the keyword 'If'.

    Msg 102, Level 15, State 1, Line 1

    Incorrect syntax near ','.

    Full code:

    SELECT If(PhoneNumber) = ((ISNULL(LEFT(Q5, ISNULL(NULLIF(CHARINDEX('-', Q5, 0) -1, -1),LEN(Q5))),'')) & "%"),'','Check'))) As DialCodeCheck, ('VOICE') As Agency, CONVERT(Varchar, CallDate, 103) As Date, LEFT(CallTime,5) As Time, (LoginID) As Agent, node_id, CAMPAIGN_CODE, ISNULL((Title),'') As BILLING_SALUTATION, ISNULL((FirstName),'') As BILLING_FIRST_NAME, ISNULL((LastName),'') As BILLING_LAST_NAME, (PhoneNumber) As BILLING_EVENING_PHONE_NUMBER,

    ISNULL((Add1),'') As Address_1, ISNULL((Add2),'') As Address_2, ('') As Address_3, ISNULL((Town),'') As Address_4, ISNULL((County),'') As Address_5, ISNULL((Postcode),'') As POST_CODE, ISNULL((NodeName),'') As UserName, ('') As Password, Package, (Q1) As Livebox, ISNULL((Q4),'') As TalkService,

    ISNULL(LEFT(Q5, ISNULL(NULLIF(CHARINDEX('-', Q5, 0) -1, -1),LEN(Q5))),'') As DiallingCode, ISNULL(REPLACE(Q1B,'No',''),'') As USBModem

    FROM cmp_OrangeBroadband

    WHERE LastCRC = 'Sale' AND (CONVERT(Varchar, CallDate, 103) = CONVERT(Varchar, GETDATE()-1, 103))

    ORDER BY CONVERT(Varchar, CallDate, 103) ASC, CallTime ASC

    Can anyone help me?

  • [font="Verdana"]You can not use control statements directly into Select statement. Instead use Case statement. For more information on Case refer BOL

    Mahesh[/font]

    MH-09-AM-8694

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply