Sql server 如何将一年中的两周周期添加到表中?(SQL Server)

Sql server 如何将一年中的两周周期添加到表中?(SQL Server),sql-server,Sql Server,此SQL语句不足以添加正确的两周周期。 我想要的是这样的东西: Column 1 (period) / Column 2 (start period) / Column 3 (end period) 20160115 / 2016-01-01 / 2016-01-15 20160131 / 2016-01-15 / 2016-01-31 20160215 / 2016-02-01 / 2016-02-15 20160229 / 2016-02-16 / 2016-02-29 等等 这就是我现在

此SQL语句不足以添加正确的两周周期。 我想要的是这样的东西:

Column 1 (period) / Column 2 (start period) / Column 3 (end period)
20160115 / 2016-01-01 / 2016-01-15
20160131 / 2016-01-15 / 2016-01-31
20160215 / 2016-02-01 / 2016-02-15
20160229 / 2016-02-16 / 2016-02-29
等等

这就是我现在拥有的,这是错误的/不完整的。 该值已正确插入到表列中,但间隙错误,因为月份的天数不同

有人能帮我吗?多谢各位

    DECLARE @d date= '20020101'
WHILE @d<'20030101'
    BEGIN
        INSERT INTO [TABLE]
        VALUES ((SELECT CONVERT(VARCHAR(8), @d, 112) AS [YYYYMMDD]), @d, @d, 'Fechado', '1')
        SET @d=DATEADD(DAY,15,@d)
    END
DECLARE@d date='20020101'
而@d使用a表示临时数字表,使用
dateadd()
表示使用某些日期数学的两个查询的
union all

rextester演示:

返回:

+----------+-------------+------------+
|  Period  | PeriodStart | PeriodEnd  |
+----------+-------------+------------+
| 20160115 | 2016-01-01  | 2016-01-15 |
| 20160131 | 2016-01-15  | 2016-01-31 |
| 20160215 | 2016-02-01  | 2016-02-15 |
| 20160229 | 2016-02-15  | 2016-02-29 |
| 20160315 | 2016-03-01  | 2016-03-15 |
| 20160331 | 2016-03-15  | 2016-03-31 |
| 20160415 | 2016-04-01  | 2016-04-15 |
| 20160430 | 2016-04-15  | 2016-04-30 |
| 20160515 | 2016-05-01  | 2016-05-15 |
| 20160531 | 2016-05-15  | 2016-05-31 |
| 20160615 | 2016-06-01  | 2016-06-15 |
| 20160630 | 2016-06-15  | 2016-06-30 |
| 20160715 | 2016-07-01  | 2016-07-15 |
| 20160731 | 2016-07-15  | 2016-07-31 |
| 20160815 | 2016-08-01  | 2016-08-15 |
| 20160831 | 2016-08-15  | 2016-08-31 |
| 20160915 | 2016-09-01  | 2016-09-15 |
| 20160930 | 2016-09-15  | 2016-09-30 |
| 20161015 | 2016-10-01  | 2016-10-15 |
| 20161031 | 2016-10-15  | 2016-10-31 |
| 20161115 | 2016-11-01  | 2016-11-15 |
| 20161130 | 2016-11-15  | 2016-11-30 |
| 20161215 | 2016-12-01  | 2016-12-15 |
| 20161231 | 2016-12-15  | 2016-12-31 |
+----------+-------------+------------+
使用a表示临时数字表,使用
dateadd()
表示两个带有日期数学的查询:

rextester演示:

返回:

