开发者

How do I convert eg '22/03/2005' to a datetime in SQL Server

开发者 https://www.devze.com 2023-01-06 02:18 出处:网络
It开发者_StackOverflow社区s SQL Server 2000. I am starting with a character string in the format DD/MM/YYYYHere\'s the table: http://msdn.microsoft.com/en-us/library/ms187928.aspx

It开发者_StackOverflow社区s SQL Server 2000. I am starting with a character string in the format DD/MM/YYYY


Here's the table: http://msdn.microsoft.com/en-us/library/ms187928.aspx

Sample:

select convert(datetime,'22/03/2005', 103)


SET DATEFORMAT dmy
SELECT CAST('22/03/2005' AS datetime)

or 

SELECT convert(datetime,'22/03/2005', 103)


It depends your the context.
SQL Server understands '2010-06-21' as a date without requiring any convert/cast, so I would just use the string in the format 'yyyy-mm-dd' if that suits your needs.
Otherwise, the other responses using cast may be better if you need to compare with date fields containing hours as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消