How to Fix Varchar to Int Conversion Error in SQL Server

alter-tablesql servertype conversionvarchar

I came across an issue today, and while I was able to fix it, I am unsure what caused the issue to happen.

I have a column that is Varchar(20). It is used to store just year data (2010, 2011, etc.) it is being converted to an Int data type, but when the command below is ran, an error code happens.

ALTER TABLE mytable ALTER COLUMN1 int NULL

The error displayed is: Conversion failed when converting the varchar value '2010. ' to data type int.

There are 3 rows in the table with the value of 2010 in Column1, but NO rows with '2010. ' (period and 10 spaces) as a value. I ran

select len(Column1) from MyTable where Column1 like '%2010%'

I got 2 rows with a length of 4, and 1 with a length of 5. After updating the row with 5 length to have 2010 as the value, the table could be altered.

I am curious where the period and spaces came from here. Also curious how 1 character length increase results in a period and 10 spaces. I am thinking something along the lines of a carriage return or line feed, but I can't find any data to back that up, its just a hunch.

Best Answer

You'd need to determine exactly what character was in the original string. Hopefully you have a copy of the original somewhere. I'm guessing it wasn't actually a full stop even if it displayed as one. This query may help

declare @s table
(
    col varchar(20) not null
);

insert @s(col) values ('2010'), ('2010.   ');

;with Numbers as
(
    select top(20)
        rn = ROW_NUMBER() over (order by (select 1))
    from sys.objects as o
)
select
    s.col,
    n.rn,
    SUBSTRING(s.col, n.rn, 1),
    ASCII(SUBSTRING(s.col, n.rn, 1)),
    UNICODE(SUBSTRING(s.col, n.rn, 1))
from @s as s
cross apply Numbers as n
where n.rn < = DATALENGTH(s.col)
and s.col like '%2010%'
order by
    s.col,
    n.rn;

The CTE is just a quick numbers table statically sized to the column's declared width. It will return one row per possible character position in the input data.

The main query uses the CTE's output as an ordinal index to extract one character at a time.