How to write a simple IF statement in SQL 2012 query

  • Hi all,

    I have a JOIN query that selects data from 3 specific tables, using a common field called "ReservationID". The column I am pulling from one of the tables is called "PropertyCode" and it contains only one of these 2 values: "ZIL" and "PCH".

    I need to add an IF Statement in my query that will extract the data from the 3 tables only where the PropertyCode="ZIL". Can anyone help in formulating the correct syntax?

  • hoolash (11/25/2013)


    Hi all,

    I have a JOIN query that selects data from 3 specific tables, using a common field called "ReservationID". The column I am pulling from one of the tables is called "PropertyCode" and it contains only one of these 2 values: "ZIL" and "PCH".

    I need to add an IF Statement in my query that will extract the data from the 3 tables only where the PropertyCode="ZIL". Can anyone help in formulating the correct syntax?

    In your case, your "IF" is actually just a filter (WHERE) somedata matches a specific value.

    to filter data, you just use a WHERE statement, something like this

    SELECT

    T1.ReservationID,

    T1.PropertyCode,

    T1.*,

    T2.*,

    T3.*

    FROM Table1 T1

    INNER JOIN Table2 T2

    ON T1.ReservationID = T2.ReservationID

    INNER JOIN Table3 T3

    ON T1.ReservationID = T3.ReservationID

    WHERE T1.PropertyCode='ZIL'

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks, Lowell! 🙂

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

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