I have simple code. I need make easy operation: "column_A * 100 ", but I can't convert varchar type to int. My error is: Conversion failed when converting the varchar value '0.27' to data type int.
SELECT POWIERZCHNIA,
POWIERZCHNIA * 100
FROM v_analiza_cechy_produktow
--Conversion failed when converting the varchar value '0,27' to data type int.
SELECT POWIERZCHNIA,
CAST (REPLACE(POWIERZCHNIA, ',' , '.') AS int)
FROM v_analiza_cechy_produktow
--Conversion failed when converting the varchar value '0.27' to data type int.
I want convert this values to int, but how?
@Larnu
SELECT POWIERZCHNIA FROM v_analiza_cechy_produktow
WHERE TRY_CONVERT(decimal(10,4),POWIERZCHNIA) IS NULL;
TOP 20 rows:
NULL 1,08 0,21 0,85 1,38 0,00 2,88 3,00 2,40 1,00 1,36 0,30 2,16 3,24 NULL NULL NULL 2,88
You can't convert a
varcharrepresentation of a decimal directly to anint(SELECT CONVERT(int, '0.27');will fail with the same error). You need toCONVERTto value first to adecimaland then anint. So, for your query:Note that I have used
decimal(3,2)as we only have one sample value'0.27'. you will likely need to choose a different scale and precision.This does, however, ask the question; why are you storing decimal values as a
varchar?