How to SQL IF statements

  • You are not going to be able to change the number of columns returned based on conditional logic like this, but you could easily NULL out the values or change them to empty strings.

    [font="Courier New"]SELECT

    T1.UserId

    , CASE WHEN T2.FirstName = 1 THEN T1.FirstName ELSE NULL END AS FirstName

    , CASE WHEN T2.LastName = 1 THEN T1.LastName ELSE NULL END AS LastName

    , CASE WHEN T2.DOB = 1 THEN T1.DOB ELSE NULL END AS DOB

    FROM

    Table1 T1

    INNER JOIN Table2 T2 ON T1.UserId = T2.UserId[/font]

  • Thanks for the help. I think that's probably the course of action I'm going to have to take.

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

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