SQL Server二月超过3月

时间:2017-03-28 12:55:19

标签: sql-server

我对2月份的日期有疑问:

结果如下:

PAYPERIOD
--------------
02/15/2017
03/02/2017

预期结果应该是这样的:

PAYPERIOD
--------------
02/15/2017
02/28/2017

我的代码:

CREATE PROCEDURE [dbo].[Get15thDayPayPeriod]
            @MinDate AS DATE = NULL,
            @MaxDate AS DATE = NULL
AS
-- Execute sp:
-- EXEC [Get15thDayPayPeriod] @MinDate = '02/01/2017', @MaxDate = '02/28/2017'


-- DECLARE @MinDate DATE = CONVERT(VARCHAR(15),DATEADD(month, DATEDIFF(MONTH, 0, GETDATE()), 0), 101), 
-- @MaxDate DATE = CONVERT(VARCHAR(15),EOMONTH(GETDATE()),101),
-- @MinDate DATE = '20170901', 
-- @MaxDate DATE = '20170930',
DECLARE @date DATE; 
DECLARE @counter INT = 0;


IF ISNULL(@MinDate, 0) = 0
BEGIN
    SET @MinDate = CONVERT(VARCHAR(15),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0), 101)
END

IF ISNULL(@MaxDate, 0) = 0 
BEGIN
    SET @MaxDate = CONVERT(VARCHAR(15),EOMONTH(GETDATE()),101)
END

DECLARE my_cursor CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY FOR 
  SELECT TOP (Datediff(day, @MinDate, @MaxDate) + 1) Date = 
  Dateadd(day, Row_number() 
                 OVER( 
                   ORDER 
  BY a.object_id) - 1, @MinDate) 
  FROM   sys.all_objects a 
         CROSS JOIN sys.all_objects b; 
CREATE TABLE #temp (Biweekly varchar(15))
OPEN my_cursor 

FETCH next FROM my_cursor INTO @date 

WHILE @@FETCH_STATUS = 0 
  BEGIN 
      IF( @counter = 15 ) 
        BEGIN
        --PRINT @date
        --PRINT @date -- here is where you get the 15th date
          IF DAY(@date) BETWEEN 10 AND 30
              BEGIN
                SET @date = DATEADD(DAY,(15-DATEPART(DAY,@date)),@date)
                INSERT INTO #temp values (CONVERT(VARCHAR(15), @date, 101))
                PRINT CONVERT(VARCHAR(15), @date, 101)
                SET @counter = 0
              END
              IF  @counter = 0 
              BEGIN
                    SET @date = DATEADD(DAY,(30-DATEPART(DAY,@date)),@date)
                    INSERT INTO #temp values (CONVERT(VARCHAR(15), @date, 101))
                    PRINT CONVERT(VARCHAR(15), @date, 101)
              END
        END 
      SET @counter = @counter + 1 
      FETCH next FROM my_cursor INTO @date 
  END 

CLOSE my_cursor 
DEALLOCATE my_cursor

SELECT PAYPERIOD = CASE WHEN DATENAME(WEEKDAY, Biweekly) <> ('Saturday') AND DATENAME(WEEKDAY, Biweekly) <> ('Sunday')
                    THEN CONVERT(VARCHAR(15), Biweekly, 101)
                    ELSE CASE WHEN DATEPART(dw, Biweekly) <= 2
                        THEN CONVERT(VARCHAR(15),DATEADD(DAY, -1 * (DATEPART(WEEKDAY, Biweekly) + 1), Biweekly), 101)
                        ELSE CONVERT(VARCHAR(15), DATEADD(DAY, DATEPART(WEEKDAY, Biweekly) * -1 + 6, Biweekly), 101)
                        END
                    END 
FROM #temp
DROP TABLE #temp

RETURN 0

其他日期完美无缺,唯一的问题是2月份。

我尝试在IF @counter = 0内添加条件,但没有运气......

1 个答案:

答案 0 :(得分:1)

您是否可以通过使用某些日期函数获得所需内容?

select distinct 
    MonthStart     = dateadd(month, datediff(month, 0,[Date] )  , 0)
  , Month15th      = dateadd(day,14,dateadd(month, datediff(month, 0,[Date] ), 0))
  , MonthEnd       = dateadd(day,-1,dateadd(month, datediff(month, 0,[Date] )+1, 0))
  --, MonthEndEom    = eoMonth(getdate()) /* sql server 2012+*/
from dates;

在此示例中使用adhoc日期表:

declare @fromdate date = '20170101'
declare @thrudate date = '20171231'

;with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, dates as (
  select top (datediff(day, @fromdate, @thrudate)+1)
      [Date]=convert(date,dateadd(day,row_number() over(order by (select 1))-1,@fromdate))
  from n as deka cross join n as hecto cross join n as kilo 
                cross join n as tenK cross join n as hundredK
   order by [Date]
)

select distinct 
    MonthStart = dateadd(month, datediff(month, 0,[Date] )  , 0)
  , Month15th  = dateadd(day,14,dateadd(month, datediff(month, 0,[Date] ), 0))
  , MonthEnd   = dateadd(day,-1,dateadd(month, datediff(month, 0,[Date] )+1, 0))
  --, MonthEndEom    = eoMonth(getdate()) /* sql server 2012+*/
from dates;

2017年,这将返回:

+------------+------------+------------+
| MonthStart | Month15th  |  MonthEnd  |
+------------+------------+------------+
| 2017-01-01 | 2017-01-15 | 2017-01-31 |
| 2017-02-01 | 2017-02-15 | 2017-02-28 |
| 2017-03-01 | 2017-03-15 | 2017-03-31 |
| 2017-04-01 | 2017-04-15 | 2017-04-30 |
| 2017-05-01 | 2017-05-15 | 2017-05-31 |
| 2017-06-01 | 2017-06-15 | 2017-06-30 |
| 2017-07-01 | 2017-07-15 | 2017-07-31 |
| 2017-08-01 | 2017-08-15 | 2017-08-31 |
| 2017-09-01 | 2017-09-15 | 2017-09-30 |
| 2017-10-01 | 2017-10-15 | 2017-10-31 |
| 2017-11-01 | 2017-11-15 | 2017-11-30 |
| 2017-12-01 | 2017-12-15 | 2017-12-31 |
+------------+------------+------------+