Skip to content
Advertisement

Conversion failed when converting the varchar value of string to data type int

I got issue with my SQL as below statement. After execute it, got error :

Conversion failed when converting the varchar value ‘ONWARDS’ to data type int.

SELECT description, moreThanDay, 
CONCAT(moreThanDay+1,' - ' , 
  IIF(LEAD(moreThanDay) OVER (ORDER BY moreThanDay) > 0, 
    LEAD(moreThanDay) OVER (ORDER BY moreThanDay),'ONWARDS')) AS rangeDay 
FROM EquipmentOwnerDetention WHERE equipmentOwnerCode=@equipmentOwnerCode 

Is anyone know on how to cast so that the result will turn out as string ? Thanks in advances.

Advertisement

Answer

The second parameter in IIF is int and ‘ONWARDS’ is string. IIF expects both to be of same data type. Try converting the first parameter to string and it may work.

Something like this

SELECT description, moreThanDay, 
CONCAT(moreThanDay+1,' - ' , 
  IIF(LEAD(moreThanDay) OVER (ORDER BY moreThanDay) > 0, 
    STR(LEAD(moreThanDay) OVER (ORDER BY moreThanDay), 10),'ONWARDS')) AS rangeDay 
FROM EquipmentOwnerDetention WHERE equipmentOwnerCode=@equipmentOwnerCode 
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement