Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting field form varvchar to int

Status
Not open for further replies.

gojohnnygogogogo

Programmer
May 22, 2002
161
GB
hello,
I have a table where some of the number fielda are chars and varchars. I am trying to change them to int, but get erorrs saying can't convert value 2.5 .
how can I convert varchars or chars to ints if numbers with decimals are in the data .?
 
I think you need to convert them to a decimal first then an int

e.g.

select convert(int,(convert(decimal,youcharfield))) from yourtable

Andy
 
hi,
I have just changed the data type to decimal and all numbers which we decimals have been rounded up !!!/
?
 
I'm afraid that was bound to happen..its always best to use the convert in a select statement (as I suggested) to avoid actually changing the data before you are sure it will convert in the way you wish...do you have a backup?...

if so you might want to look at the incorporating the round function which will allow you to truncate the decimal

e.g.

select convert(int,round((convert(decimal,youcharfield)),0,1)) from yourtable
 
I've fiqured it out. when converting to decimal you need to put the scale in aswell.
thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top