Error converting data type varchar to float isnumeric = 1 - sql

When I run the script:
select
cast(s as float)
from
t
where
ISNUMERIC(s) = 1
it stops with the error:
Error converting data type varchar to float.
Why does it happen? I'm trying to convert to float only numerics. How do I found out which row causes the error?

The isnumeric function thinks just about everything is a number. Use "try_convert" instead. if the value can't convert to your destination datatype, it returns null.
select convert(float, '1,0,1')
where try_convert(float, '1,0,1') is not null
If you are on an older version of SQL, I would write my own function.

I usually face with this when the value in a column you are trying to convert to float contains a comma (,) as thousand separator:
SELECT ISNUMERIC('140,523.86')
The Result is 1, but unable to cast it as a float.
By replacing it works fine for me:
SELECT
CAST( replace(s,',','') AS float ) AS Result
FROM t
WHERE ISNUMERIC(replace(s,',','')) = 1

ISNUMERIC() function will return 1 for values like 123e3 because these values are Interpreted as numeric values. Because sql server sees this as 123 , 3 to the powers of 10 which is really a numeric value.
You should try something like....
Select *
From tableName
WHERE Col NOT LIKE '%[^0-9]%'
This will return any row where there is a non-numeric character, even values with a ..

Related

How to multiply string value to longint in SQL

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;

SQL Server: Error converting data type varchar to float

I have the following SELECT statement to calculate RADIANS and COS.
SELECT COS(RADIANS(latitude)) as Lat
FROM tbl_geometry;
But I'm getting an error:
Error converting data type varchar to float.
My attempts:
Attempt #1:
select Cos(convert(float, (Radians(convert(float, latitude))))) as Lat
from tbl_geometry;
Attempt #2.
select Cos(Radians(convert(float, latitude))) as Lat
from tbl_geometry;
Both attempts result in the same error.
Note: column Latitude is of type varchar.
Use try_convert() to find the invalid data:
select latitude
from tbl_geometry
where try_convert(float, latitude) is null;
try_convert() is available in SQL Server 2012+.
You can use:
SELECT CASE
WHEN PATINDEX('%[^0-9]%', latitude) > 0 THEN 0
ELSE Cos(Radians(convert(float,latitude)))
end as latitude
FROM tbl_geometry
You can use the THEN to fill the attribute with null or other values if non-convertable values are encountered. Or you can use the PATINDEX in the WHERE if you want to skip those rows all together. (You may have to play around with the patindex, I don't usually use floats, so I'm not sure what is and isn't allowed. You may want to include decimal points, for example.)

SQL: Unable to CAST a query

SELECT CAST ((SUM(r.SalesVolume)/1000) AS decimal(3,3)) FROM RawData r
The above is a part of a query that I am trying to run but returns an error:
Lookup Error - SQL Server Database Error: Arithmetic overflow error converting int to data type numeric.
Not sure what this means.
The result column looks like(Without dividing by 1000 and casting):
Total_Sales_Volume
64146
69814
68259
56318
66585
51158
44365
49855
49553
88998
102739
55713
Tried casting as float but doesnt help.
The Problem is decimal(3,3) --> this means a number with 3 digit, 3 of them behind the decimal point. If you want a number like this 1234567.123 you would have do declare it as decimal(10,3)
Try this:
SELECT CAST ((SUM(r.SalesVolume)/1000.0) AS decimal(6,3)) FROM RawData r
decimal(3,3) means that you allow numbers with 3 digits in total, and 3 of these are behind the comma ... I think you meant decimal(6,3)
EDIT: In addition, you need to to divide by 1000.0, not by 1000.
If you divide by 1000, it is an integer division.
If you divide by 1000.0, then it becomes a decimal division, with commas.
Try following:
SELECT CAST ((SUM(r.SalesVolume)/1000) AS numeric(6,3)) FROM RawData r

how to convert different datatypes to int in sql

I have a nvarchar(200) column in a table that contains a mix of integers (as strings) and non-integer strings and symbols also. E.g. Some sample data :-
Excuse me for my typing in my initial post I mentioned varchar(200) but in fact it is 'nvarchar(200)'
02
0
.../
125
00125
/2009
1000
0002589
000.00125
Marathi numbers like & letters
how can I order this Column?
You can use CAST to convert a varchar to an INT given that varchar is holding a proper number.
SELECT CAST(varCharCol as Int)
E.g.
col1 as Varchar(10)
col1 = '100' converting to INT will be successufl
but if col1 = '100 xyz' will be unsucessful in the process.
Looking at your string you may have to use number of LTRIM, REPLACE to get hold of the digits or using a regex to get comma separated numbers. That too it's not very clear how your original string looks like.
References.
Many DBMS have CAST() functions where you can convert one datatype to another.
For MySQL have a look at this site
You can Use CAST and Convert to convert string type value to int type. but be sure the value should numeric.
select convert(int,'123')
select CAST('123' as int)
You can use this query
SELECT CASE
WHEN ISNUMERIC(colName)=1 THEN ROUND(colName, 0)
ELSE 0 END AS [colName]
FROM tblName

SQL Server CONVERT(NUMERIC(18,0), '') fails but CONVERT(INT, '') succeeds?

PRINT CONVERT(NUMERIC(18,0), '')
produces Error converting data type varchar to numeric.
However,
PRINT CONVERT(INT, '')
produces 0 without error...
Question: Is there some SQL Server flag for this or will I need to do case statements for every varchar to numeric conversion? (aside from the obvious why?)
Use ISNUMERIC
declare #a varchar(20)
set #a = 'notanumber'
select case when isnumeric(#a) = 0 then 0 else convert(numeric(18,0),#a) end
ISNUMERIC doesn't alway work as you might expect: in particular it returns True for some values that can't subsequently be converted to numeric.
This article describes the issue and suggests how to work around it with UDFs.
Empty string will convert to zero for float and int types, but not decimal. (And converts to 01 Jan 1900 for datetimes = zero). I don't know why.. it just is...
If you need decimal(18,0), use bigint instead. Or cast via float first
ISNUMERIC will accept - and . and 1.2E3 as a number, but all fail to convert to decimal.