I am trying to cast a string to a decimal and it keeps returning an integer.
The input and expected output is as follows:
0 0
14400 144.00
-11800 -118.00
42400 424.00
60200 602.00
217400 2174.00
5000 50.00
I am using:
Cast(Trim(POLAMNT) as Decimal(10,2)) as POLICYAMNT
but it is returning the values without the decimal.
Cast(Trim(POLAMNT) as Decimal) / pow(10,2) as POLICYAMNT
was the fastest (and cleanest) way to achieve what I needed.
Hive is creating something of the right type. Whatever is printing the results is doing you the favor of removing the .00. SQL is not so friendly. So, convert the result back to a string:
cast(cast(trim(POLAMNT) as decimal(10,2)) as string) as POLICYAMNT
This should have exactly two decimal places.
EDIT:
I misread the question. You seem to just want:
cast(trim(POLAMNT) as decimal(10,2)) / 100
Related
In Microsoft SQL Server 2005, why do the following commands produce integer results?
SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6
In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected.
If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result.
SELECT 151/CAST(6 AS DECIMAL (9,2))
Yes that is standard behavior
do
SELECT 151/6.0
or
SELECT 151/(CONVERT(DECIMAL(9,2),6))
or
SELECT 151/(6 * 1.0)
Because 151 and 6 are integers and you are doing integer division, even before the cast.
You need to make sure at least one of the arguments is a float type:
SELECT 151.0/6
Or
SELECT 151/6.0
Not a direct answer to your question. Still worth to take a look at Operators in Expressions if you need this in SSRS
/ Divides two numbers and returns a floating-point result.
\ Divides two numbers and returns an integer result.
Mod Returns the integer remainder of a division.
You need to give a placeholder for decimal places as well
Example
SELECT 151.000000/6
OR
SELECT 151/6.000000
Both will produce
25.16666666
For the same reason they would in C#, Java and other mainstream languages.
In integer arithmetic, the CAST is after the maths...
The CAST statement is a bit verbose. You can use the following instead:
DECLARE #TO_FLOAT FLOAT = 1.0;
SELECT (1 * #TO_FLOAT) / 2;
Or use a different multiplier type like DECIMAL if you prefer.
Try this:
SELECT 1.0*cast(151/6 AS DECIMAL(9,2))
SELECT 1.0*151/6
I have the below data which I want to multiply together, column A times column B to get column C.
A has datatype string and B has datatype long.
A B
16% 894
15% 200
I have tried this expression in query cast(A as int)*B but it is giving me an error.
You can try below way -
select cast(left(A, patindex('%[^0-9]%', A+'.') - 1) as int)*B
from tablename
You need to remove the '%' symbol before attempting your cast. And assuming you are actually wanting to calculate the percentage, then you also need to divide by 100.00.
cast(replace(A,'%','') as int)/100.00*B
Note: You need to use 100.00 rather than 100 to force decimal arithmetic instead of integer. Or you could cast as decimal(9,2) instead of int - either way ensures you get an accurate result.
You may well want to reduce the number of decimal points returned, in which case cast it back to your desired datatype e.g.
cast(cast(replace(A,'%','') as int)/100.00*# as decimal(9,2))
Note: decimal(9,2) is just an example - you would use whatever precision and scale you need.
The syntax of the cast in SQL Server is CAST(expression AS TYPE);
As you cannot convert '%' to an integer so you have to replace that with an empty character
as below:
SELECT cast(replace(A,'%','') AS int);
Finally you can write as below:
SELECT (cast(replace(A,'%','') AS int)/100.00)*B as C;
This question may answer on many threads but I am unable to find answer specific to my problem.
Q: I am getting data from API (in json format) where all columns are coming as string and inserting into a table which has all columns as string and serving as source table.
Now, I am trying to cast data from that source to destination and making all necessary casting to insert data into destination table. But decimal (16,8) casting failed.
I debug issue at my end and found that during the fetching data from API which is returning the data into json format converting values in some unusual format.
For e.g. 0.00007 converting into 7E-05 and this is happening for many other rows.
I know I can fix this problem at API implementation level. But I asked to solve this at SQL server end. So I need a solution which should convert 7E-05 into 0.00007.
Try something like:
SELECT CAST(CAST(#t AS FLOAT) AS DECIMAL(16,8))
Results in:
0.00007000
CAST to a FLOAT, then to a DECIMAL.
This unusual format is a rather usual scientific notation Wikipedia, read section "E-notation"
You see the E and a number meaning exponent.
"1E2" = 1 * 10^2 = 100
"1E-2" = 1 * 10^(-2) = 0.01
Try this out:
DECLARE #tbl TABLE(Numberstring VARCHAR(100));
INSERT INTO #tbl VALUES('100'),('1E2'),('1E-2'),('7E-05');
SELECT Numberstring
,CAST(Numberstring AS FLOAT)
,CAST(CAST(Numberstring AS FLOAT) AS DECIMAL(20,10))
FROM #tbl;
The result
100 100 100.0000000000
1E2 100 100.0000000000
1E-2 0,01 0.0100000000
7E-05 7E-05 0.0000700000
You can see, that the FLOAT type itself will display the last one in the scientific notation, while the cast to DECIMAL will return the number you are expecting.
I'd be happy with an upvote, but you should accept Shawn's answer as it was earlier than mine :-D
I have a column that stores transaction amounts in 'microcents'
e.g. 127740000 microcents which corresponds to 127.74 USD
I cannot figure out how to get write a query to return 127.74 for me. If I do.
SELECT microcents/1000000
it truncates the return to 127 and I lose the cents
Ugh I figured it out, CAST( blah AS DOUBLE)
Try DECIMAL types select 127740000 / decimal '1000000.0000'. For testing, you can see the type of the output value by using the typeof function.
I have the following piece of code that always results in 0.00. I know for sure that the weight_score is an actual number. Can someone help me format it so that it results in a decimal number. Using SQL server 2008 and this code is in a view. Thanks a lot!
CAST(SUM(weight_score * (45/100)) as decimal(10,2)) As avg_score
45/100 is implicitly treated as an INT divided by another INT, which results in an INT that gets rounded to 0. Add a decimal to each to work around that.
CAST(SUM(weight_score * (45.0/100.0)) as decimal(10,2)) As avg_score
45/100 is treated as INT.So change it to 45.0/100.0.
select CAST(SUM(weight_score * (45.0/100.0)) as decimal(10,2)) As avg_score
Even if you write the following code you will get 0
select (45/100)
If you want to avoid the error, you can use the NULLIF function.