此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
答案 0 :(得分:0)
使用common table expression表示特殊数字表,union all
表示使用dateadd()
进行日期数学计算的两个查询:
declare @startdate date = '2016-01-01'
declare @enddate date = '2016-12-31'
;with cte as (
select top (datediff(month,@startdate,@enddate)+1)
Month = convert(date,dateadd(month,row_number() over (order by (select 1))-1,@startdate))
from master..spt_values
order by month
)
select
Period = convert(char(8), dateadd(day,14,month), 112)
, PeriodStart = month
, PeriodEnd = dateadd(day,14,month)
from cte
union all
select
Period = convert(char(8), eomonth(month), 112)
, PeriodStart = dateadd(day,14,month)
, PeriodEnd = eomonth(month)
from cte
order by period
rextester演示:http://rextester.com/BSYIXW7864
返回:
+----------+-------------+------------+
| 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 |
+----------+-------------+------------+
答案 1 :(得分:0)
创建一个函数,然后调用您的查询:
功能:
urlpatterns = [
url(r'^(?P<username>[\w.@+-]+)/$', UserDetailAPIVIew.as_view(),
name='user-posts-api'),
url(r'^(?P<username>[\w.@+-]+)/follow/$',
FollowToggleAPIView.as_view(), name='follow-api'),
]
代码:
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
结果:
DECLARE @d date= '20020101'
WHILE @d<'20030101'
begin
set @d = dbo.advance_biweekly(@d)
print @d
end