加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 06:21:50 所属栏目:MsSql教程 来源:网络整理
导读:我有一组行,每行都有一个日期值,我需要选择属于特定日期范围的行.我怎样才能做到这一点? select * from table where convert(int,date_created) between //what should go here? 我想在’20 -10-2010’和’22 -10-2010’之间进行选择. 它一直在抱怨字符串到
我有一组行,每行都有一个日期值,我需要选择属于特定日期范围的行.我怎样才能做到这一点?
select * from table where convert(int,date_created) between //what should go here?

我想在’20 -10-2010’和’22 -10-2010’之间进行选择.

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

解决方法

您需要使用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'

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读