Using SQL 2014 I need to append a negative sign to a list of numeric values. The data are dollar amounts with numerous places behind the decimal point. I did convert the data to numeric(15, 2)
Here is my select statement.
SELECT '-' + convert(15,2), MONEY from TABLE
I am getting the error: Arithmetic overflow error converting varchar to data type numeric.
I tried converting to varchar as well.
select '-' + CONVERT(varchar10), (convert(numeric(15, 2), MONEY)) from
TABLE
I get the same error as above. Any ideas how to accomplish this?
How about multiplying by -1 instead? Something like this:
SELECT -1 * convert(MONEY, 15.2) from TABLE
Your syntax isn't correct. I am guessing you want something like the above.
Or subtract from 0.
SELECT -convert(MONEY, 15.2) from TABLE
why not trying Cast Function
SELECT -1 * CAST(MONEY as Numeric(15.2)) from TABLE
Related
I have my date which looks something like this.
I want to get sum of ITEM_QTY which has varchar datatype currently and tried following ways after typecasting but none of them worked.
SELECT SUM(TRY_CAST(ITEM_QTY as bigint))
FROM sales219
Result:
SELECT SUM(CAST(ITEM_QTY as bigint))
FROM sales219
Output:
Error converting data type varchar to bigint.
SELECT SUM(convert(bigint,ITEM_QTY))
FROM sales219
Output:
Error converting data type varchar to bigint.
SELECT SUM(try_convert(bigint,ITEM_QTY))
FROM sales219
Output:
Any suggestion is appreciated.
Thanks!
You will want to remove the double quotes from your data. In the mean time try this:
Select sum(try_convert(bigint,replace([ITEM_QTY],'"',''))) from YourTable
Or rather, fix your importing process to stop putting the quotes there to begin with.
I'm trying to calculate the sum of two separate fields with the query
select sum(percent12 + percent21) as total
from finalquery
but I keep getting this error:
Msg 8117, Level 16, State 1, Line 535
Operand data type varchar is invalid for sum operator.
However, if I do:
select percent12 + percent21 as total
from finalquery
I get:
(total)
50.0040.00
25.0025.00
100.0 0.00
100.0 0.00
100.0 0.00
How can I fix this?
Both of your percentage columns are a varchar, the + operator can concatenate strings together, and that is what you're seeing.
I would suggest that you use a CAST to a NUMERIC data type, especially as it seems that you have a fixed amount of spaces after the decimal.
Try the following:
SELECT
SUM(CAST(percent12 AS NUMERIC(10,2))+ CAST(percent21 AS NUMERIC(10,2))) AS total
FROM finalquery
Others have suggested that you CAST to a FLOAT, though that has known rounding errors (and known workarounds)
Float rounding error: SQL Server rounding Error, Giving different values
It seems that the values you are trying to sum are varchar/string type and therefore they cannot be summed up.
Try convert them to integer or float (depending of the type) before summing
ie at SQL Server Select sum(convert(float,x1) + convert(float,x2) )
When you are using the + operator in your second query, you are actually concatenating two strings. You know they are strings because the error message tells you it's a varchar (i.e. a string).
Instead, you need to convert each field into a number data type and then wrap those in your aggregation. In this case, I chose to cast it as a decimal data type based off of your sample data, but you can cast as integer or other numeric data type as well.
SELECT SUM(CAST(percent12 AS DECIMAL(10, 4)) + CAST(percent21 AS DECIMAL(10, 4))) AS total
FROM finalquery
Of course, the obvious answer is to stop storing numbers as strings. If you can modify your database schema, seriously look into changing that.
Fix your schema and you will solve this problem and prevent many others in the future. You should choose the appropriate datatype for each column - don't just blindly select some type of string because it's "easier".
I am altering a view to cast geographic coordinates (numbers with varying decimal precision) into a decimal field.
I have confirmed that the only items in the source tables are numbers and decimals -- but I am getting the error
"Error converting data type varchar to numeric."
Is it possible the decimal in the source table is being read as a character, and if so, what could I do to successfully execute this conversion?
ALTER VIEW [SCHEMA].[VIEW_V]
AS SELECT
cast(field 1 AS decimal (26,19)) as x_coord
FROM [linkedServer].[Sourcedatabase].[schema].[dt_table]
Trying using isnumeric() or try_convert():
ALTER VIEW [SCHEMA].[VIEW_V] AS
SELECT (CASE WHEN isnumeric(field1) = 1
THEN cast(field1 AS decimal(26,19))
END)as x_coord
FROM [linkedServer].[Sourcedatabase].[schema].[dt_table];
or
ALTER VIEW [SCHEMA].[VIEW_V] AS
SELECT try_convert(decimal(26, 19), field1)
FROM [linkedServer].[Sourcedatabase].[schema].[dt_table];
Where the resulting value is NULL, you will know which rows are causing the problems.
In case of float values with characters 'e' '+' it errors out if we try to convert in decimal. ('2.81104e+006'). It still pass ISNUMERIC test.
SELECT ISNUMERIC('2.81104e+006') returns 1.
SELECT convert(decimal(15,2), '2.81104e+006') returns error: Error converting data type varchar to numeric.
SELECT try_convert(decimal(15,2), '2.81104e+006') returns NULL.
SELECT convert(float, '2.81104e+006') it returns correct value 2811040.
I have a column in varchar format and I would like to convert it to float. The values are like:
188234,62
188235
188235,43
188235,88
So, When I try to convert it I get an error:
Error converting data type varchar to float.
I thought that maybe some fields are not numeric and I checked with isnumeric() function, according to the query I concluded that all the values are numeric. Then I tried different queries to convert the field types:
Query1:
SELECT CarData.dbo.FinalData_OilLevel_new_send.KM
CASE WHEN ISNUMERIC(CarData.dbo.FinalData_OilLevel_new_send.KM) = 1
THEN CAST(CarData.dbo.FinalData_OilLevel_new_send.KM AS FLOAT)
ELSE NULL
END
FROM CarData.dbo.FinalData_OilLevel_new_send
Query2:
SELECT CASE ISNUMERIC(KM) WHEN 1 THEN CONVERT(float, KM)) ELSE null END from CarData.dbo.FinalData_OilLevel_new_send
However I always get the same error. Are there some other ways to convert varchar to float?
You have to use a dot as decimal separator:
CAST(REPLACE(CarData.dbo.FinalData_OilLevel_new_send.KM,',','.') AS FLOAT)
At first you need to Replace
The , present
try like this!
select cast(replace(val,',','') as float) from table
SEE DEMO
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