SQL 动态 where语句(case when)
作者:互联网
select * from tablename
where time>= case when DATEDIFF(DAY,DATEADD(week,DATEDIFF(week,0,getdate()),0),GETDATE())=0
then DATEADD(week,-1,DATEADD(week,DATEDIFF(week,0,GETDATE()),0))
else DATEADD(week,DATEDIFF(week,0,getdate()),0)
end
逻辑 :如果当前是周一,就查询上周一以来的数据,否则就查询本周数据;
//上周一
select DATEADD(week,-1,DATEADD(week,DATEDIFF(week,0,GETDATE()),0))
//本周一
select DATEADD(week,DATEDIFF(week,0,getdate()),0)
标签:case,week,DATEDIFF,when,DATEADD,SQL,getdate,select,GETDATE 来源: https://www.cnblogs.com/xuanyuandai/p/11855024.html