Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my stored procedure Im using cte for recursion of date but for few date Im having some problem

This is my query

SQL
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


If i gave @date='2012-01-25'
CSS
Nos MyDate
1   2012-01-25 
2   2012-02-25 
3   2012-03-25 ...

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

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


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

1 solution

this way...
SQL
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!
:)
 
Share this answer
 
Comments
__TR__ 27-Dec-12 2:14am    
My 5!
Aarti Meswania 27-Dec-12 2:18am    
Thank you! :)
__TR__ 27-Dec-12 2:24am    
Comment from OP
"Hi Aarti Meswania

Im already using dateadd(mm,Nos,@date) this one.
Its not solve my problem.. That's why im asking alternate solution."
Aarti Meswania 27-Dec-12 4:52am    
it's simply working,
don't know why OP says it's not working

thanks for inform :)
kirthiga S 27-Dec-12 6:24am    
Hi Aarti,
My query is working its not a problem but my question is how can i acheive my required result.. Just go through the examples i gave.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900