SQL Server 2005 中的 to_date [英] to_date in SQL Server 2005

查看:44
本文介绍了SQL Server 2005 中的 to_date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道我必须如何更改以下内容才能使用 ms sql?

Does any one know how I would have to change the following to work with ms sql?

WHERE registrationDate between to_date ('2003/01/01', 'yyyy/mm/dd')
AND to_date ('2003/12/31', 'yyyy/mm/dd');

我所读到的内容暗示我必须使用 DATEPART() 来构建它,这可能会变得非常冗长.特别是当目标是比较我以以下格式2003-12-30 10:07:42"收到的日期时.最好将它们按原样传递给数据库.

What I have read implies I would have to construct it using DATEPART() which could become very long winded. Especially when the goal would be to compare on dates which I receive in the following format "2003-12-30 10:07:42". It would be nice to pass them off to the database as is.

感谢任何指点.

推荐答案

使用:

WHERE registrationdate BETWEEN '01/01/2003' AND '12/31/2003'

...但正如 gbn 指出的那样,为了区域设置安全 - 使用:

...but as gbn pointed out, to be locale safe - use:

WHERE registrationdate BETWEEN '20030101' AND '20031231'

SQL Server 会将字符串隐式转换为日期,前提是它是受支持的格式.显式转换是指您必须使用 CAST 或 CONVERT 来更改数据类型.

SQL Server will perform implicit conversion of the string into a date, providing it's a supported format. Explicit conversion is when you have to use CAST or CONVERT to change the data type.

将 '01/01/2003' 转换为 DATETIME 时,时间部分将为 00:00:00,因为它没有被指定.

When converting '01/01/2003' to a DATETIME, the time portion will be 00:00:00 because it wasn't specified.

这篇关于SQL Server 2005 中的 to_date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