如何在cte递归中添加月份 [英] How to add month in cte recursion

查看:172
本文介绍了如何在cte递归中添加月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的存储过程中我使用cte进行日期递归但是几天我有一些问题



这是我的查询



In my stored procedure Im using cte for recursion of date but for few date Im having some problem

This is my query

Declare @date datetime ='2012-01-30'
;with cte as(
select 1 Nos,@date MyDate
union all
select Nos+1,dateadd(mm,1,MyDate)MyDate from cte where Nos<12
)
select * from cte





如果我给@date =''2012-01-25''



If i gave @date=''2012-01-25''

Nos MyDate
1   2012-01-25 
2   2012-02-25 
3   2012-03-25 ...



迭代是正确的。但对于@date =''2012-01-30''输出将是




The iteration is correct. But for @date=''2012-01-30'' The output will be

Nos MyDate
1   2012-01-30 
2   2012-02-29 
3   2012-03-29 ...





为了进行游行,它继续''29''但是我希望''30''用于行军



For march it continues with ''29'' but i want ''30'' for march

推荐答案

这个方式...

this way...
Declare @date datetime
set @date='2012-01-30';

with cte as(
select 1 Nos,@date MyDate
union all
select Nos+1,dateadd(mm,Nos,@date)MyDate from cte where Nos<12
)
select * from cte



快乐编码!

:)


Happy Coding!
:)


这篇关于如何在cte递归中添加月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