Skip to content
Advertisement

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

I’m getting an “error converting varchar to int” for fields Q1_c and Q2_c.

Those fields are CHAR(1) and the values for those fields are either 1,2,3,4,5, or U, but it will not convert when there is a U, so I am trying to change the value of U to something else like 0. I have also tried CAST(a.q2_c as varchar(1)) = 'U' – but that doesn’t work either. Should I be using INT or VARCHAR()? I have also tried using CONVERT instead of CAST.

Advertisement

Answer

You can go about this in a couple of different ways:

Since the fields are in character format, replace ‘u’ with ‘0’ and then convert it into int:

Or if you wanted to create a new calculated field you can directly create an integer field:

Hope this helps.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement