SQL结合datediff&datepart查询

 

--datepart(weekday,TxnAt)-1 =6  0表示星期天,6表示星期六
select  datepart(weekday,Txn)-1 as intweekday,(
case when cast(datediff(d,17,datepart(hh,Txn)) as int) < 0 then '-1'
else datediff(d,17,datepart(hh,Txn))
end ) as intdate  
,datepart(hh,Txn)as diffhour,'17',* from (
select * from dbo.TXN where staffno='1122334455' 
union  select * from dbo.ZDCS where staffno='1122334455') a 
where txn between '2016-01-01' and '2016-12-31' and  (datepart(weekday,Txn)-1  =6 or datepart(weekday,Txn)-1  =0  or datediff(d,17,datepart(hh,Txn)) >=0)
order by intdate desc,intweekday desc

 

posted @ 2016-07-14 16:26  iDennis  阅读(298)  评论(0编辑  收藏  举报