Implementation Method of the start date of the first week of a year

Source: Internet
Author: User

Reference Web site http://www.jb51.net/article/29551.htm
Copy codeThe Code is as follows:
SELECT [StartDate] FROM [dbo]. [udf_Week] (hz2012) WHERE [Week] = 1

Execution result of the preceding SQL statement:

However, you still think this method is complicated and only requires the date part, and the time part does not need to be displayed. Therefore, if Insus. NET changes and generates a new function, you can add the function to your database to apply it in the program.
Copy codeThe Code is as follows:
Udf_StartDateOfFirstWeek
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create function udf_StartDateOfFirstWeek
(
@ Year INT
)
RETURNS DATE
AS
BEGIN
DECLARE @ StartDate DATE = CAST (@ Year as varchar (4) + '-01-01') as date)
RETURN
Case when (DATEPART (DW, @ StartDate)> 4)
Then dateadd (DAY, (8-DATEPART (DW, @ StartDate), @ StartDate)
Else dateadd (DAY, (-(DATEPART (DW, @ StartDate)-1), @ StartDate)
END
END
GO

Function execution Demo:

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.