i have decimal numbers like in the table below, im trying to convert them to decimal, because when i select them, they are come out as integer, for example on row 1 if i select 0.8 or 2.86 it will give me 0 or 2 respectively
im using the following statement
select ELERENVPR, ELEDCSFPM from db-name
i tried cast to convert it
select cast(ELERENVPR, decimal), cast(ELEDCSFPM,decimal) from db-name
im not sure if it works, becouse now its renaming the table to 00001 and 00002, like the picture below
decimal with no scale or precision parameters has defaults, which might be to zero precision.
You need to be explicit about what you want. I would recommend something like decimal(10, 2) (8 digits before the decimal place, 2 after). Something like this:
select cast(ELERENVPR, decimal(10, 2)) as ELERENVPR,
cast(ELEDCSFPM, decimal(10, 2)) as ELEDCSFPM
from db-name ;
The as renames the columns.
Related
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;
I have a column Amount defined in my SQL Server database as varchar(20), null.
It has values like this:
1.56867
2.0
2.0000
2
If the user in the client app enters 2 for search, then I need to pull all records from the db except value 1.56867.
So search results should yield:
2.0
2.0000
2
I tried using this in my SQL query but it's still returning value 1.56867.
CONVERT(decimal, myTable.Amout) = CONVERT(decimal, 2)
Can you advise?
Thanks.
You should really consider fixing your data model, and store numbers as numbers rather than strings.
That said, you need to specify a scale and a precision for the decimal, otherwise i defaults to decimal(38, 0) , which results in the decimal part being truncated. Something like:
convert(decimal(20, 5), myTable.Amout) = 2
This gives you 20 digits max, including 5 decimal digits. There is no need to explictly convert the right operand here (that's a literal number already).
If your column may contain values that are not convertible to numbers, you can use try_convert() instead of convert() to avoid errors.
The problem is that when converting the string '1.56867' to decimal, SQL server assumes you want 0 decimal places (as pointed out by #GMB), and rounds the value to the nearest integer (2).
select convert(decimal,'1.56867');
-------
2
Also, since your column is defined to hold character data, you should use the try_cast() function to avoid Error converting data type varchar to numeric. errors that would otherwise occur if non-numeric data is present in the column.
select *
from (values
('1.56867')
,('2.0')
,('2.0000')
,('2')
,(null)
,('non-numeric value')
) myTable(Amount)
where try_cast(Amount as decimal(38,19)) = 2
;
Amount
-----------------
2.0
2.0000
2
(3 rows affected)
I am trying to round my value to one decimal, below is sample which I tried.
INPUT DESIRED OUTPUT
129.700 129.7
129.769 129.8
I have tried
SELECT CAST(ROUND(('129.768'),0) AS DECIMAL(10,1))
FROM Table1
is not giving correct value.
Please help.
You want one digit after the decimal place, so that would be an argument of 1, not 0:
SELECT CAST(ROUND('129.768', 1) AS DECIMAL(10, 1))
I'm not sure why you feel the need to convert back to a decimal -- unless you are saving the value to a table and want to control the type. This also does what you want:
SELECT ROUND('129.768', 1)
I'm a rookie SQL Programmer, but have searched here and many other SQL Forums and can't figure out why my simple division script is still ignoring the decimals. I've CAST EVERYTHING as Decimal, but still don't get any in the output . .
(CAST((ABS(CAST(CAST(SUM(h4qqnb) AS DECIMAL(12,4)) - CAST(SUM(h4hdnb) AS DECIMAL(12,4))AS DECIMAL(12,4)))/CAST(SUM(h4hdnb) AS DECIMAL(12,4))) AS DECIMAL(10,2))*100)||'%' AS Count_Variance_Rate,
What am I missing?
thanks!
That's a seriously ugly expression, with far too much CASTing, but essentially what you're doing (just looking at the outermost CAST statement) is saying
CAST(someNumber as DECIMAL(10,2)
which is going to give you a number with two decimal places of precision. You're then multiplying that by 100, which is going to give you an integer.
If you're trying to get a percentage value formatted to two decimal places, you can do it like this, assuming that h4qqnb and h4hdnb are decimal fields to begin with:
concat(cast(cast(abs(sum(h4qqnb) - sum(h4hdnb)) / sum(h4hdnb) as decimal(10, 4)) * 100 as decimal(10, 2)), '%') as Count_Variance_Rate2
Working example at http://sqlfiddle.com/#!2/279752/5/0
I am using the following query.
select * from wp_rg_lead_detail where lead_id=5063 and field_number=cast(1.6 as decimal).
but it returns the field number 2 result instead of 1.6
Please let me know how can I do it?
Here:
select * from wp_rg_lead_detail where lead_id=5063 and field_number=cast(1.6 as decimal(2, 1))
You must specify number of digits in your decimal when casting, an number of digits after coma (I set them to 2 digits decimal with 1 after coma). You can easily test such casting by simply writing query:
SELECT Cast( 1.6 as decimal(2,1))
this will produce you effect of your casting. If you don't include (2,1) part, it will be automatically rounded to 2.