How does SQL Server decide format for implicit datetime conversion?

后端 未结 3 2045
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 07:43
declare @str_datetime varchar(50)
set @str_datetime=\'30-04-2012 19:01:45\' -- 30th April 2012
declare @dt_datetime datetime
select @dt_datetime=@str_datetime
         


        
3条回答
  •  借酒劲吻你
    2020-12-03 08:03

    By default, the date format for SQL server is in U.S. date format MM/DD/YY, unless a localized version of SQL Server has been installed. This setting is fine for cases when an application that requires this functionality is deployed in a manner guaranteeing that dates are used and inserted in the same format across all platforms and locations where the application is used.

    However, in some cases the date must be in a DD/MM/YY format because many countries/regions use this format rather than the U.S. default of MM/DD/YY. This is especially an issue for international applications that are distributed all over the world.

    Source

    So you what you need to do is set date format before hand as so:

    SET DATEFORMAT dmy
    GO
    

    And then your query will work.

提交回复
热议问题