sql SQL:几个月的第一个/第二个星期二
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql SQL:几个月的第一个/第二个星期二相关的知识,希望对你有一定的参考价值。
DECLARE @FirstTuesdayOfTwoMonthsBack datetime
DECLARE @SecondTuesdayOfTwoMonthsBack datetime
DECLARE @FirstTuesdayOfPreviousMonth datetime
DECLARE @SecondTuesdayOfPreviousMonth datetime
DECLARE @FirstTuesdayOfThisMonth datetime
DECLARE @SecondTuesdayOfThisMonth datetime
DECLARE @FirstTuesdayOfNextMonth datetime
SET @FirstTuesdayOfTwoMonthsBack = (SELECT DATEADD(Month, DATEDIFF(Month, 0, GETDATE()) - 2 , 0) + 6 - (DATEPART(Weekday, DATEADD(Month, DATEDIFF(Month, 0, GETDATE()) -2, 0)) + (@@DateFirst + 3) + 7) %7) --SECOND tuesday IN the previous month
SET @SecondTuesdayOfTwoMonthsBack = (SELECT DATEADD(Month, DATEDIFF(Month, 0, GETDATE()) - 2 , 0) + 6 + 7 - (DATEPART(Weekday, DATEADD(Month, DATEDIFF(Month, 0, GETDATE()) -2, 0)) + (@@DateFirst + 3) + 7) %7) --SECOND tuesday IN the previous month
SET @FirstTuesdayOfPreviousMonth = (SELECT DATEADD(Month, DATEDIFF(Month, 0, GETDATE()) - 1 , 0) + 6 - (DATEPART(Weekday, DATEADD(Month, DATEDIFF(Month, 0, GETDATE()) -1, 0)) + (@@DateFirst + 3) + 7) %7) --FIRST tuesday IN the previous month
SET @SecondTuesdayOfPreviousMonth = (SELECT DATEADD(Month, DATEDIFF(Month, 0, GETDATE()) - 1 , 0) + 6 + 7 - (DATEPART(Weekday, DATEADD(Month, DATEDIFF(Month, 0, GETDATE()) -1, 0)) + (@@DateFirst + 3) + 7) %7) --SECOND tuesday IN the previous month
SET @FirstTuesdayOfThisMonth = (SELECT DATEADD(Month, DATEDIFF(Month, 0, GETDATE()), 0 ) + 6 - (DATEPART(Weekday, DATEADD(Month, DATEDIFF(Month, 0, GETDATE()), 0)) + @@DateFirst + 3) %7) --FIRST tuesday IN this month
SET @SecondTuesdayOfThisMonth = (SELECT DATEADD(Month, DATEDIFF(Month, 0, GETDATE()), 0) + 6 + 7 - (DATEPART(Weekday, DATEADD(Month, DATEDIFF(Month, 0, GETDATE()), 0)) + (@@DateFirst + 3) + 7) %7) --SECOND tuesday IN the this month
SET @FirstTuesdayOfNextMonth = (SELECT DATEADD(Month, DATEDIFF(Month, 0, GETDATE()) + 1, 0) + 6 - (DATEPART(Weekday, DATEADD(Month, DATEDIFF(Month, 0, GETDATE()) + 1 , 0)) + @@DateFirst + 3) %7) --FIRST tuesday IN the next month
PRINT @FirstTuesdayOfTwoMonthsBack
PRINT @SecondTuesdayOfTwoMonthsBack
PRINT @FirstTuesdayOfPreviousMonth
PRINT @SecondTuesdayOfPreviousMonth
PRINT @FirstTuesdayOfThisMonth
PRINT @SecondTuesdayOfThisMonth
PRINT @FirstTuesdayOfNextMonth
以上是关于sql SQL:几个月的第一个/第二个星期二的主要内容,如果未能解决你的问题,请参考以下文章
如何获取每个月的第二个星期五的日期范围?
SQL 如何得到本月第一个星期天的日期
如何在n个月内使用sql(无程序)获取每个月的第一天和最后一天
前几个月的 SQL 固定日期
SQL 通过微秒时间戳获取过去几个月的行?
sql 查找从现在开始/之前的几个月的日期