佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > SQLServer 日期函數(shù)大全(小結(jié))

SQLServer 日期函數(shù)大全(小結(jié))

熱門標簽:Linux服務(wù)器 網(wǎng)站排名優(yōu)化 地方門戶網(wǎng)站 呼叫中心市場需求 服務(wù)外包 百度競價排名 AI電銷 鐵路電話系統(tǒng)

一、統(tǒng)計語句

1、--統(tǒng)計當前【>當天00點以后的數(shù)據(jù)】

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

2、--統(tǒng)計本周

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

3、--統(tǒng)計本月

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

4、統(tǒng)計當前

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

二、時間函數(shù)

1、當前系統(tǒng)日期、時間

select getdate() 

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

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

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

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

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

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、求相差天數(shù)

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、查詢本周注冊人數(shù)

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

19、上周注冊人數(shù)

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

20、本月注冊人數(shù)

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

21、上月注冊人數(shù)

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

如果要效率,用一下方式

22、查詢本周注冊人數(shù)

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

23、上周注冊人數(shù)

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

24、本月注冊人數(shù)

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

25、上月注冊人數(shù)

select   count(*)   from   [user]  
where   create_day>=dateadd(month,-1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))  
and   create_daydateadd(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、本周注冊人數(shù)

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

31、上周注冊人數(shù)

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

32、本月注冊人數(shù)

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

33、上月注冊人數(shù)

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   上月

今天的所有數(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())=0

本年的所有數(shù)據(jù):select * from 表名 where DateDiff(yy,datetime類型字段,getdate())=0

系統(tǒng)函數(shù):

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

參數(shù) interval的設(shè)定值:

縮寫(Sql Server) Access 和 ASP 說明
Year Yy yyyy 年 1753 ~ 9999
Quarter Qq q 季 1 ~ 4
Month Mm m 月1 ~ 12
Day of year Dy y 一年的日數(shù),一年中的第幾日 1-366
Day Dd d 日,1-31
Weekday Dw w 一周的日數(shù),一周中的第幾日 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()取得系統(tǒng)日期時間;其中DateDiff,DateAdd,DatePart也同是能用于Access和asp中,這些函數(shù)的用法也類似

舉例:

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, 如果直接和當天的時間比較,就總得不到準確數(shù)據(jù),但是我們可以把這種格式的時間[格式化]成 2007-02-02,也就是只有年-月-日,然后把當天的時間也格式化成 年-月-日的格式.
這樣,思路就出來了!
我們格式化日期要用到 Convert()這個函數(shù),要用到3個參數(shù),首先來格式化當天的日期,Convert(varchar(10),getDate(),120)
這樣我們就可以把當天的日期格式化為: 2007-2-2,然后格式化數(shù)據(jù)庫表中的日期
Convert(varchar(10),TimeFiled,120),最后我們就可以用一條Sql語句得到當天的數(shù)據(jù)了.
例如:

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

注意:
Convert()函數(shù)中的各個參數(shù)的意義,第一個參數(shù),varchar(10)是目標系統(tǒng)所提供的數(shù)據(jù)類型,包括 bigint 和 sql_variant。不能使用用戶定義的數(shù)據(jù)類型。第二個參數(shù)是你要轉(zhuǎn)換的字段,我這里是[time]。最后一個就是格式了,這個值是可選的:20或者120都可以,它遵循的是[ODBC 規(guī)范],輸入/輸出樣式為:yyyy-mm-dd hh:mm:ss[.fff]
具體的可以參考Sql Server的聯(lián)機幫助!

======================================================

T-Sql查找表中當月的記錄

思路:將要查找的時間字段用Month()函數(shù)取出其中的月份,然后再取出當前月的月份,對比就OK了
例:

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

今天的所有數(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())=0

本年的所有數(shù)據(jù):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的值會有相應(yīng)的變化 

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

查詢本季度數(shù)據(jù)

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

查詢上季度數(shù)據(jù)

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

查詢本年數(shù)據(jù)

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

查詢上年數(shù)據(jù)

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

查詢當前這周的數(shù)據(jù)

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

查詢上周的數(shù)據(jù)

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

查詢上個月的數(shù)據(jù)

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 下月第一天 

查詢當前月份的數(shù)據(jù)

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

查詢距離當前現(xiàn)在6個月的數(shù)據(jù)

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

查詢時間區(qū)間:

select * from  表   WHERE 時間字段 > '2018-12-11 13:36:31'  and  時間字段  = '2019-01-09 13:36:31'

到此這篇關(guān)于SQLServer 日期函數(shù)大全(小結(jié))的文章就介紹到這了,更多相關(guān)SQLServer 日期函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • SQLserver中用convert函數(shù)轉(zhuǎn)換日期格式的方法
  • SqlServer中的日期與時間函數(shù)
  • Sqlserver 常用日期時間函數(shù)
  • SQLServer日期函數(shù)總結(jié)案例詳解

標簽:蘭州 衡水 銅川 崇左 黃山 仙桃 湘潭 湖南

巨人網(wǎng)絡(luò)通訊聲明:本文標題《SQLServer 日期函數(shù)大全(小結(jié))》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    新邵县| 淮北市| 基隆市| 吉安市| 宝坻区| 娱乐| 石河子市| 乡城县| 连城县| 陇南市| 溆浦县| 巴彦淖尔市| 澄迈县| 乾安县| 什邡市| 左云县| 渭南市| 承德县| 缙云县| 荥阳市| 曲沃县| 灵璧县| 襄樊市| 远安县| 石棉县| 马尔康县| 阳山县| 山西省| 常熟市| 中阳县| 疏勒县| 石嘴山市| 昌宁县| 保山市| 集贤县| 丹凤县| 晋宁县| 额尔古纳市| 铜川市| 龙海市| 台江县|