Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help with Varchar to Int and back conversion

Author  Topic 

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2013-10-10 : 09:46:20
I get "Conversion failed when converting the varchar value ... to data type int when I run the below section of sql code:


EndDate = CASE
WHEN aprop = 'abc' THEN '12/31/' + Cur_Charge_Year + 1)
ELSE '03/31/' + Cur_Charge_Year
END,....


EndDate is defined as a datetime, Cur_Charge_Year is populated with 4 character varchar values.

Could someone please help me out with the needed conversion?

Thanks!

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2013-10-10 : 09:50:44
Disregard, this worked for me:

CONVERT(VARCHAR, (CONVERT(INT, Cur_Charge_Year) + 1))
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-10 : 10:19:26
One suggestion - specify the varchar with a length, for example:
CONVERT(VARCHAR(32), (CONVERT(INT, Cur_Charge_Year) + 1))


http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=188789

Editing: An even better link here: http://myshallowsqlblog.wordpress.com/size-does-matter/
Go to Top of Page

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2013-10-10 : 10:32:25
OK, thanks Jim...
Go to Top of Page
   

- Advertisement -