Solved

# convert a real number to decimal

Posted on 2010-09-14
759 Views
how would i convert a real number to decimal with 15 places to the left of the .

the database has it stored as real ... I simply want to retrive it as decimal using covert or whatever else works
0
Question by:troyvw
• 6
• 4

LVL 5

Expert Comment

ID: 33677350
a real number has only 4 bytes.  15 digit accuracy just isn't there.

DECLARE @real AS REAL, @dec AS DECIMAL
SET @real = 123456789012345
SELECT @real
SET @dec = @real
SELECT @dec
SET @dec = 123456789012345
SELECT @dec

0

Author Comment

ID: 33677368
What is the maximum and how would i convert it?
0

LVL 5

Expert Comment

ID: 33677399
A real number is accurate to 7 digits left of the decimal.  You can change the data type of the column.  It will convert, but the accuracy of the 15 digits of information was lost when it was stored in a real data type.
0

Author Comment

ID: 33677412
That is ok... it can be seven ... what would the code look like
0

LVL 5

Expert Comment

ID: 33677463
if you don't want to change the data type in the database, I suppose you could:
select  cast(someRealValue as DECIMAL) as DecimalValue from sometable
0

Author Comment

ID: 33677484
How would i specity the precision... for example if i wanted 7 decimal places
0

LVL 5

Expert Comment

ID: 33677612
if you want anything after the decimal, you're out of luck.
In the example below, I specified that I want 10 digits of total precision, with 4 of them coming after the decimal point (That leaves 7 to the left of the decimal point).

``````DECLARE @real REAL

SET @real = 123456.8913

SELECT @real

SELECT CAST(@real AS DECIMAL(11,4))
``````
0

LVL 5

Expert Comment

ID: 33677622
10 should have been 11
0

LVL 5

Accepted Solution

David Christal CISSP earned 500 total points
ID: 33678025

If you want anything after the decimal, you're out of luck.
In the example below, I specified that I want 11 digits of total precision, with 4 of them coming after the decimal point (That leaves 7 to the left of the decimal point).

``````DECLARE @real REAL

SET @real = 123456.8913

SELECT @real

SELECT CAST(@real AS DECIMAL(11,4))
``````
0

Author Closing Comment

ID: 33691585
ty
0

## Featured Post

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+) Â  Â  as used in Oracle; Â  Â  *= Â  Â  =* Â  Â as used in Sybase â€¦
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alonâ€¦
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Itemsâ€¦
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.