Solved

convert int to decimal in sql

Posted on 2009-05-11
5
1,634 Views
Last Modified: 2012-08-14
I have a int column with a value of 3250. I need to return a 32.50 value. How do I do this? whatever I tried seems to illeminates the 50.
0
Comment
Question by:sleiman
5 Comments
 
LVL 6

Expert Comment

by:bokist
Comment Utility
something like this ??

select  my_field = (convert(float, 3250)) / 100
0
 
LVL 6

Expert Comment

by:bull_rider
Comment Utility
Replace columnname and tablename accordingly in the attached code snippet

Hope this solves your issue.
Select (convert(float,columnname))/100) AS ReturnResult from tablename

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
Comment Utility
I'm more of a C#/C++ buff, but I'll give this a shot.

The way I'd do this in a programming language is convert the number to a String, and insert a decimal two places to the left of the end of it. That way, there's no risk of any loss of precission at all, and it makes things much simpler ;). I'll try and code it out, though my SQL is not top-notch. If this doesn't work for you, let me know, as the syntax varies from engine to engine.

SELECT (SUBSTRING(column, 0, LEN(column) + "." + SUBSTRING(column, LEN(column) - 2) AS Result FROM tblTableName;

I can't remember if Strings in SQL are 0-based or 1-based--if they're not 0-based, it should be a pretty easy fix, though ;)
0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
Comment Utility
Never convert to float. Float know as aproximate data type. Approximate numeric data types do not store the exact values specified for many numbers. It will give you wrong results most of the time. Use decimal instead. Check this out:
Select cast(columnname as decimal)/100.00 as yourresult from yourtable
 
More info on decimal and float datatype below:
http://msdn.microsoft.com/en-us/library/ms187912.aspx
0
 

Author Comment

by:sleiman
Comment Utility
Using the float conversion did not return the result that I needed.

(convert(float, 3250)) / 100  returns 32.0 and I need to get 32.50

(cast(3250 as decimal)/100.00) returns 32.50000

Final solution:
cast(columnname/100.00 as decimal(10,2)) return 32.50
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now