Home Forums SQL Server 2008 T-SQL (SS2K8) CONCATINATE 3 COLUMNS (int) INTO A WORKABLE DATE FORMAT RE: CONCATINATE 3 COLUMNS (int) INTO A WORKABLE DATE FORMAT

  • LoosinMaMind (9/12/2012)


    Hi

    I have been given a new system to work on and the dates are held in three seperate columns

    day number ;ie 1,2,3,4 etc

    month number ; ie 9,10,11 etc

    year; 2011, 2012 etc

    The format of these is an INT

    I need to somehow concatinate the 3 into a workable date format in a SELECT statement

    Any help would be greatly appreciated.

    Thanks in advance

    SAMPLE DATA AND EXPECTED RESULTS SCRIPT BELOW.

    CREATE TABLE PS_TestForOnline

    (

    DAY_ INT,

    MONTH_ INT,

    YEAR_ INT

    );

    INSERT INTO PS_TestForOnline

    VALUES(1,1,2012);

    INSERT INTO PS_TestForOnline

    VALUES(1,2,2012);

    INSERT INTO PS_TestForOnline

    VALUES(1,3,2012);

    SELECT * FROM PS_TestForOnline

    --&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    --EXPECTED RESULTS

    SELECT

    '1-1-2012' DATE_EXAMPLE_1, '1-2-2012' DATE_EXAMPLE_2, '1-3-2012' DATE_EXAMPLE_3 -- NEED THESE TO BE IN DATE FORMAT DD-MM-YYYY

    --&&&&&&&&&&&&&&&&&&&&&&

    DROP TABLE PS_TestForOnline

    If you wanted to make it so you could query the table directly instead of having code do it over and over again, add a persisted computed column to the table to do the calculation that Lowel posted. Just pray people named their columns for inserts or BOOM!

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)