Use the SQL Scalar Value Function to obtain the first month/end month of the current quarter of the specified date

Source: Internet
Author: User

-- ===================================================== ======
-- Author: <LK>
-- Create Date: <2008-9-19>
-- Description: <used for quarterly statistics>/* returns the start month of the quarter */
-- Call method: Select DBO. sys_getquarterbegindate ('2017-11-1 ')
-- ===================================================== ======

Alter function [DBO]. [sys_getquarterbegindate] (@ month smalldatetime)
Returns smalldatetime

As

-- Function: returns the start month of the quarter.
Begin

Declare @ DT smalldatetime

If month (@ month) <4
Set @ dt = convert (varchar, year (@ month) + '-1-1'
Else
Begin
If month (@ month) <7
Set @ dt = convert (varchar, year (@ month) + '-4-1'
Else
Begin
If month (@ month) <10
Set @ dt = convert (varchar, year (@ month) + '-7-1'
Else
Set @ dt = convert (varchar, year (@ month) + '-10-1'
End
End

Return @ dt

End

 

 

 

-- ===================================================== ======
-- Author: <LK>
-- Create Date: <2008-9-19>
-- Description: * returns the end of the quarter */
-- Call: Select DBO. sys_getquarterenddate ('2017-11-1 ')
-- ===================================================== ======

Alter function [dbo]. [sys_GetQuarterEndDate] (@ month smalldatetime)
RETURNS smalldatetime

AS

-- Function: returns the end month of the quarter.
BEGIN

DECLARE @ dt smalldatetime

If month (@ month) <4
SET @ dt = CONVERT (VARCHAR, YEAR (@ month) + '-3-31'
ELSE
BEGIN
If month (@ month) <7
SET @ dt = CONVERT (VARCHAR, YEAR (@ month) + '-6-30'
ELSE
BEGIN
If month (@ month) <10
SET @ dt = CONVERT (VARCHAR, YEAR (@ month) + '-9-30'
ELSE
SET @ dt = CONVERT (VARCHAR, YEAR (@ month) + '-12-31'
END
END

RETURN @ dt

END

 

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.