I am working with a table with a column 'value' with the type varchar(100).
All values in that column must be changed by multiplying them with 0.001 but my following update script fails due to "arithmetical overflow error while converting varchar to a numeric type".
update testTable
set value = cast ((value * 0.001) as varchar);
I must not change the type of the column and it holds values between 0 and 4294966796.
How do i cast correctly to get the calculation in the update working?
I tried cast (cast ((value * 0.001)) as float) as varchar) but it still throws the error.
CAST(CAST( value AS NUMERIC) *0.001 AS VARCHAR(100))
Here try this :
update testTable
set value = cast ((cast(value as float) * 0.001) as varchar);
If it still fails then one of the rows have non-numeric value
You can;
update testTable cast(cast(value as decimal) * 0.001 as varchar(32))
One way
update testTable
set value = convert(float,value) * 0.001
A simple example you can run
DECLARE #z varchar(100)
SELECT #z = CONVERT(float,'123') * 0.001
SELECT #z
0.123
If your values exceed the size of a float, then you can do the arithmetic as strings. This is a special case, because multiplying by 0.001 is just moving the decimal place over three places to the left. The following works for values greater than 1000, with or without decimal places:
update testTable
set value = (case when charindex('.', value) = 0
then left(value, len(value) - 3)+'.'+right(value, 3)
else left(value, charidnex('.', value) - 3) + '.' +
replace(right(value, len(value) - charindex('.', value) + 4), '.', '')
end)
If you have values less than 100, then you will need to prepend the values with 0s for this to work.
Related
I want to divide two integers: 8/15 and return "53.33".
I've tried every single combination, and I've found that this is the only way I can return the desired value:
select cast(100*cast(8/cast(15 as decimal(10,4)) as decimal(18,4)) as decimal(18,2))
Is there a shorter way?
Thanks.
You are doing interger division. Use float, and multiply by 100.
select 8/15.00 * 100
Or, for your precision, just do the cast once
select cast(8/15.00 * 100 as decimal (10,2))
If these are integers in a table then multiple by 1.0 and cast once
DEMO
declare #table table (int_one int, int_two int)
insert into #table
values
(8,15)
select
cast(((int_one * 1.0) / int_two) * 100 as decimal(10,2))
from #table
try this:
select (cast (8 as money)/cast(15 as money) * 100)
I have a number, e.g., 11.61. I then make the following division: 1 / 11.61.
DECLARE #value1 numeric(28, 20)
,#value2 numeric(28, 20)
SET #value1 = 11.61
SET #value2 = 1 / #value1
The results look like
(No column name) (No column name)
11.61000000000000000000 0.08613264427217915000
I then want to reverse the division: 1 / #value2
SET #value1 = 1 / #value2
The result looks like
(No column name) (No column name)
11.61000000000000079000 0.08613264427217915000
The original and expected value should be 11.61. However, as you can see the result is 11.61000000000000079000. How can this error be avoided?
use CAST
like CAST(#value1 as decimal(10,2)) and CAST(#value2 as decimal(10,2))
it will show only 2 decimal places.
I have a monetary value like 1.223.01 but it is stored as a varchar so it is really like '1.223.01'.
I need this to be converted to decimal so I can do some calculations on it.
What I have tried is
SELECT Convert(decimal(18,2), '1.223.01') AS test
But I get a conversion error.
How can I convert this?
You've not specified what the output should be, so it's either 1223.01 or 122301.00 that you require.
So using some basic string manipulation you can get both of those values:
DECLARE #val AS VARCHAR(20) = '1.223.01'
-- remove all decimal points
SET #val = REPLACE(#val, '.', '')
-- convert value without decimals to get 122301.00
SELECT CONVERT(decimal(18,2), #val) AS TestValue
-- add a decimal point before the last 2 digits to get 1223.01
SELECT CONVERT(decimal(18,2), LEFT(#val,LEN(#val) - 2) + '.' +
RIGHT(#val, 2)) AS TestValue
This assumes that the values stored always have a decimal point with 2 digits after it.
Both can be done in a single SELECT if required, by substituting REPLACE(#val, '.', '') into the position of #val in the 2 queries.
DECLARE #val AS VARCHAR(20) = '1.223.01'
SELECT CONVERT(DECIMAL(18, 2), REPLACE(#val, '.', '')) AS TestValue
SELECT CONVERT(DECIMAL(18, 2), LEFT(REPLACE(#val, '.', ''),
LEN(REPLACE(#val, '.', '')) - 2) + '.'
+ RIGHT(REPLACE(#val, '.', ''), 2)) AS TestValue
I never heard of that format. Sounds like you have a varchar and not a money type. Here is how you can manipulate it.
First removing all periods. Then dividing by 1 to explain to sql-server that this is a numeric. Then multiply by .01 to change it into a decimal number
SELECT
replace(value, '.', '')/1*.01
FROM (values('1.223.01'),('1.111.223.75'),('0.01')) x(value)
Result:
1223.01
1111223.75
0.01
I have a situation like this
I got a column with 'money' type, 2 decimal . Example data:(65.00)
I need to add 12 zero / 000000000000 to it so that the output would be like this:
(65.00 convert to 6500) + (000000000000) = 000000006500
Output: 000000006500
How can I achieve this?. Thank you for your help and suggestion
You can do this with a couple of casts, multiplying by 100, and using REPLICATE('0') to pad with the requisite number of zeroes).
I'm assuming you DO want up to 2 x trailing decimals, but no more.
DECLARE #value MONEY;
SET #value = 65.123;
DECLARE #intValue BIGINT;
SET #intValue = CAST(#value * 100.0 AS BIGINT);
SELECT REPLICATE('0',12-LEN(#intValue)) + CAST(#intValue AS NVARCHAR(20));
Returns 000000006512
If you need to do this on a set, a CTE can be used for the intermediate step, e.g.
WITH cte AS
(
SELECT CAST(MoneyField * 100.0 AS BIGINT) AS intValue
FROM SomeTable
)
SELECT
REPLICATE('0',12-LEN(cte.intValue)) + CAST(cte.intValue AS NVARCHAR(20))
FROM cte;
Fiddle here
It is Possible .But output Column should be in the type of varchar(15) .If you want to do further operation of your output you have to convert that into int or whatever
SELECT CONCAT(REPEAT('0',12-LENGTH(65.00)),(65.00*100));
I have a table called testTable with two columns, id that is auto incremented and someValue.
The data contained in the someValue column are: 12, 1.2, .4, 1d4, +, -, .
Data type for someValue is varchar(50).
Why are the following queries throwing
Error converting data type varchar to numeric.
select ID, someValue
from testTable
where ISNUMERIC(someValue + 'd0') = 1 and CAST(someValue as decimal(8,2)) > 0.1;
select tt.ID,tt.someValue
from (select ID, someValue
from testTable
where ISNUMERIC(someValue + 'd0') = 1) as tt
where CAST(tt.someValue as decimal(8,2)) > 0.1;
You have a few problems; CAST does not work on non-decimal input, and ISNUMERIC accepts strings that can convert to monetary amounts, including non-decimal values like '-', '.', or 'US$100'.
The right way to solve this is to add a Decimal column to your database, have whatever populates someValue to populate the Decimal column with the value you want to compare against, and only compare against the Decimal column.
If, for some reason, you cannot do that, you can use ISNUMERIC and only include non-monetary decimal amounts, and use Convert instead of CAST:
select ID, someValue
from testTable
where ID IN
(select ID from testTable where ISNUMERIC(someValue) = 1
AND Patindex('%[^0-9-+.]%', someValue) = 0
AND someValue NOT IN ('-', '+', '.')
)
and Convert(decimal, someValue) > 0.1
In your first statement you take directly from testTable and attempt:
CAST(someValue as decimal(8,2))
This will throw an error as you have not filtered out non-numerical values.