Sql查询根据月末日期生成月度付款日期

时间:2016-11-09 10:12:39

标签: sql sql-server sql-server-2008 date sql-server-2012

我想根据"月薪"生成每月付款日期。和"月末"一年的约会。假设我选择了" 01/26/2016" as"支付日期"和" 01/31/2016" as"月末"日期和日期范围是2016年。 那么"月末"日期将是该年度的最后一天,支付日期将是("月末"月份日期 - "支付日期"月份 )2016年全年的其他月份。我已经为截止日期" 01/26/2016"和" 01/31/2016" as"月末"日期。我的SQL版本是2012。 我想知道SQL中的查询。

任何帮助将不胜感激。

enter image description here

截图" 02/08 / 2016" as"支付日期"和" 01/31/2016" as"月末" enter image description here

3 个答案:

答案 0 :(得分:1)

last question相同的逻辑。

您可以如下所示:

DECLARE @StartDate DATETIME = '2016.01.26'
DECLARE @EndDate DATETIME = '2016.01.31'

SELECT
    DATENAME(dw, A.Month) DayNameOfMonth,
    A.[Month],
    DATENAME(dw, A.[Pay Date]) DayNameOfPayDate,
    A.[Pay Date]
FROM
(
    SELECT
         DATEADD(DAY, -1 - (DAY(EOMONTH(@EndDate)) - DAY(@EndDate)),  DATEADD(MONTH, v.m, DATEFROMPARTS(YEAR(@EndDate), 1, 1))) 'Month',
         DATEADD(DAY, -1 - (DAY(EOMONTH(@StartDate)) - DAY(@StartDate)),  DATEADD(MONTH, v.m, DATEFROMPARTS(YEAR(@StartDate), 1, 1))) 'Pay Date'
    FROM
        (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12)) AS v(m)
) A

结果:

DayNameOfMonth                 Month      DayNameOfPayDate               Pay Date
------------------------------ ---------- ------------------------------ ----------
Sunday                         2016-01-31 Tuesday                        2016-01-26
Monday                         2016-02-29 Wednesday                      2016-02-24
Thursday                       2016-03-31 Saturday                       2016-03-26
Saturday                       2016-04-30 Monday                         2016-04-25
Tuesday                        2016-05-31 Thursday                       2016-05-26
Thursday                       2016-06-30 Saturday                       2016-06-25
Sunday                         2016-07-31 Tuesday                        2016-07-26
Wednesday                      2016-08-31 Friday                         2016-08-26
Friday                         2016-09-30 Sunday                         2016-09-25
Monday                         2016-10-31 Wednesday                      2016-10-26
Wednesday                      2016-11-30 Friday                         2016-11-25
Saturday                       2016-12-31 Monday                         2016-12-26

<强>已更新

DECLARE @StartDate DATETIME = '2016.01.31' -- month end
DECLARE @EndDate DATETIME = '2016.02.08' -- pay date

SELECT         
    DATEADD(DAY, -1 - (DAY(EOMONTH(@StartDate)) - DAY(@StartDate)),  DATEADD(MONTH, v.m, DATEFROMPARTS(YEAR(@StartDate), 1, 1))) 'Month End',        
    DATEADD(DAY, DATEDIFF(DAY, @StartDate, @EndDate), DATEADD(DAY, -1 - (DAY(EOMONTH(@StartDate)) - DAY(@StartDate)),  DATEADD(MONTH, v.m, DATEFROMPARTS(YEAR(@StartDate), 1, 1)))) 'Pay Date'
FROM
    (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12)) AS v(m)

更新2

DECLARE @StartDate DATETIME = '2016.01.31' -- month end
DECLARE @EndDate DATETIME = '2016.02.08' -- pay date

SELECT
    *
FROM
(
    SELECT         
        DATEADD(DAY, -1 - (DAY(EOMONTH(@StartDate)) - DAY(@StartDate)),  DATEADD(MONTH, v.m, DATEFROMPARTS(YEAR(@StartDate), 1, 1))) 'Month End',        
        DATEADD(DAY, DATEDIFF(DAY, @StartDate, @EndDate), DATEADD(DAY, -1 - (DAY(EOMONTH(@StartDate)) - DAY(@StartDate)),  DATEADD(MONTH, v.m, DATEFROMPARTS(YEAR(@StartDate), 1, 1)))) 'Pay Date'
    FROM
        (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12)) AS v(m)
) A
WHERE
    YEAR(A.[Pay Date]) = YEAR(@EndDate)

答案 1 :(得分:1)

试试这个

DECLARE @PayDate AS DATE = '2016-Jan-26', @MonthEndDate AS DATE = '2016-Jan-31'
DECLARE @Start AS INT = 0, @End AS INT = 11
DECLARE @DateDiff AS INT = (SELECT DATEDIFF(DAY, @PayDate, @MonthEndDate))

; WITH CTE AS
(
SELECT @Start as number
UNION ALL
SELECT number + 1
FROM CTE
WHERE number < @End
) SELECT 

DATENAME(dw, DATEADD(MONTH, number, @MonthEndDate)) + ', ' + convert(varchar(12), DATEADD(MONTH, number, @MonthEndDate), 113) AS MonthEnd, 
DATENAME(dw, DATEADD(DAY, -@DateDiff, DATEADD(MONTH, number, @MonthEndDate))) + ', ' + convert(varchar(12), DATEADD(DAY, -@DateDiff, DATEADD(MONTH, number, @MonthEndDate)), 113) AS PayDate 
FROM CTE

/* Output:
MonthEnd                    PayDate
---------------------------------------------------
Sunday, 31 Jan 2016         Tuesday, 26 Jan 2016
Monday, 29 Feb 2016         Wednesday, 24 Feb 2016
Thursday, 31 Mar 2016       Saturday, 26 Mar 2016
Saturday, 30 Apr 2016       Monday, 25 Apr 2016
Tuesday, 31 May 2016        Thursday, 26 May 2016
Thursday, 30 Jun 2016       Saturday, 25 Jun 2016
Sunday, 31 Jul 2016         Tuesday, 26 Jul 2016
Wednesday, 31 Aug 2016      Friday, 26 Aug 2016
Friday, 30 Sep 2016         Sunday, 25 Sep 2016
Monday, 31 Oct 2016         Wednesday, 26 Oct 2016
Wednesday, 30 Nov 2016      Friday, 25 Nov 2016
Saturday, 31 Dec 2016       Monday, 26 Dec 2016
*/

答案 2 :(得分:0)

DECLARE @Y int = 2016;

SELECT RD.ME MonthEnd, RD.PD PayDate FROM 
(select
 1 as mth 
union 
select 2 
union 
select 3 
union 
select 4 
union 
select 5
union 
select 6
union 
select 7 
union 
select 8
union 
select 9
union 
select 10 
union 
select 11
union 
select 12) M 
CROSS APPLY (SELECT DATEADD(month, M.mth - 1, DATEADD(year, @y - 1900, 0)) RM ) OA
CROSS APPLY (SELECT DATEADD(day, -1 , DATEADD(month, 1, OA.RM)) ME,  DATEADD(day, -6 , DATEADD(month, 1, OA.RM)) PD) RD ORDER BY RD.ME