• rayh 98086 (7/11/2013)


    Hello,

    I would like to create a SQL query that will make a decision based on date. For example, if the date is Jun 30,2013 or greater, then show 2013, else 2012.

    IF myDate>6/30/2013 THEN '2013' ELSE '2012' as getSeason FROM myTable

    I know this is not the write syntax, but is it possible to do something this way?

    the CASE statement is used in sQL to do that; but in your case, you might want to use the YEAR function for this specific issue;

    so it would look something like this:

    SELECT

    OtherColumns,

    CASE

    WHEN myDate > '6/30/2013'

    THEN '2013'

    ELSE '2012'

    END as getSeason

    FROM myTable

    --or

    SELECT

    OtherColumns,

    YEAR(myDate) AS getSeason

    FROM myTable

    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!