The following
Microsoft SQL Server T-SQL code samples demonstrate the usage
of double apostrophes/single quotes and CHAR(39) to represent an apostrophe inside a string which is enclosed in single quotes. Apostrophe is the same as single quote.
-- Double up apostrophes/single quotes or use char(39) in concatenation
SELECT * FROM Northwind.dbo.Products
WHERE ProductName='Chef Anton''s Gumbo Mix'
------------
SELECT * FROM Northwind.dbo.Products
WHERE ProductName='Chef Anton'+CHAR(39)+'s Gumbo Mix'
------------
-- Place two apostrophes in the string to replace a single apostrophe
SELECT 'That''s the right amount on the John Deere tractor''s invoice.'
-- That's the right amount on the John Deere tractor's invoice.
GO
-- Using local string variable
DECLARE @string varchar(256) =
'That''s the right amount on the John Deere tractor''s invoice.'
SELECT @string
-- That's the right amount on the John Deere tractor's invoice.
GO
-- Using char(39) - single quote - concatenation
DECLARE @string varchar(256) =
'That'+CHAR(39)+'s the right amount on the John Deere tractor'+CHAR(39)+'s invoice.'
SELECT @string
-- That's the right amount on the John Deere tractor's invoice.
|