feetwet feetwet - 4 months ago 13
SQL Question

Why does MS SQL CAST as bigint result in int overflow?

Why does the following T-SQL statement:

select CAST(COALESCE('3537601039',0) as bigint)


result in the following error when run against MS SQL Server 10?


The conversion of the varchar value '3537601039' overflowed an int
column.


Is there a "more correct" way to do this conversion of a
varchar
to a
bigint
that would avoid this problem when the number is within range of a
bigint
?

Answer

The first thing that is happening is that your string is trying to convert to a regular int to match the 0. Try this:

select CAST(COALESCE('3537601039','0') as bigint)
Comments