Update String Columns without Using Single Quotations - General Question
-
05-10-2019 - |
Question
UPDATE CustomerPhone
SET PhoneTypeID = 7, PhoneNumber = 999-444
WHERE CustomerID = 500 AND PhoneNumber = 9-1-1;
PhoneNumber is of type varchar(20) whereas PhoneTypeID and CustomerID are of type int. I'm running the above statement in SQL Server, it works fine.
I wonder how come it works? I thought any string value has to be put between '...'
Solution
SQL Server will CAST
or CONVERT
the value that you pass it, when you specify a value that isn't of type that you expect.
i.e. Try:
SELECT * FROM CustomerPhone WHERE PhoneTypeID = '7'
Here SQL Server will take your string '7'
and try to convert to the appropriate type of int, smallint, tinyint (whatever appropriate).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow