SQL 擷取當前日期,年、月、日、周、時、分、秒

來源:互聯網
上載者:User

標籤:

select GETDATE() as ‘當前日期‘,    DateName(year,GetDate()) as ‘年‘,    DateName(month,GetDate()) as ‘月‘,    DateName(day,GetDate()) as ‘日‘,    DateName(dw,GetDate()) as ‘星期‘,    DateName(week,GetDate()) as ‘周數‘,    DateName(hour,GetDate()) as ‘時‘,    DateName(minute,GetDate()) as ‘分‘,    DateName(second,GetDate()) as ‘秒‘

結果:

2016-03-23 11:04:14.450  2016 03 23 星期三 13 11 4 14       

 

1.顯示本月第一天
SELECT DATEADD(mm,DATEDIFF(mm,0,getdate()),0) 
select convert(datetime,convert(varchar(8),getdate(),120)+‘01‘,120)

2.顯示本月最後一天
select dateadd(day,-1,convert(datetime,convert(varchar(8),dateadd(month,1,getdate()),120)+‘01‘,120))
SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(m,0,getdate())+1,0)) 

3.上個月的最後一天 
SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(mm,0,getdate()),0)) 

4.本月的第一個星期一
select DATEADD(wk,DATEDIFF(wk,0, dateadd(dd,6-datepart(day,getdate()),getdate())),0)

5.本年的第一天 
SELECT DATEADD(yy,DATEDIFF(yy,0,getdate()),0) 

6.本年的最後一天 
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))

7.去年的最後一天 
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate()),0))

8.本季度的第一天 
SELECT DATEADD(qq,DATEDIFF(qq,0,getdate()),0)  

9.本周的星期一 
SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0) 

10.查詢本月的記錄 
select * from tableName where DATEPART(mm, theDate) = DATEPART(mm, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE()) 

11.查詢本周的記錄 
select * from tableName where DATEPART(wk, theDate) = DATEPART(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE()) 

12.查詢本季的記錄 
select * from tableName where DATEPART(qq, theDate) = DATEPART(qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE()) 
其中:GETDATE()是獲得系統時間的函數。

13.擷取當月總天數:
select DATEDIFF(dd,getdate(),DATEADD(mm, 1, getdate()))

select datediff(day,
dateadd(mm, datediff(mm,‘‘,getdate()), ‘‘),
dateadd(mm, datediff(mm,‘‘,getdate()), ‘1900-02-01‘))

14.擷取當前為星期幾
DATENAME(weekday, getdate())

15. 當前系統日期、時間 
select getdate() 

16. dateadd 在向指定日期加上一段時間的基礎上,返回新的 datetime 值
例如:向日期加上2天 
select dateadd(day,2,‘2004-10-15‘) --返回:2004-10-17 00:00:00.000

17. datediff 返回跨兩個指定日期的日期和時間邊界數。
select datediff(day,‘2004-09-01‘,‘2004-09-18‘) --返回:17

18. datepart 返回代表指定日期的指定日期部分的整數。
SELECT DATEPART(month, ‘2004-10-15‘) --返回 10
年為year,月為month,日為day,小時hour,分為minute,秒為second

19. datename 返回代表指定日期的指定日期部分的字串
SELECT datename(weekday, ‘2004-10-15‘) --返回:星期五

17. day(), month(),year() --可以與datepart對照一下
select 當前日期=convert(varchar(10),getdate(),120),目前時間=convert(varchar(8),getdate(),114) 
select datename(dw,‘2004-10-15‘) 
select 本年第多少周=datename(week,‘2004-10-15‘),今天是周幾=datename(weekday,‘2004-10-15‘)

函數 參數/功能
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) 說明
Year Yy 年 1753 ~ 9999
Quarter Qq 季 1 ~ 4
Month Mm 月1 ~ 12
Day of year Dy 一年的日數,一年中的第幾日 1-366
Day Dd 日,1-31
Weekday Dw 一周的日數,一周中的第幾日 1-7
Week Wk 周,一年中的第幾周 0 ~ 51
Hour Hh 時0 ~ 23
Minute Mi 分鐘0 ~ 59
Second Ss 秒 0 ~ 59
Millisecond Ms 毫秒 0 ~ 999

舉例:
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年

應用樣本:

查詢某個日期之間的記錄資料:
select * from 表 where 開始時間>‘2005-02-01‘ and 結束時間<=‘2005-06-05‘order by id desc


查詢最近30內的記錄資料:
select * from 表 where datediff(Dd,last_date,getdate())<=30 order by id desc

查詢最近一周內的點擊率大於100的記錄資料:
select * from t_business_product where hit_count>100 and datediff(Dw,last_date,getdate())<=7 order by id desc

查詢某一年(如2006年)的記錄資料:
select * from 表 where DatePart(Yy,last_date)=2006 order by id desc
或
select * from 表 where DatePart(Year,last_date)=2006 order by id desc

如查詢系統當前年份插入的一年內的資料:
select * from 表 where DatePart(Yy,getdate())=DatePart(Yy,getdate()) order by id desc

取系統日期 並將 日期 分開 
select 當前日期=convert(varchar(10),dateadd(day,-1,getdate()),120),目前時間=convert(varchar(8),getdate(),114)

取 年月日
year(),month(),date()

SQL 擷取當前日期,年、月、日、周、時、分、秒

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.