+----------+-------------+------------+
|  Period  | PeriodStart | PeriodEnd  |
+----------+-------------+------------+
| 20160115 | 2016-01-01  | 2016-01-15 |
| 20160131 | 2016-01-15  | 2016-01-31 |
| 20160215 | 2016-02-01  | 2016-02-15 |
| 20160229 | 2016-02-15  | 2016-02-29 |
| 20160315 | 2016-03-01  | 2016-03-15 |
| 20160331 | 2016-03-15  | 2016-03-31 |
| 20160415 | 2016-04-01  | 2016-04-15 |
| 20160430 | 2016-04-15  | 2016-04-30 |
| 20160515 | 2016-05-01  | 2016-05-15 |
| 20160531 | 2016-05-15  | 2016-05-31 |
| 20160615 | 2016-06-01  | 2016-06-15 |
| 20160630 | 2016-06-15  | 2016-06-30 |
| 20160715 | 2016-07-01  | 2016-07-15 |
| 20160731 | 2016-07-15  | 2016-07-31 |
| 20160815 | 2016-08-01  | 2016-08-15 |
| 20160831 | 2016-08-15  | 2016-08-31 |
| 20160915 | 2016-09-01  | 2016-09-15 |
| 20160930 | 2016-09-15  | 2016-09-30 |
| 20161015 | 2016-10-01  | 2016-10-15 |
| 20161031 | 2016-10-15  | 2016-10-31 |
| 20161115 | 2016-11-01  | 2016-11-15 |
| 20161130 | 2016-11-15  | 2016-11-30 |
| 20161215 | 2016-12-01  | 2016-12-15 |
| 20161231 | 2016-12-15  | 2016-12-31 |
+----------+-------------+------------+

创建一个函数,然后调用查询:

功能:

create FUNCTION advance_biweekly
(
    @current date
)
RETURNS date
AS
BEGIN
    --if it's the first day of month, advance two weeks
    if (DAY(@current) = 1)
        return DATEADD(WEEK, 2, @current)
    else
    --if its last day of month, advance one day
    if (DAY(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @current) + 1, 0)))  = DAY(@current))
        return DATEADD(DAY, 1, @current)
    else
    --else, it's the middle of the month, go to end
        return dateadd(month,((YEAR(@current)-1900)*12)+MONTH(@current)-1,DAY(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @current) + 1, 0)))-1)

    return null

END
GO
代码:


创建一个函数,然后调用查询:

功能:

create FUNCTION advance_biweekly
(
    @current date
)
RETURNS date
AS
BEGIN
    --if it's the first day of month, advance two weeks
    if (DAY(@current) = 1)
        return DATEADD(WEEK, 2, @current)
    else
    --if its last day of month, advance one day
    if (DAY(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @current) + 1, 0)))  = DAY(@current))
        return DATEADD(DAY, 1, @current)
    else
    --else, it's the middle of the month, go to end
        return dateadd(month,((YEAR(@current)-1900)*12)+MONTH(@current)-1,DAY(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @current) + 1, 0)))-1)

    return null

END
GO
代码:


不应该是+14?不应该是+14?看起来我对双周有不同的定义:(对我来说是两周,但看起来只是头15天,剩下的就剩下了?@JuanCarlosOropeza我理解其中的区别,但在问题的开头,我偏离了预期的结果,这表明是半个月。是的,我的咆哮更多的是为了语言障碍。我能理解半个月,看起来像是我对“双周”和你的一模一样。你的回答让我明白了我必须做什么!看来我对双周有不同的定义(对我来说是两周,但看起来只是头15天,剩下的就剩下了?@JuanCarlosOropeza我理解其中的区别,但在问题的开头,我偏离了预期的结果,这意味着半个月。是的,我的咆哮更多的是为了语言障碍。我能理解半个月,似乎是我对“双周”和你的一模一样。你的回答让我明白了我必须做的事情!谢谢。我发现其他解决方案对我来说更容易,我仍然将此逻辑用于其他功能!谢谢。我发现其他解决方案对我来说更容易,我仍然将此逻辑用于其他功能!
2002-01-15
2002-01-31
2002-02-01
2002-02-15
2002-02-28
2002-03-01
2002-03-15
2002-03-31
2002-04-01
2002-04-15
2002-04-30
2002-05-01