Sql Server string to date conversion

后端 未结 14 711
执笔经年
执笔经年 2020-11-22 09:18

I want to convert a string like this:

\'10/15/2008 10:06:32 PM\'

into the equivalent DATETIME value in Sql Server.

In Oracle, I wou

14条回答
  •  抹茶落季
    2020-11-22 09:50

    I know this is a wicked old post with a whole lot of answers but a lot of people think that they NEED to either break things apart and put them back together or they insist that there's no way to implicitly do the conversion the OP original asked for.

    To review and to hopefully provide an easy answer to others with the same question, the OP asked how to convert '10/15/2008 10:06:32 PM' to a DATETIME. Now, SQL Server does have some language dependencies for temporal conversions but if the language is english or something similar, this becomes a simple problem... just do the conversion and don't worry about the format. For example (and you can use CONVERT or CAST)...

     SELECT UsingCONVERT = CONVERT(DATETIME,'10/15/2008 10:06:32 PM')
            ,UsingCAST   = CAST('10/15/2008 10:06:32 PM' AS DATETIME)
    ;
    

    ... and that produces the follow answers, both of which are correct.

    Like they say on the TV commercials, "But wait! Don't order yet! For no extra cost, it can do MUCH more!"

    Let's see the real power of temporal conversions with the DATETIME and partially examine the mistake known as DATETIME2. Check out the whacky formats that DATETIME can handle auto-magically and that DATETIME2 cannot. Run the following code and see...

    --===== Set the language for this example.
        SET LANGUAGE ENGLISH --Same a US-English
    ;
    --===== Use a table constructor as if it were a table for this example.
     SELECT *
            ,DateTimeCONVERT  = TRY_CONVERT(DATETIME,StringDT)
            ,DateTimeCAST     = TRY_CAST(StringDT AS DATETIME)
            ,DateTime2CONVERT = TRY_CONVERT(DATETIME2,StringDT)
            ,DateTime2CAST    = TRY_CAST(StringDT AS DATETIME2)
       FROM (
             VALUES
             ('Same Format As In The OP'    ,'12/16/2001 01:51:01 PM')
            ,('Almost Normal'               ,'16 December, 2001 1:51:01 PM')
            ,('More Normal'                 ,'December 16, 2001 01:51:01 PM')
            ,('Time Up Front + Spaces'      ,'   13:51:01  16 December   2001')
            ,('Totally Whacky Format #01'   ,'  16  13:51:01  December   2001')
            ,('Totally Whacky Format #02'   ,'  16    December 13:51:01  2001  ')
            ,('Totally Whacky Format #03'   ,'  16    December 01:51:01  PM 2001  ')
            ,('Totally Whacky Format #04'   ,' 2001 16    December 01:51:01  PM ')
            ,('Totally Whacky Format #05'   ,' 2001    December 01:51:01  PM  16  ')
            ,('Totally Whacky Format #06'   ,' 2001 16    December  01:51:01 PM  ')
            ,('Totally Whacky Format #07'   ,' 2001 16    December  13:51:01 PM  ')
            ,('Totally Whacky Format #08'   ,' 2001 16  13:51:01 PM  December    ')
            ,('Totally Whacky Format #09'   ,'   13:51:01   PM  2001.12/16 ')
            ,('Totally Whacky Format #10'   ,'   13:51:01   PM  2001.December/16 ')
            ,('Totally Whacky Format #11'   ,'   13:51:01   PM  2001.Dec/16 ')
            ,('Totally Whacky Format #12'   ,'   13:51:01   PM  2001.Dec.16 ')
            ,('Totally Whacky Format #13'   ,'   13:51:01   PM  2001/Dec.16')
            ,('Totally Whacky Format #14'   ,'   13:51:01   PM  2001 . 12/16 ')
            ,('Totally Whacky Format #15'   ,'   13:51:01   PM  2001 . December / 16 ')
            ,('Totally Whacky Format #16'   ,'   13:51:01   PM  2001 . Dec /   16 ')
            ,('Totally Whacky Format #17'   ,'   13:51:01   PM  2001 . Dec .   16 ')
            ,('Totally Whacky Format #18'   ,'   13:51:01   PM  2001 / Dec .   16')
            ,('Totally Whacky Format #19'   ,'   13:51:01   PM  2001 . Dec -   16 ')
            ,('Totally Whacky Format #20'   ,'   13:51:01   PM  2001 - Dec -   16 ')
            ,('Totally Whacky Format #21'   ,'   13:51:01   PM  2001 - Dec .   16')
            ,('Totally Whacky Format #22'   ,'   13:51:01   PM  2001 - Dec /   16 ')
            ,('Totally Whacky Format #23'   ,'   13:51:01   PM  2001 / Dec -   16')
            ,('Just the year'               ,' 2001      ')
            ,('YYYYMM'                      ,' 200112      ')
            ,('YYYY MMM'                    ,'2001 Dec')
            ,('YYYY-MMM'                    ,'2001-Dec')
            ,('YYYY    .     MMM'           ,'2001    .     Dec')
            ,('YYYY    /     MMM'           ,'2001    /     Dec')
            ,('YYYY    -     MMM'           ,'2001    /     Dec')
            ,('Forgot The Spaces #1'        ,'2001December26')
            ,('Forgot The Spaces #2'        ,'2001Dec26')
            ,('Forgot The Spaces #3'        ,'26December2001')
            ,('Forgot The Spaces #4'        ,'26Dec2001')
            ,('Forgot The Spaces #5'        ,'26Dec2001 13:51:01')
            ,('Forgot The Spaces #6'        ,'26Dec2001 13:51:01PM')
            ,('Oddly, this doesn''t work'   ,'2001-12')
            ,('Oddly, this doesn''t work'   ,'12-2001')
            ) v (Description,StringDT)
    ;
    

    So, yeah... SQL Server DOES actually have a pretty flexible method of handling all sorts of weird-o temporal formats and no special handling is required. We didn't even need to remove the "PM"s that were added to the 24 hour times. It's "PFM" (Pure Freakin' Magic).

    Things will vary a bit depending on the the LANGUAGE is that you've selected for your server but a whole lot of it will be handled either way.

    And these "auto-magic" conversions aren't something new. They go a real long way back.

提交回复
热议问题