getdate问题2

everwindforce 2003-12-21 06:51:00
如何查询本日或当前日期相关数据(假设有一个日期字段):
如:
本日
最近一周(7天)
上月本日(一天)
两周前的一周(7天)
...全文
28 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
everwindforce 2003-12-21
  • 打赏
  • 举报
回复
谢谢谢谢谢谢大家:)
gmlxf 2003-12-21
  • 打赏
  • 举报
回复
~~~~~~~~~~~~这个时间是我不想要得,怎么去掉呢
--------------
你可以通过convert函数转化成varchar格式(取前10位)
如下:

select convert(varchar(10)b,120)
from abcd
where datediff(d,b,getdate()) between -3 and 3

seekmoon 2003-12-21
  • 打赏
  • 举报
回复
select CONVERT(varchar(10), getDate(),120) --不要时间2002-1-1
select convert(char(8),getdate(),112) ----20020101
select convert(char(8),getdate(),108) ---06:05:05
jingxijun 2003-12-21
  • 打赏
  • 举报
回复
select CONVERT(varchar(10), b,121) from abcd,other fields
where datediff(d,b,getdate()) between -3 and 3
everwindforce 2003-12-21
  • 打赏
  • 举报
回复
偶是要从已有的数据中查询,
刚才试了一下,烛光的回答最符合我的要求.
(其他几位老大的回答偶还没看懂.)

这里还有一个问题:
select *
from abcd
where datediff(d,b,getdate()) between -3 and 3
返回:
1900-01-02 00:00:00.000 2003-12-20 00:00:00.000 NULL
1900-01-03 00:00:00.000 2003-12-20 00:00:00.000 NULL
1900-01-04 00:00:00.000 2003-12-20 00:00:00.000 NULL
1900-01-05 00:00:00.000 2003-12-20 00:00:00.000 NULL
2003-12-20 00:00:00.000 2003-12-19 00:00:00.000 NULL
~~~~~~~~~~~~这个时间是我不想要得,怎么去掉呢
playyuer 2003-12-21
  • 打赏
  • 举报
回复
declare @ datetime
set @ = '2003-12-21' --getdate()
select dateadd(day,-datepart(weekday,@)+2 -case when datepart(weekday,@) = 1 then 7 else 0 end,@) as 周一
,dateadd(day,8-datepart(weekday,@)-case when datepart(weekday,@) = 1 then 7 else 0 end,@) as 周日
,dateadd(month,-1,@) as 上月本日
,dateadd(week,-2,dateadd(day,-datepart(weekday,@)+2 -case when datepart(weekday,@) = 1 then 7 else 0 end,@)) as 两周前周一
,dateadd(week,-2,dateadd(day,8-datepart(weekday,@)-case when datepart(weekday,@) = 1 then 7 else 0 end,@)) as 两周前周日
gmlxf 2003-12-21
  • 打赏
  • 举报
回复
你可以在联机帮助中看看系统日期函数,以下几个比较的常用:

getdate
datediff
dateadd
datepart

还有就是日期型的可以+n来表示今后的日期,如getdate + 1=明天
gmlxf 2003-12-21
  • 打赏
  • 举报
回复
--1、本日
where datediff(d,datecol,getdate()) =0

--2、最近一周(7天)
where datediff(d,datecol,getdate()) between -3 and 3

--3、上月本日(一天)
-- 查询:
select dateadd(m,-1,getdate()) 上月本日
-- 条件
where datediff(d,dateadd(m,-1,getdate()),datecol)=0

--4、两周前的一周(7天)
where datediff(d,datecol,getdate()) between 21 and 27
jingxijun 2003-12-21
  • 打赏
  • 举报
回复
取上个月本日的字符串
select cast(year(getdate()) as char(4))+ cast(month(getdate())-1 as char(2))+cast(day(getdate()) as char(2))

再转换成日期格式
select cast(cast(year(getdate()) as char(4))+ cast(month(getdate())-1 as char(2))+cast(day(getdate()) as char(2)) as datetime)
shuiniu 2003-12-21
  • 打赏
  • 举报
回复
if month(getdate()) = month(字段) and day(getdate()) = day(字段) --上月本日(一天)
jingxijun 2003-12-21
  • 打赏
  • 举报
回复
select getdate()-7
slelect getdate()-30
jingxijun 2003-12-21
  • 打赏
  • 举报
回复
DATEDIFF ( datepart , startdate , enddate )
startdate 是从 enddate 减去。如果 startdate 比 enddate 晚,返回负值。

取当前日期:
select getdate()
shuiniu 2003-12-21
  • 打赏
  • 举报
回复
if DATEDIFF ( dd , 字段 , getdate() ) = 0 --本日
if DATEDIFF ( wk , 字段 , getdate() ) = 0 --本周

34,590

社区成员

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

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