1. 程式人生 > 實用技巧 >SQLServer 日期函式大全(詳細)

SQLServer 日期函式大全(詳細)

一、統計語句

1、--統計當前【>當天00點以後的資料】

SELECT * FROM 表 WHERE CONVERT(Nvarchar, dateandtime, 111) = CONVERT(Nvarchar, GETDATE(), 111)   ORDER BY dateandtime DESC

2、--統計本週

SELECT * FROM 表 WHERE datediff(week,[dateadd],getdate())=0

3、--統計本月

SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())=0

4、統計當前

SELECT * FROM 表 WHERE datediff(day,[dateadd],getdate())=0
Select * From table with(nolock) Where Convert(varchar(10),[CreateTime],120) = Convert(varchar(10),getDate(),120)

二、時間函式

1、當前系統日期、時間

select getdate() 

2、dateadd 在向指定日期加上一段時間的基礎上,返回新的 datetime 值,例如:向日期加上2天

select dateadd(day,2,'2004-10-15')   --返回:2004-10-17 00:00:00.000

3、datediff 返回跨兩個指定日期的日期和時間邊界數

select datediff(day,'2004-09-01','2004-09-18')    --返回:17

4、datepart 返回代表指定日期的指定日期部分的整數

SELECT DATEPART(month, '2004-10-15')   --返回 10

5、datename 返回代表指定日期的指定日期部分的字串

SELECT datename(weekday, '2004-10-15')   --返回:星期五

6、day(), month(),year() --可以與datepart對照一下

select 當前日期=convert(varchar(10),getdate(),120),
select 當前時間=convert(varchar(8),getdate(),114),
select datename(dw,'2004-10-15')
select 本年第多少周=datename(week,'2004-10-15'),
select 今天是周幾=datename(weekday,'2004-10-15')

7、求相差天數

select   datediff(day,'2004-01-01',getdate()) 

8、一個月第一天的

SELECT   DATEADD(mm,   DATEDIFF(mm,0,getdate()),   0) 

9、本週的星期一

SELECT   DATEADD(wk,   DATEDIFF(wk,0,getdate()),   0)  
select   dateadd(wk,datediff(wk,0,getdate()),6)  

10、一年的第一天

SELECT   DATEADD(yy,   DATEDIFF(yy,0,getdate()),   0)

11、季度的第一天

SELECT   DATEADD(qq,   DATEDIFF(qq,0,getdate()),   0)

12、當天的半夜

SELECT   DATEADD(dd,   DATEDIFF(dd,0,getdate()),   0)

13、上個月的最後一天

SELECT   dateadd(ms,-3,DATEADD(mm,  DATEDIFF(mm,0,getdate()),   0))

14、去年的最後一天

SELECT   dateadd(ms,-3,DATEADD(yy,   DATEDIFF(yy,0,getdate()),   0)) 

15、本月的最後一天

SELECT   dateadd(ms,-3,DATEADD(mm,   DATEDIFF(m,0,getdate())+1,   0))

16、本年的最後一天

SELECT   dateadd(ms,-3,DATEADD(yy,   DATEDIFF(yy,0,getdate())+1,   0))

17、本月的第一個星期一

select   DATEADD(wk,  DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())),   0)

18、查詢本週註冊人數

select   count(*)   from   [user]  
where   datediff(week,create_day-1,getdate())=0 

19、上週註冊人數

select   count(*)   from   [user]  
where   datediff(week,create_day-1,getdate())=1

20、本月註冊人數

select   count(*)   from   [user]  
where   datediff(month,create_day,getdate())=0 

21、上月註冊人數

select   count(*)   from   [user]  
where   datediff(month,create_day,getdate())=1

如果要效率,用一下方式

22、查詢本週註冊人數

select   count(*)   from   [user]  
where   create_day>=dateadd(day,2-datepart(weekday,getdate()),convert(varchar,getdate(),112))  
and   create_day<dateadd(day,9-datepart(weekday,getdate()),convert(varchar,getdate(),112)) 

23、上週註冊人數

select   count(*)   from   [user]  
where   create_day>=dateadd(day,-5-datepart(weekday,getdate()),convert(varchar,getdate(),112))  
and   create_day<dateadd(day,2-datepart(weekday,getdate()),convert(varchar,getdate(),112)) 

