The T-SQL generates a simple Gregorian T-SQL containing the date of the month and the week of the year

Source: Internet
Author: User

-- Added the weekly times of the month and year of the date!
-- Sunday is counted as "last week "! (Note the differences between weekofyear, weekofmonth, myweekofyear, and myweekofmonth)
-- Note that the datename value varies depending on the SQL Server language version or date format!
-- The test environment is: SQL Server 2000 Simplified Chinese version + windows Simplified Chinese version

Declare @ datetime
Set @ = '2017-02-25 11:00:50 '-- 1995 is a Sunday.

Select @ as date
, Dateadd (year, datediff (year, 0, @), 0) the first day of the AS year
, Dateadd (year, 1 + datediff (year, 0, @), 0)-1 as the last day of the year
, Dateadd (quarter, datediff (quarter, 0, @), 0) the first day of the AS season
, Dateadd (quarter, 1 + datediff (quarter, 0, @), 0)-1 as the last day of the season
, Dateadd (month, datediff (month, 0, @), 0) the first day of the AS month
, Dateadd (month, 1 + datediff (month, 0, @), 0)-1 as the last day of the month
, Dateadd (Week, datediff (Week, 0, @), 0) the first day of the AS week
, Dateadd (week, 1 + datediff (Week, 0, @), 0)-1 as the last day of the week

Select
Dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0) as [date]
, Datename (weekday, dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0) as [weekdayname]
, Datepart (weekday, dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0) as [weekday]
, (@ Datefirst + datepart (weekday, dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0) % 7 as [myweekday]

, Datepart (Week, dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0) as weekofyear
, Datediff (Week
, Case when (@ datefirst + datepart (weekday, dateadd (day, 0, datediff (day, 0, dateadd (year, datediff (year, 0 ,@), 0) % 7 = 1
Then dateadd (day,-1, dateadd (day, 0, datediff (day, 0, dateadd (year, datediff (year, 0, @), 0 ))))
Else dateadd (day, 0, datediff (day, 0, dateadd (year, datediff (year, 0, @), 0) -- the first day of the year where date is located: January 1, January 1
End
, Case when (@ datefirst + datepart (weekday, dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0) % 7 = 1
Then dateadd (day,-1, dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0 ))))))
Else dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0 )))))
End
) + 1 as myweekofyear

, Datediff (Week, dateadd (day, 1-day (dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0), dateadd (day, 0, datediff (day, 0, dateadd (day, d. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0), dateadd (day, 0, datediff (day, 0, dateadd (day, d. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0) + 1 as weekofmonth

, Datediff (Week
, Case when (@ datefirst + datepart (weekday, dateadd (month, datediff (month, 0, dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0) % 7 = 1
Then dateadd (month, datediff (month, 0, dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0), 0)-1
Else dateadd (month, datediff (month, 0, dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0), 0)
End
, Case when (@ datefirst + datepart (weekday, dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0) % 7 = 1
Then dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0)-1
Else dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0 )))))
End
) + 1 as myweekofmonth

, Datepart (dayofyear, dateadd (day, 0, datediff (day, 0, dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0) as dayofyear

From
(
Select 0 as I
Union all select 1 Union all select 2 Union all select 3
Union all select 4 union all select 5 Union all select 6
Union all select 7 Union all Select 8 Union all select 9
Union all select 10 Union all select 11
) M
,
(
Select 0 as I
Union all select 1 Union all select 2 Union all select 3 Union all select 4 union all select 5
Union all select 6 Union all select 7 Union all Select 8 Union all select 9 Union all select 10
Union all select 11 union all select 12 Union all select 13 Union all select 14
Union all select 15 Union all select 16 Union all select 17 Union all select 18
Union all select 19 Union all select 20 Union all select 21 Union all select 22
Union all select 23 Union all select 24 Union all Select 25 Union all select 26
Union all select 27 Union all select 28 Union all select 29 Union all select 30
) D

Where datediff (month, dateadd (year, datediff (year, 0, @), 0), dateadd (day, D. i, dateadd (month, M. i, dateadd (year, datediff (year, 0, @), 0) = M. I
Order by [date]


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.