feetwet feetwet - 1 year ago 135
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

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

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download