0
votes

I need to get 03-01 as the result @startmonth but I keep getting

Conversion failed when converting date and/or time from character string.

DECLARE @startmonth date, @newdate date

SELECT @newdate    = DATEADD(DAY, 1, CONVERT(DATE, '2012-02-29'))
SELECT @startmonth = CONVERT(DATE,RIGHT(RTRIM(CONVERT(DATE,@newdate)),5))

print @startmonth 
1

1 Answers

0
votes

If you want to store the result as 03-01 then the variable cannot be of date type. Change the @startmonth datatype to varchar

Also there are few unwanted codes in your query. Try this.

DECLARE @startmonth VARCHAR(6),@newdate    DATE

SELECT @newdate = Dateadd(DAY, 1,'2012-02-29')

SELECT @startmonth = LEFT(CONVERT(VARCHAR(15), @newdate, 110), 5)

Print @startmonth