MSSQL gets the first day and the last day of the week on the specified date. Gets the specified date on the first and last days of the month

Source: Internet
Author: User

Ufn_getweekfirstandendday gets the first and last day of the week on the specified date

ALTER FUNCTION [dbo]. [Ufn_getweekfirstandendday] (@tmpDate DATETIME)
RETURNS @tmpTable TABLE (
FirstDay DATETIME,
Endday DATETIME
)
As
BEGIN
INSERT into @tmpTable
SELECT A.firstday,b.endday from (
SELECT 1 as Id,dateadd (wk, DATEDIFF (wk,0, @tmpDate), 0) as FirstDay
) A
Left JOIN (
SELECT 1 as Id,dateadd (wk, DATEDIFF (wk,0, @tmpDate), 6) as Endday
) b
On a.id = b.ID
RETURN
END
Ufn_getmonthfirstandendday Get a specified date sit on the first day of the month and the last day
ALTER FUNCTION [dbo]. [Ufn_getmonthfirstandendday] (@tmpDate DATETIME)
RETURNS @tmpTable TABLE (
FirstDay DATETIME,
Endday DATETIME
)
As
BEGIN
INSERT into @tmpTable
SELECT A.firstday,b.endday from (
SELECT 1 as ID, CONVERT (varchar), DATEADD (Day,-datepart (Day,getdate ()) +1,getdate ()), + ' 00:00:00 ' as ' firstday '
) A
Left JOIN (
SELECT 1 as ID, CONVERT (varchar), DATEADD (Day,-1,dateadd (Month,1,getdate ()-day (GETDATE ()) +1), + ' 23:59:59 ' as ' Endday '
) b
On a.id = b.ID
RETURN
END

MSSQL gets the first day and the last day of the week on the specified date. Gets the specified date on the first and last days of the month

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.