How to assign a text containing a single quote to a char, varchar, nchar or nvarchar valiable or colum
Sometimes the need to assign a text like "She's the boss" to a text column or variable arises.
DECLARE @MyText AS NVARCHAR(100) = 'She's the boss';
You can already see on the syntax coloring that something is wrong. To put a single quote inside a string literal you must precede it with another single quote
DECLARE @MyText AS NVARCHAR(100) = 'She''s the boss';
If you run the whole batch below
DECLARE @MyText AS NVARCHAR(100) = 'She''s the boss';
SELECT @MyText;
You will see it produces the desired result: She's the boss
To give you another example the text ‘I am between single quotes’ would look like this
DECLARE @MyText AS NVARCHAR(100) = '''I am between single quotes''';
SELECT @MyText;
Print article | This entry was posted by =tg= on 2011-01-27 at 13:12:00 . Follow any responses to this post through RSS 2.0. |