Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion failed when converting the nvarchar to int

I have a field which is varchar and contains numbers and dates as strings. I want to update all numbers in this field that is greater than 720. I have attempted firstly to do a select but I get this error:

Conversion failed when converting the nvarchar value '16:00' to data type int.

This is my query:

select id, case(isnumeric([other08])) when 1 then [other08] else 0 end
from CER where sourcecode like 'ANE%' --and other08 > 720

It fails when I uncomment the last part.

I am trying to get all numerics greater than 720, but I can't do the comaprison. It also fails when casting and converting.

Thanks all for any help

like image 866
Abs Avatar asked Jan 24 '26 06:01

Abs


1 Answers

You also need to perform the checks and conversion in the WHERE clause:

SELECT 
       id, 
       CASE WHEN isnumeric([other08]) = 1 THEN CAST([other08] AS INT) ELSE 0 END
FROM   CER 
WHERE  sourcecode LIKE 'ANE%' 
AND CASE WHEN isnumeric([other08]) = 1 THEN CAST([other08] AS INT) ELSE 0 END > 720
like image 198
Winston Smith Avatar answered Jan 26 '26 20:01

Winston Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!