根据当天的时间如何查询一个月,三个月,六个月,一年的数据。

yuanhua123 2005-01-10 05:36:32
请问如何用SQL实现,请给出例子。谢谢!
...全文
327 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangzs8896 2005-01-11
  • 打赏
  • 举报
回复
楼上说的很明白了,再不明白就:
查看一下datediff函数就明白了!
select * from tablename where datediff(m,lawintime,getdate())=1
意思是把你getdate()所在月的上一个月的数据取出来了。
datediff(m,lawintime,getdate())的意思是 getdate()所在的月 - lawintime的月

xluzhong 2005-01-11
  • 打赏
  • 举报
回复
select * from tablename where datediff(m,lawintime,getdate())=0

如果getdate()是2005-1-11
select * from tablename where datediff(m,lawintime,getdate())=0
则查出的数据为2005-1-1到2005-1-31

根据你自己的需要,设置等号或大于等运算符,以及运算符后面的数值
yuanhua123 2005-01-11
  • 打赏
  • 举报
回复
请问 ( xluzhong(打麻将一缺三,咋办?) )
在这个字段里的时间 这种格式。
lawintime
2005-1-1
2005-1-2
2005-1-3
2005-1-11
select * from tablename where datediff(m,lawintime,getdate())=1
就查不出来。请问有办法处理吗?谢谢!
xluzhong 2005-01-11
  • 打赏
  • 举报
回复
select * from tablename where datediff(m,lawintime,getdate())=1
select * from tablename where datediff(m,lawintime,getdate())<3
select * from tablename where datediff(m,lawintime,getdate())<6
select * from tablename where datediff(y,lawintime,getdate())<2
yuanhua123 2005-01-11
  • 打赏
  • 举报
回复
我的表如下 law_info
列名 数据类型 数据长度
id int 4
lawintime datetime 8
我在sql2000里面 使用

select * from law_info where lawintime(m,coldate,getdate())=1
出错 说 lawintime 不是可以识别的函数名。 ???????
请 问为什么?
yuanhua123 2005-01-11
  • 打赏
  • 举报
回复
我的表如下 law_info
列名 数据类型 数据长度
id int 4
lawintime datetime 8
我在sql2000里面 使用

select * from law_info where law_intime(m,coldate,getdate())=1
出错 说 law_intime 不是可以识别的函数名。 ???????
请 问为什么?
huangyj 2005-01-10
  • 打赏
  • 举报
回复
xluzhong(打麻将一缺三,咋办?)
的正确
xluzhong 2005-01-10
  • 打赏
  • 举报
回复
select * from tablename where datediff(m,coldate,getdate())=1
select * from tablename where datediff(m,coldate,getdate())<3
select * from tablename where datediff(m,coldate,getdate())<6
select * from tablename where datediff(y,coldate,getdate())<2
子陌红尘 2005-01-10
  • 打赏
  • 举报
回复
select * from tablename where coldate > dateadd(mm,-1,getdate())
select * from tablename where coldate > dateadd(mm,-3,getdate())
select * from tablename where coldate > dateadd(mm,-6,getdate())
select * from tablename where coldate > dateadd(yy,-1,getdate())
僵哥 2005-01-10
  • 打赏
  • 举报
回复
晕倒,没有数据结构如何让人告诉你?

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