Sqlserver
 SQL >> Database >  >> RDS >> Sqlserver

"The Conversion of the Varchar Value Overflowed an Int Column" — usa BIGINT

Declare @phoneNumber int select @phoneNumber=Isnull('08041159620',0); 

Che va in errore con:

The conversion of the varchar value '8041159620' overflowed an int column.: select cast('8041159620' as int) 

Perché: un int è un numero intero a 4 byte che spazia da -2^31 (-2.147.483.648) a 2^31 - 1 (2.147.483.647). Il valore 8.041.159.620 è ben oltre quel tetto. Il sinonimo SQL-92 di int è integer.

Soluzione:

Declare @phoneNumber bigint 

Riferimento

Separatamente, memorizzare un numero di telefono in un tipo numerico merita una ripensata. Lo zero iniziale nel tuo letterale viene già scartato, e i numeri non possono contenere estensioni, il segno più dei prefissi internazionali, o spazi — varchar è la casa giusta per loro.