Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Trying to add month with a Date

$
0
0

Hi All,

I am trying to add month with a date. it will take the earlier month and add one month . Here is my code

declare @CollectionDate date='10-30-2014'
select @CollectionDate
;WITH CTemp AS (
SELECT TransactionDate=CAST(@CollectionDate AS DATE) ,RemainingTransaction=1
UNION all
SELECT TransactionDate=DATEADD(MONTH,1,CONVERT(date, CONVERT(varchar(4), YEAR(TransactionDate))+'-'+CONVERT(varchar(2),MONTH(TransactionDate))+'-'+ CONVERT(varchar(2),DATEPART(day, @CollectionDate)))),
RemainingTransaction+1
FROM CTemp
WHERE RemainingTransaction < 9
)
select * from CTemp

When I am giving date 10-28-2014 then it is working fine. But when the date is 10-31-2014 then it shows me the error

Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.

I can undestand it is for the month of February but how do I overcome it?

Can anyone help me on this?

Thanks in advance!!


Niladri Biswas


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>