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)
 Convert VARCHAR(10) to INT (DECIMAL/FLOAT value)

Author  Topic 

duanecwilson
Constraint Violating Yak Guru

273 Posts

Posted - 2013-08-15 : 10:30:46
I have a source VARCHAR(10) column with float values, such as 12.4, 13.96, 8.899, 54.9 in it t1hat are supposed to end up as integers. Rounding them should only happen if the decimal portion is .9 or higher. So 13.9 would become 14, but 13.899 would become 13.
What would be the best way of handling this?

Duane

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2013-08-15 : 10:49:38
One way:
CAST((CAST(myvalue AS FLOAT)+0.1) AS INT)


djj
Go to Top of Page

duanecwilson
Constraint Violating Yak Guru

273 Posts

Posted - 2013-08-15 : 11:29:06
Thank you. This is a great, simple answer. I should have thought of it myself.
quote:
Originally posted by djj55

One way:
CAST((CAST(myvalue AS FLOAT)+0.1) AS INT)


djj



Duane
Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2013-08-15 : 11:40:24
No problem. Glad I could help. Sometimes you get so involved, you do not see the forest for the trees. :-)

djj
Go to Top of Page

ShivaKrishna
Starting Member

20 Posts

Posted - 2013-08-28 : 06:58:32
CAST((CAST(myvalue AS FLOAT)+0.1) AS INT) is correct
Go to Top of Page
   

- Advertisement -