SQL里时间怎么比较?

2025-01-03 13:31:11
推荐回答(3个)
回答1:

declare @h int;
set @h=DATEPART(HOUR,getdate());--取时间里的小时

if (@h between 7 and 11)
print '上午好'
else if (@h between 12 and 18)
print '下午好'
else
print '晚上好'

回答2:

1\如果是指90天
select *
from table
where datediff(day,time,getdate())<=90
或者
select *
from table
where time>=dateadd(day,-90,getdate())
2\如果是3月
select *
from table
where time>=dateadd(month,-3,getdate())
3\如果是当前月往前推两个自然月的1号算起
select *
from table
where time>=Dateadd(mm,datediff(mm,0,getdate())-2,0)

回答3:

dclare @time_now date
set @time_now=now()
if @time_now>'7:00' and @time_now<'12:00' then
print "上午好!"
else if @time_now @time_now>12:00' and @time_now<'18:00' then
print "下午好!"
else
print "晚上好!"
end if