24、本月註冊人數

select   count(*)   from   [user]  
where   create_day>=dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))  
and   create_day<dateadd(month,1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))

25、上月註冊人數

select   count(*)   from   [user]  
where   create_day>=dateadd(month,-1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))  
and   create_day<dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)) 

26、本週

select   count(*)   from   User  
where   datediff(dd,create_day,getdate())   <=   datepart(dw,getdate()) 

27、上週

select   count(*)   from   User  
where   datediff(dd,create_day,(getdate()   -   datepart(dw,getdate())))   <=   7

28、本月

select   count(*)   from   User  
where   datepart(mm,create_day)   =   datepart(mm,getdate()) 

29、上月

select   count(*)   from   User  
where   datepart(mm,create_day)   =   datepart(mm,getdate())   -   1

30、本週註冊人數

select   count(*)   from   [User]  
where   datediff(dd,create_day,getdate())   <=   datepart(dw,getdate()) 

31、上週註冊人數

select   count(*)   from   [User]  
where   datediff(dd,create_day,(getdate()   -   datepart(dw,getdate())))   <=   7

32、本月註冊人數

select   count(*)   from   [User]  
where   datepart(mm,create_day)   =   datepart(mm,getdate())

33、上月註冊人數

select   count(*)   from   [User]  
where   datepart(mm,create_day)   =   datepart(mm,getdate())   -   1

34、查詢今日所有

SELECT * from feedback WHERE (DATEDIFF(d,fedtime,GETDATE())=0) ORDER BY fedid DESC

month(create_day)=month(getdate())本月

month(create_day)=month(getdate())-1 上月

今天的所有資料:select * from表名where DateDiff(dd,datetime型別欄位,getdate())=0

昨天的所有資料:select * from表名where DateDiff(dd,datetime型別欄位,getdate())=1

7天內的所有資料:select * from表名where DateDiff(dd,datetime型別欄位,getdate())<=7

30天內的所有資料:select * from表名where DateDiff(dd,datetime型別欄位,getdate())<=30

本月的所有資料:select * from表名where DateDiff(mm,datetime型別欄位,getdate())=0

本年的所有資料:select * from表名where DateDiff(yy,datetime型別欄位,getdate())=0

系統函式:

系統函式
函式 引數/功能
GetDate( ) 返回系統目前的日期與時間
DateDiff (interval,date1,date2) 以interval 指定的方式,返回date2 與date1兩個日期之間的差值 date2-date1
DateAdd (interval,number,date) 以interval指定的方式,加上number之後的日期
DatePart (interval,date) 返回日期date中,interval指定部分所對應的整數值
DateName (interval,date) 返回日期date中,interval指定部分所對應的字串名稱

引數 interval的設定值:

縮寫(Sql Server) Access 和 ASP 說明
Year Yy yyyy 年 1753 ~ 9999
Quarter Qq q 季 1 ~ 4
Month Mm m 月1 ~ 12
Day of year Dy y 一年的日數,一年中的第幾日 1-366
Day Dd d 日,1-31
Weekday Dw w 一週的日數,一週中的第幾日 1-7
Week Wk ww 周,一年中的第幾周 0 ~ 51
Hour Hh h 時0 ~ 23
Minute Mi n 分鐘0 ~ 59
Second Ss s 秒 0 ~ 59
Millisecond Ms - 毫秒 0 ~ 999

access 和 asp 中用date()和now()取得系統日期時間;其中DateDiff,DateAdd,DatePart也同是能用於Access和asp中,這些函式的用法也類似

1.GetDate() 用於sql server :select GetDate()
2.DateDiff('s','2005-07-20','2005-7-25 22:56:32')返回值為 514592 秒
DateDiff('d','2005-07-20','2005-7-25 22:56:32')返回值為 5 天
3.DatePart('w','2005-7-25 22:56:32')返回值為 2 即星期一(週日為1,週六為7)
DatePart('d','2005-7-25 22:56:32')返回值為 25即25號
DatePart('y','2005-7-25 22:56:32')返回值為 206即這一年中第206天
DatePart('yyyy','2005-7-25 22:56:32')返回值為 2005即2005年

