sql DATEDIFF 函数常见用法
生活随笔
收集整理的這篇文章主要介紹了
sql DATEDIFF 函数常见用法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
sql? DATEDIFF 函數(shù)
今天的所有數(shù)據(jù):select * from 表名 where DateDiff(dd,datetime類型字段,getdate())=0
昨天的所有數(shù)據(jù):select * from 表名 where DateDiff(dd,datetime類型字段,getdate())=1
7天內(nèi)的所有數(shù)據(jù):select * from 表名 where DateDiff(dd,datetime類型字段,getdate())<=7
30天內(nèi)的所有數(shù)據(jù):select * from 表名 where DateDiff(dd,datetime類型字段,getdate())<=30
半個月的所有數(shù)據(jù):select * from 表名 where DateDiff(mm,datetime類型字段,getdate()) <=15
本月的所有數(shù)據(jù):select * from 表名 where DateDiff(mm,datetime類型字段,getdate())=0
上月的所有數(shù)據(jù):select * from 表名 where DateDiff(mm,datetime類型字段,getdate()) <=1
本年的所有數(shù)據(jù):select * from 表名 where DateDiff(yy,datetime類型字段,getdate())=0
--查詢今天是今年的第幾天
select datepart(dayofyear,getDate()) --
--查詢今天是本月的第幾天
select day(getDate())
--查詢本周的星期一日期是多少
SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0) --2019-08-12 00:00:00.000
--昨天
select convert(varchar(10),dateadd(DD,-1,getdate()),120) --2019-08-12
--本月有多少天
select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast((cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01' ) as datetime )))) --
--兩個時間段相差幾天
select datediff(day,'2012/8/1','2012/8/20') as daysum --19
總結(jié)
以上是生活随笔為你收集整理的sql DATEDIFF 函数常见用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。