T-SQL 時間操作(dateadd ,datediff ,datepart ,datename ),datepartdatename

來源:互聯網
上載者:User

T-SQL 時間操作(dateadd ,datediff ,datepart ,datename ),datepartdatename

--1.  當前系統日期、時間 select getdate() --2015-01-06 09:27:27.277--2.時間操作 dateadd  在向指定日期加上一段時間的基礎上,返回新的 datetime 值dateadd(datepart,number,date)註:datepart : year(yy, yyyy) | quarter(qq,q) | month(mm,m) | week(ww,wk) | day(dd,dy) | hour(h) | minute(mi,n) | second (ss,s)| millisecond(ms)date 參數是合法的日期運算式。number 是您希望添加的間隔數;對於未來的時間,此數是正數,對於過去的時間,此數是負數。例:select getdate()                   select dateadd(year,1,getdate())  --2016-01-06 09:27:27.277  select dateadd(month,1,getdate()) --2017-02-06 09:27:27.277  select dateadd(day,1,getdate())   --2015-01-07 09:27:27.277select dateadd(week,1,getdate())  --2015-01-13 09:27:27.277select dateadd(weekday,1,getdate()) --2015-01-07 09:27:27.277select dateadd(hour,1,getdate())    --2015-01-06 10:27:27.277select dateadd(Minute,1,getdate())  --2015-01-06 09:28:27.277select dateadd(Second,1,getdate()) --2015-01-06 09:28:28.277--3. datediff 返回跨兩個指定日期的日期和時間邊界數。datediff( date-part, startdate, enddate )註:datepart : year(yy, yyyy) | quarter(qq,q) | month(mm,m) | week(ww,wk) | day(dd,dy) | hour(h) | minute(mi,n) | second (ss,s)| millisecond(ms)startdate 和 enddate 參數是合法的日期運算式。傳回值:(enddate)-(startdate)例: select datediff(year,getdate(),dateadd(year,2,getdate()))     --返回:2 SELECT * FROM crm_contract where datediff(month,Subtime,getdate())=0  --查看crm_contract申請時間subtime是本月的資料  --4. datepart 函數用於返回日期/時間的單獨部分,比如年、月、日、小時、分鐘等等。--如果需要得到自己需要的日期,則需要講日期時間格式化SELECT DATEPART(year, getdate())    as '年份'   SELECT DATEPART(month, getdate())   as '月份' SELECT DATEPART(day, getdate())     as '日期' SELECT DATEPART(week, getdate())    as  '本年第幾周數'SELECT (DATEPART(weekday, getdate())-1)  as '今天是周幾?'  SELECT DATEPART(hour, getdate())    as '小時' SELECT DATEPART(Minute, getdate())  as '分鐘'  SELECT DATEPART(Second, getdate())  as '秒時' --5. datename 返回代表指定日期的指定日期部分的字串SELECT datename(weekday, getdate())                  as '今天是星期幾?'SELECT datename(month,convert(varchar(10), getdate(),126)) as '今天是幾月份?'SELECT datename(day,convert(varchar(10), getdate(),126))   as '今天是幾號?'SELECT datename(year,convert(varchar(10), getdate(),126))   as '今年是哪年?'--上周周一SELECT   DATEADD(wk,   DATEDIFF(wk,0,getdate()),-7)--上周周天SELECT   DATEADD(wk,   DATEDIFF(wk,0,getdate()),-1)--.本周周一 SELECT   DATEADD(wk,   DATEDIFF(wk,0,getdate()),0) --.本周周天  select   dateadd(wk,datediff(wk,0,getdate()),6)--下周周一select   dateadd(wk,datediff(wk,0,getdate()),7)--下周周天select   dateadd(wk,datediff(wk,0,getdate()),13)--1.本月第一天   SELECT   DATEADD(mm,   DATEDIFF(mm,0,getdate()),   0)     --.本月最後一天   SELECT   dateadd(ms,-3,DATEADD(mm,   DATEDIFF(m,0,getdate())+1,   0))      --.本周星期一   SELECT   DATEADD(wk,   DATEDIFF(wk,0,getdate()),   0)   --.本周星期天    select   dateadd(wk,datediff(wk,0,getdate()),6)   --.本年第一天   SELECT   DATEADD(yy,   DATEDIFF(yy,0,getdate()),   0)    --.本年最後一天   SELECT   dateadd(ms,-3,DATEADD(yy,   DATEDIFF(yy,0,getdate())+1,   0))     --.本季度第一天   SELECT   DATEADD(qq,   DATEDIFF(qq,0,getdate()),   0)        --.當天的半夜   SELECT   DATEADD(dd,   DATEDIFF(dd,0,getdate()),   0)        --.上個月的最後一天   SELECT   dateadd(ms,-3,DATEADD(mm,   DATEDIFF(mm,0,getdate()),   0))        --.去年的最後一天   SELECT   dateadd(ms,-3,DATEADD(yy,   DATEDIFF(yy,0,getdate()),   0))         --.本月的第一個星期一   select   DATEADD(wk,   DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())),   0)



相關文章

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.