[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 809
  • 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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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