[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 794
  • Last Modified:

convert a real number to decimal

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
troyvw
Asked:
troyvw
  • 6
  • 4
1 Solution
 
David Christal CISSPCommented:
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
 
troyvwAuthor Commented:
What is the maximum and how would i convert it?
0
 
David Christal CISSPCommented:
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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
troyvwAuthor Commented:
That is ok... it can be seven ... what would the code look like
0
 
David Christal CISSPCommented:
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
 
troyvwAuthor Commented:
How would i specity the precision... for example if i wanted 7 decimal places
0
 
David Christal CISSPCommented:
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))

Open in new window

0
 
David Christal CISSPCommented:
10 should have been 11
0
 
David Christal CISSPCommented:

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))

Open in new window

0
 
troyvwAuthor Commented:
ty
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now