How to convert decimal to scientific in SQL Server 2008 R2 - sql

I have a table named Product. The table looks like this:
ID Product Volume
1 xyz 4654.000000000000000
2 abc 121.000000000000000
I want to represent Volume in a scientific notation. Volume is of datatype decimal(20,8).
How can I do this conversion?

Normally, the formatting and presentation of data should be done by UI, on the client side, but if you insist...
DECLARE #t decimal(20,8) = 4654.000000000000000;
SELECT #t, CONVERT(nvarchar(32), CAST(#t as float), 2)
result
4654.00000000 4.654000000000000e+003
First CAST decimal to float (please note, it may loose precision), then CONVERT float to nvarchar using style 2:
Always 16 digits. Always use in scientific notation.
Note
float type has only 15 digits of precision, so it can't store your decimal(20,8) without loss of precision.

declare #t decimal(20,8)=132423423421.00000000
print cast(#t as float)
you can cast volume field to float in select statement and then you will get scientific notation of it as shown above.

Related

SQL Server 2014 rounding a float gives unexpected result

Could anyone explain the result of the following query? I think this is a bug in SQL Server 2014.
DECLARE #x float
SET #x=58.415
SELECT ROUND(58.415,2), ROUND(#x,2)
Because the first argument is stored as decimal(5,3):
EXEC sp_describe_first_result_set N'SELECT 58.415 x', null, 0;
You have two different codes:
DECLARE #x float
SET #x=58.415
SELECT ROUND(58.415,2), ROUND(#x,2)
GO
DECLARE #x decimal(19,3)
SET #x=58.415
SELECT ROUND(58.415,2), ROUND(#x,2)
GO
Basically, the float is
Approximate-number data types for use with floating point numeric
data. Floating point data is approximate; therefore, not all values in
the data type range can be represented exactly.
An improvement made by #Zohar explaining why the value is converted to decimal:
In Transact-SQL statements, a constant with a decimal point is
automatically converted into a numeric data value, using the minimum
precision and scale necessary. For example, the constant 12.345 is
converted into a numeric value with a precision of 5 and a scale of 3
The explanation for what you are seeing is that floating point arithmetic is not exact in SQL Server (or in any other database or programming language). Here is what is actually happening, with the "real" value being shown for explanation:
SELECT
ROUND(58.415, 2), -- rounds UP to 58.420, this is DECIMAL(10,3), EXACT
ROUND(58.4149999999999, 2) -- rounds DOWN to 58.41
The issue here is that when you made the following variable assignment:
DECLARE #x float
SET #x = 58.415
internally, SQL Server actually stored the value as an approximation, something like 58.41499999999. Then, when rounding to two decimal places, you were left with 58.41.
In general, if you require exact precision, you should use an exact type. In this case, DECIMAL(10,3) would work.

How do I convert this value 1.2851048260000018E7 (double) to int datatype in a SQL query?

When I run a SQL query, I get a big exponential value 1.2851048260000018E7 for the dollar amount. How to convert it to regular value?
I'm assuming you're not really looking for int, but rather a non-scientific notation. If you simply want dollars and cents use the following (since you didn't provide sample code/data, you'll have to adjust this for your query):
SELECT CAST(1.2851048260000018E7 AS decimal(18,2))
If you need more decimal digits for calculations, use whatever would be appropriate for scale with the syntax decimal(precision, scale), described here.
If you really are looking for an int datatype, that is dollars with cents rounded, use:
SELECT CAST(ROUND(1.2851048260000018E7, 0) AS int)
Use caution if leaving out the ROUND function. When you CAST a decimal to int, the number will be truncated at the decimal point, not rounded.

How to convert a COBOL string with implied decimal to decimal type in Netezza?

See I have a COBOL column like this
05 AMOUNT PIC 999V99.
Here the V means decimal is implied. So value 123.45 will be represented as 12345
In order to convert it back to decimal(5,2) in Netezza, I tried the following
CAST('12345' AS DECIMAL(5,2)) --This will cause overflow, of course
CAST('12345' AS DECIMAL(5))/100 --Works, but looks awkward
Does any one know a better way to convert '12345' back to decimal(5,2) in Netezza?
Thanks
I use the method of converting it to pack decimal and then to numeric type.
I noticed an extra character is needed when converting to PACKED DECIMAL. The extra character can be any character.
Prefix TOOLKIT.SQLEXT. is where our extension function installed.
SELECT TOOLKIT.SQLEXT.PACKEDDECIMAL2STR(TOOLKIT.SQLEXT.NUM2PACKEDDEC('12345'||' ') ,2)::NUMERIC(5,2);
Result:
123.45
SELECT TOOLKIT.SQLEXT.PACKEDDECIMAL2STR(TOOLKIT.SQLEXT.NUM2PACKEDDEC('-12345'||' ') ,2)::NUMERIC(5,2);
Result:
-123.45
Try this:
select cast(substr(a.field,1,3)||'.'||substr(a.field,4,2) AS DECIMAL(5,2))
from (select '12345' as field) a

Issue with converting varchar to money back to varchar in sql server

I've got a column in my database which contains a price, stored as a varchar(10). The format that I need to pull it out as has a comma, but no decimal. So, if the price is 1500.00121, it should come out as "1,500", with comma intact. So here is what I have so far:
CONVERT(varchar, CAST(p.Price1 AS money), 1)
This still has the decimal place to ".xx" in it. How can I remove the decimal and trailing numbers while retaining the comma?
Why not stored it in Decimal(10,2) datatype? It's much better than storing it in VARCHAR since you don't have extra casting to another datatype.
You can use CAST and ROUND function:
SELECT ROUND(CAST('1500.00121' AS DECIMAL(10,4)), 0, 1)
That's it. The CAST function converts datatype to another datatype. The ROUND function returns a numeric value, rounded to the specified length or precision.
The original syntax for ROUND is
ROUND ( numeric_expression , length [ ,function ] )
Where Function parameter is the type of operation to perform. Function must be tinyint, smallint, or int. When function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.
SQLFiddle Demo
ROUND (MSDN)
Decimal (MSDN)
I hope it will work
declare #q money = 1500.123
select parsename(convert(varchar,convert(money,#q), 1),2)
No sql server on hand but Something like
Declare #correctlyTyped Money
Set #correctlyTyped = Convert(Money, SomeBadlyTypedField)
Select
Substring(#correctlyTyped,0,DataLength(#correctlyTyped) - CharIndex('.',#correctlyTyped ))
Convert it to money, then select everything up to the decimal point.
Note this is relying on the locale / collation of the data. In mainland Europe for instance, , is the decimal separator and . is the thousand separator.

sql server round function not working well

I am using sql server 2000 and facing round function issue like the following statement working fine.
SELECT ROUND(5 * 7.83, 1)
The result will be 39.2
But when I get these values from the table, it gives 39.1, meaning it truncates and does not round up.
SELECT ROUND(rate * qty, 1)
FROM tbl
The result will be 39.1
rate and qty columns data types are float. Insert 5 in qty and 7.83 in rate, then check it. How I can fix it?
Convert the table values to real,
SELECT ROUND(convert(real,rate)*convert(real,qty),1)
Your sample simply query is not reflective of the data types involved.
Try these two instead:
SELECT ROUND(5 * 7.83, 1)
SELECT ROUND(cast(5 as float) * cast(7.83 as float), 1)
The 2nd one matches your table data types. Float datatypes are not meant for precise decimal calculations, use a decimal type for those instead.
What Every Computer Scientist Should Know About Floating-Point Arithmetic
Without losing too much precision for normal numbers, you can just cast to decimal on the fly to force human-comprehensible decimal arithmetics, e.g.
SELECT ROUND(cast(rate as decimal(10,5)) * cast(qty as decimal(10,5), 1)
FROM tbl