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
troyvwAsked:
Who is Participating?
 
David Christal CISSPConnect With a Mentor Commented:

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
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
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
 
troyvwAuthor Commented:
ty
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.