Obtain the SQL server functions on the first and last days of a month.

Source: Internet
Author: User
Leave a base: Set Ansi_nulls On
Go
Set Quoted_identifier On
Go
-- ========================================================== =====
-- Author: rickylin
-- Create Date: 2007-11-27
-- Description: gets the date of the first day of a month.
-- ========================================================== =====
Alter   Function Fngetfirstdayofmonth
(
@ Year   Smallint
, @ Month Tinyint
)
Returns   Smalldatetime
As
Begin
Declare   @ Firstday   Smalldatetime
Declare   @ Str Varchar ( 10 )
Set   @ Str   =   Cast ( @ Year   As   Varchar ( 4 )) +   ' - '   +   Cast ( @ Month   As   Varchar ( 2 )) +   ' -1 '

Set @ Firstday = Convert(Smalldatetime,@ Str)

Return @ Firstday

End
Go

-- ========================================================== =====
-- Author: rickylin
-- Create Date: 2007-11-27
-- Description: gets the date of the last day of a month.
-- ========================================================== =====
Create   Function Fngetlastdayofmonth
(
@ Year   Smallint
, @ Month Tinyint
)
Returns   Smalldatetime
As
Begin
Declare   @ Lastday   Smalldatetime
Declare   @ Str Varchar ( 10 )
Set   @ Str   =   Cast ( @ Year   As   Varchar ( 4 )) +   ' - '   +   Cast ( @ Month   As   Varchar ( 2 )) +   ' -1 '

set @ lastday = dateadd (D, - 1 , dateadd (M, 1 , DBO. fngetfirstdayofmonth ( @ year , @ month )))

Return @ Lastday

End
Go

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.