在T-SQL中选择日期范围内的行

时间:2011-06-09 10:43:12

标签: tsql date

我有一组行,每行都有一个日期值,我需要选择属于特定日期范围的行。我怎么能这样做?

select * from table where convert(int,date_created) between //what should go here?

我想在'20-10-2010''22-10-2010'之间进行选择。

它一直在抱怨字符串到日期的转换。

2 个答案:

答案 0 :(得分:20)

您需要使用yyyymmdd这是SQL Server最安全的格式

select * from table
where date_created BETWEEN '20101020' and '20101022'

不确定为什么你有CONVERT到那里......

注意:如果date_created有一个时间组件,因为它假设是在午夜时失败了。

编辑:

要过滤2010年10月20日至2010年10月22日包含的日期,如果date_created列有时间,而不向date_created应用功能:

where date_created >= '20101020' and date_created < '20101023'

答案 1 :(得分:2)

要么不将date_created转换为int,要么使用整数作为数据值

我会将date_created作为约会。

select * from table where date_created between '20101020' and '20101022'

相关问题