The last day of the date and the first day

Source: Internet
Author: User

One, the last day of the month

Returns the last day of the month, this contains the specified date, with an optional offset.

[] )

1, there are two input methods for start_date, which can be converted to the string type of date and the date data type.

Declare @date Date Set @date = getdate ()Select EOMONTH (@date)Select EOMONTH ('  2016-08-06'), EOMONTH ('20160806')

2,month_to_add is an int type that can be a positive integer, a negative integer, and 0, the default value is 0, and if omitted, the default value of 0 is used.

Declare @date Date Set @date = getdate ()Select EOMONTH (@date  as Currentmonth_endday,    EOMONTH ( @date,1 as nextmonth_endday,    EOMONTH (@date,-  1 as Lastmonth_endday

3, the last day of the quarter

Use Eomonth to get the last day of the quarter, just specify any day of the last one months of the quarter, and the last month of the quarter is fixed: 3*quarter_num, the possible value is: 3,6,9,12.

 declare   @date   date  set   @date  =  getdate   ()  select  datepart  (Quarter, @date  ) as   Quarter_num  select  EOMONTH (Datefromparts (year  ( @date ), Span style= "color: #ff00ff;" >datepart  (Quarter, @date ) *  Span style= "color: #800000; Font-weight:bold; " >3 , 1 )) 

Second, the first day of the month
Returns a date value for the specified year, month, and day.

 Year Month  Day)

Using the Datefromparts function, the date type can be obtained from three positive integers (year,month,day).

1, you only need to set the day parameter to 1, you can get the first days of the month.

Declare @date Date Set @date = getdate ()Select datefromparts (year (@date),month(@date), 1

2, get the first day of the quarter

First, use the DatePart function to get the quarter of the current date, possibly the value: 1,2,3,4.

Declare @date Date Set @date = getdate ()selectDATEPART(quarter,@date as Quarter_num

The first day of the quarter is the first day of the quarter one months, so the first month of the quarter matches the formula: (quarter_num-1) *3+1, equivalent to Quarter_num*3-2

Declare @date Date Set @date = getdate ()Select datefromparts (year (@date),DATEPART(quarter,@date )*3-2,1

Reference doc:

DATEPART (Transact-SQL)

DATEFROMPARTS (Transact-SQL)

EOMONTH (Transact-SQL)

The last day of the date and the first day

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.