SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Convert VARCHAR(10) to INT (DECIMAL/FLOAT value)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

duanecwilson
Constraint Violating Yak Guru

USA
273 Posts

Posted - 08/15/2013 :  10:30:46  Show Profile  Reply with Quote
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

USA
333 Posts

Posted - 08/15/2013 :  10:49:38  Show Profile  Reply with Quote
One way:
CAST((CAST(myvalue AS FLOAT)+0.1) AS INT)


djj
Go to Top of Page

duanecwilson
Constraint Violating Yak Guru

USA
273 Posts

Posted - 08/15/2013 :  11:29:06  Show Profile  Reply with Quote
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

USA
333 Posts

Posted - 08/15/2013 :  11:40:24  Show Profile  Reply with Quote
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

India
20 Posts

Posted - 08/28/2013 :  06:58:32  Show Profile  Reply with Quote
CAST((CAST(myvalue AS FLOAT)+0.1) AS INT) is correct
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000