如何使用SQL获得每周的每周月份ID,开始和结束日期的BiWeekly日期?

时间:2017-11-21 09:07:40

标签: sql sql-server sql-server-2008

示例:Jan 2017

Week 1- 01/01/2017 - 01/14/2017

Week 2 - 01/15/2017 - 01/28/2017

依旧......

数据库:SQL Server

1 个答案:

答案 0 :(得分:0)

试试这个:

elements

或定期15天,例如:

WITH dates AS (
    SELECT DATEADD(DAY,number,CAST('2017-01-01' AS DATE)) day
    FROM master..spt_values WHERE TYPE = 'p'
    AND number < 366
    )

SELECT 
    DATEPART(WEEK, day) as Week, 
    day as FirstWeekDay,
    dateAdd(day, 6, day) as LastWeekDay
FROM dates
WHERE DATENAME(dw, day) IN ('Monday')
Order by day

<强> 更新

如果您的WITH dates AS ( SELECT DATEADD(DAY,number,CAST('2017-01-01' AS DATE)) day FROM master..spt_values WHERE TYPE = 'p' AND number < 366 ) SELECT row_number() over (order by day), day as FirstWeekDay, dateAdd(day, 13, day) as LastWeekDay FROM dates WHERE DATENAME(dw, day) IN ('Sunday') and DATEPART(WEEK, day) % 2 = 1 order by day 表格包含所有日期,则可以使用此查询:

Calendar