# Single Quotation Marks in SQL

• Michael R. OBrien Jr (1/2/2013)

I guess I have never been a fan of triple quoting, I usually use CHAR(39) I find it is a lot easier to read for others:

SELECT 'O' + CHAR(39) + 'Neal'

Just a thought, nice article though

That works and I think makes things a little cleaner, but in this example with dynamic sql you would still need to do this (replace one char(39) in O'Neil with two):

`SET @sql = 'Print ' + CHAR(39) + REPLACE(@quotedvar, CHAR(39), CHAR(39) + CHAR(39)) + CHAR(39)`

• RichB (1/3/2013)

SELECT QUOTENAME('o''neil', '''')

I looked at Quotename when I was writing this but honestly I'm still not sure exactly what it does. I tried it out in this context and couldn't get it to do what I needed it to. I may just have been using it wrong though.

Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

• I know the point you were trying to get across, but using sp_executesql is not needed to print the value of the variable. It could simply be written as follows.

DECLARE @quotedvar nvarchar(100)

SET @quotedvar = 'O''Neil'

PRINT @quotedvar

• pop022 (1/3/2013)

I know the point you were trying to get across, but using sp_executesql is not needed to print the value of the variable. It could simply be written as follows.

DECLARE @quotedvar nvarchar(100)

SET @quotedvar = 'O''Neil'

PRINT @quotedvar

Absolutely, but it did make for a simple example of the use of dynamic sql. In fact using sp_executesql is also just an example as you can use EXEC in my examples just as easily.

Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

• Kenneth.Fisher (1/3/2013)

RichB (1/3/2013)

SELECT QUOTENAME('o''neil', '''')

I looked at Quotename when I was writing this but honestly I'm still not sure exactly what it does. I tried it out in this context and couldn't get it to do what I needed it to. I may just have been using it wrong though.

I agree. Quotename is fine to use in creating a valid sql string for O'Neil, however it doesn't help you (that I can see) in creating an executable string using sp_executesql. All Quotename is trying to do is to create a valid sql string, it does not know you want the command PRINT + a valid sql string all stored as a sql string.

• Although the overall technical correctness of this article is good there is a glaring error in the narrative. There is no such thing as a "single quote" mark. There is an apostrophe mark and there is a quotation mark. A single quote looks thusly " and a double quote looks this way "". What the article’s author refers to as a "single quote" is in reality an apostrophe mark ' and what is referred to as a "double quote" is actually a quotation mark.

I bring this up because the difference is really quite significant. Calling an apostrophe a "single quote" is, putting it simply, quite wrong. It would be like calling a “V” a single “W” and calling a “W” a “double W”.

When looking up "quotation marks" in the Oxford Online Dictionary, http://oxforddictionaries.com/words/punctuation, I see no punctuation mark labeled as "single quote". Nor is there a mention of a "double quote".

Yes, I'm tilting windmills. But one must try occasionally, mustn't one?

Thanks for the soapbox.

• Nemeaux (1/3/2013)

Although the overall technical correctness of this article is good there is a glaring error in the narrative. There is no such thing as a "single quote" mark. There is an apostrophe mark and there is a quotation mark. A single quote looks thusly " and a double quote looks this way "". What the article’s author refers to as a "single quote" is in reality an apostrophe mark ' and what is referred to as a "double quote" is actually a quotation mark.

I bring this up because the difference is really quite significant. Calling an apostrophe a "single quote" is, putting it simply, quite wrong. It would be like calling a “V” a single “W” and calling a “W” a “double W”.

When looking up "quotation marks" in the Oxford Online Dictionary, http://oxforddictionaries.com/words/punctuation, I see no punctuation mark labeled as "single quote". Nor is there a mention of a "double quote".

Yes, I'm tilting windmills. But one must try occasionally, mustn't one?

Thanks for the soapbox.

Actually I appreciate the feedback. One of my biggest problems with writing, be it a blog entry, a QotD or an article like this one is that I have a tendency to use "common usage" phrases. I forget that these can be very difficult for someone who isn't fluent in English or for that matter learned it using different idioms than I'm used to.

In my own defense though you will find them described as single and double quotation marks in BOL. I checked when I was writing the article :cool:.

Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

• Ah, if only we could now get BOL to see the light!

In any case, good article...keep up the good writing!

Dave

• roger.plowman (1/3/2013)

This issue is yet another example of why SQL is one of the worst-designed languages of all time from a syntactic POV.

Would it have killed the designers to create two string delimiters that could be interchanged (ala BASIC) and reserved square brackets for field/table delimiting?

So that when you have a string which contains both delimiters as literals, you have double the problem? Syntactically, having two delimiters that mean the same thing is a problem, not a solution.

And while we're at it to use #'s to delimit dates/times (ala MS Access)?

"ala MS Access" == NO. Besides, there's plenty of good documentation on date formatting in SQL, and simply changing 's to #s wouldn't give you any better conversion rate.

Oh, and use a dedicated "escape" character instead of doubling the escaped character? Sheesh!

On this one, I agree with you -- in principle at least. From a syntax standpoint, it makes sense to have a specific escape character, like regular expressions do, for example. But what happens when you need to include the escape character as a literal? Double the escaped character, again.

In the case of T-SQL and string literals, the only character which would ever need to be escaped is '. So the options were:

a) create an escape character (let's say \) and then use that character to escape every literal ' and \ (i.e, have possible two escape clauses: \' and \\), or

b) use ' as the escape character for itself and have one possible escape clause ('').

So they again opted for the simpler solution.

• sknox (1/3/2013)

roger.plowman (1/3/2013)

This issue is yet another example of why SQL is one of the worst-designed languages of all time from a syntactic POV.

Would it have killed the designers to create two string delimiters that could be interchanged (ala BASIC) and reserved square brackets for field/table delimiting?

So that when you have a string which contains both delimiters as literals, you have double the problem? Syntactically, having two delimiters that mean the same thing is a problem, not a solution.

And while we're at it to use #'s to delimit dates/times (ala MS Access)?

"ala MS Access" == NO. Besides, there's plenty of good documentation on date formatting in SQL, and simply changing 's to #s wouldn't give you any better conversion rate.

Oh, and use a dedicated "escape" character instead of doubling the escaped character? Sheesh!

On this one, I agree with you -- in principle at least. From a syntax standpoint, it makes sense to have a specific escape character, like regular expressions do, for example. But what happens when you need to include the escape character as a literal? Double the escaped character, again.

In the case of T-SQL and string literals, the only character which would ever need to be escaped is '. So the options were:

a) create an escape character (let's say \) and then use that character to escape every literal ' and \ (i.e, have possible two escape clauses: \' and \\), or

b) use ' as the escape character for itself and have one possible escape clause ('').

So they again opted for the simpler solution.

I do see one benefit for using an escape character that isn't your delimiter. '\\' is quite a bit easier to follow than ''''. Or for that mater '\\\\' is much easier than ''''''. On the whole though I agree with you. A character had to be picked as an escape character and regardless of what character it was we were going to have some level of problems with it.

Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

• nice article. thanks.

DECLARE @topsql NVARCHAR(200)

SET @topsql = 'DECLARE @quotedvar nvarchar(100) ' + CHAR(13)

+ 'DECLARE @sql nvarchar(1000) ' + CHAR(13)

+ 'SET @quotedvar = ''O''''Neil''' + CHAR(13)

+ 'SET @sql = ''PRINT '''''' +REPLACE(@quotedvar,'''''''','''''''''''') + '''''''''

+ CHAR(13) + 'PRINT @sql ' + CHAR(13) + 'EXEC sp_executesql @sql '

PRINT @topsql

PRINT '-------'

EXEC sp_executesql @topsql

declare @q nvarchar(50), @sql nvarchar(100)

set @q = '''o''''neil'''

set @sql = 'print ' + @q + ''

print @sql

EXEC sp_executesql @sql

___________________________________

print 'o''neil'

o'neil

• Nemeaux (1/3/2013)

Although the overall technical correctness of this article is good there is a glaring error in the narrative. There is no such thing as a "single quote" mark. There is an apostrophe mark and there is a quotation mark. A single quote looks thusly " and a double quote looks this way "". What the article’s author refers to as a "single quote" is in reality an apostrophe mark ' and what is referred to as a "double quote" is actually a quotation mark.

I bring this up because the difference is really quite significant. Calling an apostrophe a "single quote" is, putting it simply, quite wrong. It would be like calling a “V” a single “W” and calling a “W” a “double W”.

When looking up "quotation marks" in the Oxford Online Dictionary, http://oxforddictionaries.com/words/punctuation, I see no punctuation mark labeled as "single quote". Nor is there a mention of a "double quote".

Yes, I'm tilting windmills. But one must try occasionally, mustn't one?

Thanks for the soapbox.

http://english.stackexchange.com/questions/36046/apostrophe-vs-single-quote

Also here:

http://en.wikipedia.org/wiki/Quotation_mark

SQL uses what seems to be called typewriter quotation marks, or dumb or straight quotes. I think there is a semantic difference between apostrophes and single quotation marks. For example, many Penguin books use single quotation marks to set off dialogue. In that case, though, the "open quote" is in one direction and the "close quote" is in the inverted direction. So although you may be technically correct (I'm not yet sure that you are), it seems to be the case -- excepting the symmetrical straight or dumb quotes -- that while all apostrophes look like single (close) quotation marks, not all single quotation marks look like apostrophes. See also:

http://www.ergonis.com/products/tips/punctuation-apostrophes-quotation-marks.php

That typographical distinction doesn't exist in SQL, which uses a "dumb" quotation mark (for lack of a better term) rather than a "smart" one. So you may indeed be titling at windmills, although I happen to find those same windmills very interesting and am grateful that you contributed your comments to this discussion.

- webrunner

-------------------
A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

• At the very least the WWW consortium does not agree that there is no such thing.

‘ is left single quote

’ is right single quote

--EDIT--

Proof...I typed in the html escape sequence and they rendered on the page.

& lsquo;

& rsquo;

_______________________________________________________________

Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

• The standard ASCII character set is a holdover from the early days of teletype, band printers, computers and manual typewriters. (How many of you have ever seen a band printer? They were incredibly fast.)

If one wants to get nitpicky, typographers have always used the form of characters that was added to the standard ASCII to create the Extended Character set. Even in the days of lead type, most font sets--whether manual or automated linotype--included these typographical characters.

[font="Times New Roman"]

From the standard character set:

This is an apostrophe: ' (ASCII 39)

This is a quotation mark: " (ASCII 34)

From the extended characters set:

This is a left single quotation mark: ‘ (ASCII 145)

This is a right single quotation mark: ’ (ASCII 146)

This is a left double quotation mark: “ (ASCII 147)

This is a right double quotation mark: ” (ASCII 148)

[/font]

ASCII 39 and ASCII 34--in typography--would be used to represent minutes and seconds when displaying latitude or longitude or as an abbreviation for feet and inches.

Oh, and since I used the double-hyphen above rather than a true em dash that is cause to differentiate there as well.

[font="Times New Roman"]

This is a hyphen: - (ASCII 45) used as a minus symbol, for compound words, or to break a word between lines.

This is an en dash: – (ASCII 150) used to represent a range such as 1952–1987.

This is an em dash: — (ASCII 151) used as a text separator. This symbol would properly be used in place of the double-hyphen—like this—rather than the double-hyphen typical of computers and typewriters.

And one final typographical note: the ellipsis is typically typed using three periods. But the proper method to display an ellipsis would be to use ASCII char 133: …

[/font]

For anyone who really cares about proper typography The Chicago Manual of Style is the editor's Bible.

Viewing 15 posts - 16 through 30 (of 73 total)