Jr. Developer asked me other day how to escape single quote?
User can escape single quote using two single quotes (NOT double quote).
Following T-SQL Script will give error
USE AdventureWorks GO SELECT * FROM Person.Address WHERE City = 'Villeneuve-d'Ascq' GO
Resultset:
Error: 105 Unclosed quotation mark after the character string ‘
To fix the error mentioned above, escape single quotes with two single quotes as displayed in following example.
USE AdventureWorks
GO
SELECT *
FROM Person.Address
WHERE City = 'Villeneuve-d''Ascq'
GO
UPDATE : A very good SQL blog Author Madhivanan has written similar article but with good example. Recommended read.






@ENTITY_NAME NVARCHAR(100)
IF @ENTITY_NAME = ‘%’
BEGIN
SELECT @WHERE = @WHERE + ‘ AND (T.ENTITY_ID LIKE ”’ + @ENTITY_NAME + ”’ OR T.ENTITY_ID IS NULL)’
END
(If @ENTITY_NAME value is ‘ramarao’ in this I did not get any problem. But, ‘ramarao’s ‘ I am getting error like Unclosed quotation mark after the character string ‘). Can you please help out me by giving proper code (how to replace two single quotes in place of one single quote).
Thanks in Advance.
Thanks,
B. Rama Rao
write a simple example for me, which meets my requirement.