MSSQL generation date list

Source: Internet
Author: User

--> Title: generating test data

--> Author: wufeng4552

--> Date: 08:52:38

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [f_getdate] ') and xtype in (n'fn', n'if', n'tf '))

Drop function [DBO]. [f_getdate]

Go

Create Function [DBO]. [f_getdate]

(

@ Year int, -- the year to be queried

@ BZ bit -- @ Bz = 0 query workday, @ Bz = 1 query rest day, @ BZ is null query all date

)

Returns @ Re table (date datetime, weekday nvarchar (3 ))

As

Begin

Declare @ TB table (ID int, date datetime)

Insert @ TB select number,

Dateadd (day, number, dateadd (year, @ year-1900, '2017-1-1 '))

From master .. spt_values where type = 'p' and number between 0 and 366

Delete from @ TB where date> dateadd (year, @ year-1900, '2017-12-31 ')

If @ Bz = 0

Insert into @ Re (date, weekday)

Select date, datename (weekday, date)

From @ TB

Where (datepart (weekday, date) + @ DATEFIRST-1) % 7 between 1 and 5

Else if @ Bz = 1

Insert into @ Re (date, weekday)

Select date, datename (weekday, date)

From @ TB

Where (datepart (weekday, date) + @ DATEFIRST-1) % 7 in (0, 6)

Else

Insert into @ Re (date, weekday)

Select date, datename (weekday, date)

From @ TB

Return

End

Go

Select * From DBO. [f_getdate] ('20140901', 0)

 

 

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.