SQL Server gets (this week, this month, Lent, this season, this year) one day

Source: Internet
Author: User

/*------------------------------this week----------------------------------------*/--first day of the weekSELECT DATEADD( Day,1-(DATEPART(Weekday,getdate())+@ @DATEFIRST-1)%7,getdate())  --orSelect DateAdd(WK,DateDiff(WK,0,getdate()),0)  --The last day of the weekSelect DateAdd(WK,DateDiff(WK,0,getdate()),6)  /*-----------------------------this month, last month, next month-------------------------------------------*/   --first day of last monthSELECT CONVERT(CHAR(Ten),DATEADD(Month,-1,DATEADD(DD,- Day(GETDATE())+1,GETDATE())),111)  --last day of last monthSELECT CONVERT(CHAR(Ten),DATEADD(MS,-3,DATEADD(MM,DATEDIFF(MM,0,getdate()),0)),111)+'23:59:59'  --first day of the monthSelect DateAdd(DD,-DatePart(DD,getdate())+1,getdate())  --first Monday of the monthSELECT DATEADD(WK,DATEDIFF(WK,"',DATEADD(DD,6 -  Day(getdate()),getdate())),"') --last day of the monthSelect DateAdd(DD,-DatePart(DD,getdate()) ,DateAdd(MM,1,getdate()))  --days of the monthSelect DateDiff(DD,DateAdd(DD,-DatePart(DD,getdate())+1,getdate()),DateAdd(DD,-DatePart(DD,getdate())+1,DateAdd(MM,1,getdate())))  --orSelect DatePart(DD,DateAdd(DD,-1,DateAdd(MM,1,cast(cast( Year(getdate()) as varchar)+'-'+cast(Month(getdate()) as varchar)+'-01'  as datetime))))     --the first day of next monthSelect DateAdd(DD,-DatePart(DD,getdate())+1,DateAdd(MM,1,getdate()))  --the last day of next monthSELECT CONVERT(CHAR(Ten),DATEADD(MS,-3,DATEADD(MM,DATEDIFF(M,0,getdate())+2,0)),111)+'23:59:59'  /*-----------------------------this season----------------------------------*/  --first day of the quarterSELECT   DATEADD(QQ,DATEDIFF(QQ,0,getdate()),0)   --last day of the quarter (direct reckoning method)SELECT DATEADD( Day,-1,CONVERT(Char(8),DATEADD(Month,1+DATEPART(Quarter,getdate())*3-Month(getdate()),getdate()), -)+'1')  /*-----------------------------this year, last year----------------------------------*/    --last day of last yearSELECT   DateAdd(MS,-3,DATEADD(YY,DATEDIFF(YY,0,getdate()),0))   --The first day of this yearSELECT   DATEADD(YY,DATEDIFF(YY,0,getdate()),0)   --last day of the yearSELECT  DateAdd(MS,-3,DATEADD(YY,DATEDIFF(YY,0,getdate())+1,0))

SQL Server gets (this week, this month, Lent, this season, this year) one day

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.