求一个关于日期的SQL语句
时间:2011-12-03
来源:互联网
取得一个月内的数据,例如有字段id,name,datetime
要求参数为日期,比方说输入25,则查出上月25号到今天在记录
求高手指点。。。谢谢。。。
作者: al525la 发布时间: 2011-12-03
作者: hanmengyuan 发布时间: 2011-12-03
select * from tb where [datetime] between convert(varchar(8),dateadd(m,1,getdate()),120)+'25' and getdate()
作者: qianjin036a 发布时间: 2011-12-03
select * from tb where [datetime] between dateadd(dd,24,dateadd(mm,-1,dateadd(dd,-day(getdate())+1,getdate()))) and getdate()
作者: pengxuan 发布时间: 2011-12-03
SQL code
select * from tb
where [datetime] between convert(varchar(8),dateadd(m,1,getdate()),120)+'25' and getdate()
试了 不对啊
作者: al525la 发布时间: 2011-12-03
引用 2 楼 qianjin036a 的回复:
SQL code
select * from tb
where [datetime] between convert(varchar(8),dateadd(m,1,getdate()),120)+'25' and getdate()
试了 不对啊
抱歉,在 dateadd 函数里少打一个负号:
SQL code
create table tb(id int,name varchar(10),[datetime] datetime) insert into tb select 1,'aa','2011-11-5' insert into tb select 1,'bb','2011-11-26' insert into tb select 1,'cc','2011-12-2' go select * from tb where [datetime] between convert(varchar(8),dateadd(m,-1,getdate()),120)+'25' and getdate() /* id name datetime ----------- ---------- ----------------------- 1 bb 2011-11-26 00:00:00.000 1 cc 2011-12-02 00:00:00.000 (2 行受影响) */ go drop table tb
作者: qianjin036a 发布时间: 2011-12-03
if object_id('tb','U') is not null drop table tb go create table tb ( id int, [datetime] datetime ) insert into tb select 1,'2011-11-1' union all select 1,'2011-11-2' union all select 1,'2011-11-3' union all select 1,'2011-11-11' union all select 1,'2011-11-12' union all select 1,'2011-11-13' union all select 1,'2011-11-25' union all select 1,'2011-11-26' union all select 1,'2011-11-27' union all select 1,'2011-12-1' union all select 1,'2011-12-2' union all select 1,'2011-12-3' go select * from tb where [datetime] between convert(varchar(10),dateadd(dd,24,dateadd(mm,-1,dateadd(dd,-day(getdate())+1,getdate()))),120) and getdate() /* id datetime ----------- ----------------------- 1 2011-11-25 00:00:00.000 1 2011-11-26 00:00:00.000 1 2011-11-27 00:00:00.000 1 2011-12-01 00:00:00.000 1 2011-12-02 00:00:00.000 1 2011-12-03 00:00:00.000 (6 行受影响) */
作者: pengxuan 发布时间: 2011-12-03
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28