An SQL function that converts a date to a week.

Source: Internet
Author: User

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
/*
Returns the Monday, Friday, Sunday, and Monday of the week before the current date.
*/
Alter function dbo. previusweek
(@ Day as varchar (50 ))
Returns @ table (previusmonday varchar (20), previusfriday varchar (20), previussunday varchar (20), previus13weekmonday varchar (20 ))
As
Begin
-- Define date Variables
Declare @ previusday varchar (20), @ previusmonday varchar (20), @ PreviousSunday varchar (20), @ previusfriday varchar (20 ),
@ Previus13weekmonday varchar (20)
-- Get the date of the previous week of the current date
Set @ previusday = convert (varchar (10), dateadd (day,-7, @ Day), 120)
-- Obtain the Monday date of the week
Set @ previusmonday = convert (varchar (10), dateadd (wk, datediff (wk, 0, @ previusday), 0), 120)
-- Get Sunday and Friday
Set @ previussunday = convert (varchar (10), dateadd (day, 6, @ previusmonday), 120)
Set @ previusfriday = convert (varchar (10), dateadd (day, 4, @ previusmonday), 120)
-- Get Monday 13 weeks ago
Set @ previus13weekmonday = convert (varchar (10), dateadd (wk,-13, @ previusmonday), 120)
-- Pay to the temporary table and return
Insert into @ table
Values (@ previusmonday, @ previusfriday, @ previussunday, @ Previous13WeekMonday)
Return
End

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

 

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.