How to convert a “dd/mm/yyyy” string to datetime in SQL Server?

前端 未结 6 1048
盖世英雄少女心
盖世英雄少女心 2020-11-29 04:12

I tried this

SELECT convert(datetime, \'23/07/2009\', 111)

but got this error

The conversion of a varchar data type to a datetime

相关标签:
6条回答
  • 2020-11-29 04:12
    SELECT convert(varchar(10), '23/07/2009', 111)
    
    0 讨论(0)
  • 2020-11-29 04:19

    The last argument of CONVERT seems to determine the format used for parsing. Consult MSDN docs for CONVERT.

    111 - the one you are using is Japan yy/mm/dd.

    I guess the one you are looking for is 103, that is dd/mm/yyyy.

    So you should try:

     SELECT convert(datetime, '23/07/2009', 103)
    
    0 讨论(0)
  • 2020-11-29 04:25

    Try:

    SELECT convert(datetime, '23/07/2009', 103)
    

    this is British/French standard.

    0 讨论(0)
  • 2020-11-29 04:25

    You can convert a string to a date easily by:

    CAST(YourDate AS DATE)
    
    0 讨论(0)
  • 2020-11-29 04:31

    SQL Server by default uses the mdy date format and so the below works:

    SELECT convert(datetime, '07/23/2009', 111)
    

    and this does not work:

    SELECT convert(datetime, '23/07/2009', 111)
    

    I myself have been struggling to come up with a single query that can handle both date formats: mdy and dmy.

    However, you should be ok with the third date format - ymd.

    0 讨论(0)
  • 2020-11-29 04:34
    SELECT convert(datetime, '23/07/2009', 103)
    
    0 讨论(0)
提交回复
热议问题