sql server 的本月、季度、年
1、select * from sysLog where DateDiff(dd,creatDate,getdate())=0 //当天 2、SELECT * FROM sysLog where creatDate between dateadd(week, datediff(week,0,GETDATE()-1),0) and GETDATE() //本周,由于西方国家认为每周的周日是一周的第一天,所以按照中国人的习惯,应该修改成现在这种写法 3、SELECT * FROM sysLog WHERE datediff(month,[creatDate],getdate())=0 //本月 4、select * from sysLog where DATEPART(qq, creatDate) = DATEPART(qq, GETDATE()) and DATEPART(yy, creatDate) = DATEPART(yy, GETDATE()) //本季度 5、select *from sysLog where year(creatDate)=year(getdate()) //本年
6、计算近半年用 and (datediff(month,[creatDate],getdate())=0 or datediff(month,[creatDate],getdate())=1 or datediff(month,[creatDate],getdate())=2 or datediff(month,[creatDate],getdate())=3
or datediff(month,[creatDate],getdate())=4 or datediff(month,[creatDate],getdate())=5 or datediff(month,[creatDate],getdate())=6))拼接
如
select top 10 t.num,t.id,d.subject objname from(select count(l.id) num,d.id from log l inner join docbase d on d.id=l.objid where d.isdelete=0 and d.docstatus=1 and ((DATEDIFF(month,l.createdate,getdate())=6 or DATEDIFF(month,l.createdate,getdate())=0 or DATEDIFF(month,l.createdate,getdate())=1 or DATEDIFF(month,l.createdate,getdate())=2 or DATEDIFF(month,l.createdate,getdate())=3 or DATEDIFF(month,l.createdate,getdate())=4 or DATEDIFF(month,l.createdate,getdate())=5)) and l.logtype='402881e40b6093bf010b60a5849c0007' group by d.id) t inner join docbase d on d.id=t.id order by t.num desc