Date Conversion

  • I have at a table whihc for export purposes to an accounting software needs to have the invoice date in a dd/mm/yyyy format, which is not a problem. But for the purposes of reports, I really need the date in a mm/dd/yyyy format. I attempted to do a datepart and concatenate in the correct order, but get an out of range error. What am I missing?

    Thank you!

  • The CONVERT function offers lots of different data styles. See Books Online for details.

    [font="Verdana"]Markus Bohse[/font]

  • chad_hainline (9/10/2008)


    I have at a table whihc for export purposes to an accounting software needs to have the invoice date in a dd/mm/yyyy format, which is not a problem. But for the purposes of reports, I really need the date in a mm/dd/yyyy format. I attempted to do a datepart and concatenate in the correct order, but get an out of range error. What am I missing?

    Thank you!

    What datatype is your invoice date?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • The original format is a varchar. I attemmpted to do a Convert(varchar,date,101) earlier, but the results came back unchanged.

  • If it's varchar then this would do the trick:

    [font="Courier New"]DROP TABLE #Test

    CREATE TABLE #Test (mmddyyyy VARCHAR(10))

    INSERT INTO #Test

    SELECT '01/01/2008' UNION ALL

    SELECT '31/01/2008' UNION ALL

    SELECT '01/02/2008' UNION ALL

    SELECT '28/02/2008'

    SELECT mmddyyyy,

       SUBSTRING(mmddyyyy,4,2) + '/' + SUBSTRING(mmddyyyy,1,2) + '/' + RIGHT(mmddyyyy,4) AS ddmmyyyy

    FROM #Test

    [/font]

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Thank you very much! Worked like a charm!

Viewing 6 posts - 1 through 5 (of 5 total)

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