Sql 取當天或當月的記錄
表中的時間格式是這樣的:2007-02-02 16:50:08.050, 如果直接和當天的時間比較,就總得不到準確資料,但是我們可以把這種格式的時間[格式化]成 2007-02-02,也就是隻有年-月-日,然後把當天的時間也格式化成 年-月-日的格式.
這樣,思路就出來了!
我們格式化日期要用到 Convert()這個函式,要用到3個引數,首先來格式化當天的日期,Convert(varchar(10),getDate(),120)
這樣我們就可以把當天的日期格式化為: 2007-2-2,然後格式化資料庫表中的日期
Convert(varchar(10),TimeFiled,120),最後我們就可以用一條Sql語句得到當天的資料了.
例如:

轉自網路

程式程式碼 Select * From VIEW_CountBill Where Convert(varchar(10),[time],120) = Convert(varchar(10),getDate(),120)


注意:
Convert()函式中的各個引數的意義,第一個引數,varchar(10)是目標系統所提供的資料型別,包括 bigint 和 sql_variant。不能使用使用者定義的資料型別。第二個引數是你要轉換的欄位,我這裡是[time]。最後一個就是格式了,這個值是可選的:20或者120都可以,它遵循的是[ODBC 規範],輸入/輸出樣式為:yyyy-mm-dd hh:mm:ss[.fff]
具體的可以參考Sql Server的聯機幫助!

======================================================
T-Sql查詢表中當月的記錄
思路:將要查詢的時間欄位用Month()函式取出其中的月份,然後再取出當前月的月份,對比就OK了
例:

程式程式碼 Select * From VIEW_CountBill Where Month([time]) = Month(getDate())

今天的所有資料:select * from表名 where DateDiff(dd,datetime型別欄位,getdate())=0

昨天的所有資料:select * from表名 where DateDiff(dd,datetime型別欄位,getdate())=1

7天內的所有資料:select * from表名 where DateDiff(dd,datetime型別欄位,getdate())<=7

30天內的所有資料:select * from表名 where DateDiff(dd,datetime型別欄位,getdate())<=30

本月的所有資料:select * from表名 where DateDiff(mm,datetime型別欄位,getdate())=0

本年的所有資料:select * from表名 where DateDiff(yy,datetime型別欄位,getdate())=0

查詢今天是今年的第幾天: select datepart(dayofyear,getDate())

查詢今天是本月的第幾天:1. select datepart(dd, getDate())

2.select day(getDate())

查詢本週的星期一日期是多少 (注意:指定日期不能是週日,如果是週日會計算到下週一去。所以如果是週日要減一天) SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0)

查詢昨天日期:select convert(char,dateadd(DD,-1,getdate()),111) //111是樣式號,(100-114)

查詢本月第一天日期:Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) as firstday

查詢本月最後一天日期:Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)) as lastday //修改-3的值會有相應的變化

本月有多少天: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

在指定的日期上±N天:select convert(char,dateadd(dd,1,'2012/8/20'),111) as riqi //輸出2012/8/21

在指定的日期上±N分鐘:select dateadd(mi,-15,getdate()) //查詢當前時間15分鐘之前的日期

指定時間 : select * from 表名 where 時間欄位 >= to_date('yyyy-MM-dd','1900-01-01');

今天

select * from 表名 where to_days(時間欄位名) = to_days(now());

昨天

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 時間欄位名) <= 1

近7天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(時間欄位名)

近30天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(時間欄位名)

本月

SELECT * FROM 表名 WHERE DATE_FORMAT( 時間欄位名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )

上一月

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 時間欄位名, '%Y%m' ) ) =1

查詢本季度資料

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());

查詢上季度資料

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

查詢本年資料

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());

查詢上年資料

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

查詢當前這周的資料

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());

查詢上週的資料

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

查詢上個月的資料

select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')

select * from user where DATE_FORMAT(pudate,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ; 

select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now()) 

select * from user where MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now()) 

select * from user where YEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = YEAR(now()) and MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now()) 

select * from user where pudate between  上月最後一天  and 下月第一天 

查詢當前月份的資料

select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')

查詢距離當前現在6個月的資料

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查詢時間區間: select * from 表 WHERE 時間欄位 > '2018-12-11 13:36:31' and 時間欄位 <= '2019-01-09 13:36:31'

轉載於:https://www.cnblogs.com/ysySelf/p/10168055.html