Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error converting data type bigint to varchar.

DECLARE @ID BIGINT
set @ID = 1323
UPDATE School 
SET RegistrationFee = 'fee_' + @ID --<<<<error 
Where SchoolRegistrationId = 123

Error converting data type varchar to bigint.

like image 344
Nick Kahn Avatar asked Mar 01 '11 19:03

Nick Kahn


People also ask

What is Bigint in SQL?

A big integer is a binary integer with a precision of 63 bits. The range of big integers is -9223372036854775808 to +9223372036854775807.

What is varchar SQL?

VARCHAR(size) A VARIABLE length string (can contain letters, numbers, and special characters). The size parameter specifies the maximum string length in characters - can be from 0 to 65535. BINARY(size) Equal to CHAR(), but stores binary byte strings.

Is numeric SQL Server?

SQL Server ISNUMERIC() FunctionThe ISNUMERIC() function tests whether an expression is numeric. This function returns 1 if the expression is numeric, otherwise it returns 0.


1 Answers

SQL Server automatically converts the data from one data type to another. This is Implicit conversion. Your script has a process with diffirent types (varchar and bigint). Bigint is an exact numeric type. "Character expressions that are being converted to an exact numeric data type must consist of digits, a decimal point, and an optional plus (+) or minus (-). Leading blanks are ignored. Comma separators, such as the thousands separator in 123,456.00, are not allowed in the string." (see. https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-type-conversion-database-engine link.)

You should use explicit convertion to force It doesn't occur implicit convertion automaticly. Such as CAST function.

Choose one depend on RegistrationFee column data type in following expressions.

'fee_' + CAST(@ID AS NVARCHAR(25)) 
'fee_' + CAST(@ID AS VARCHAR(25)) 
'fee_' + CAST(@ID AS CHAR(25)) 
'fee_' + CAST(@ID AS NCHAR(25)) 

-- Bigint max value is '9,223,372,036,854,775,807' that has 25 chars.

like image 86
Başar Kaya Avatar answered Sep 20 '22 15:09

Başar Kaya