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
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.
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
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 two columns of data in a SQL Server 2008 table that I need to convert from varchar(50) over to decimal. Get different errors depending on the methods I try to use.
Error converting data type varchar to numeric.
Arithmetic overflow
Some ways I have tried:
CONVERT(DECIMAL(38,10),REPLACE(REPLACE(REPLACE(RTRIM(LTRIM(value)),',',''),' ',''),'-',''))
CASE
WHEN value = 1
THEN CONVERT(numeric(14,2), value)
ELSE 'NA'
END
Here is a sample of the data in the table that I am trying to convert:
271.5
14.95
352.5
150
37.5
20.5
300
90
This shouldn't be this hard...
CAST should work well as long as the data is clean and the range is sufficient.
If you're not sure that all values are actually numeric, you can scrub them before converting them;
SELECT value, CASE WHEN ISNUMERIC(value)=1
THEN CAST(value AS NUMERIC(10,2))
END value2
FROM table1
This will convert the string value to a NUMERIC if it is actually convertible, and otherwise give the value NULL.
An SQLfiddle to test with.
I am trying to execute the following Query
select distinct pincode as Pincode,CAST(Date_val as DATE) as Date,
SUM(cast(megh_38 as int)) as 'Postage Realized in Cash',
SUM(cast(megh_39 as int)) as 'MO Commission',
from dbo.arrow_dtp_upg
group by pincode,Date_Val
but I am getting an error "Conversion failed when converting the nvarchar value '82.25' to data type int."
Am I using a wrong data type?
The string "82.25" represents a float (or a decimal) not an int, so use cast(megh_38 as float) if you need a float.
If you require the integer part only then use floor(cast(megh_38 as float)).
You could try this:
cast(megh_38 as decimal(10,2))
cast(megh_39 as decimal(10,2))
One possible problem is delimiter (dot vs comma).
Questions - why you prefer nvarchar for store float or int values? Maybe decimal or float is better solution?