|
Hello all I'm sure I will be laughed out of here but I cant seem to figure out how to do this I simply want to ad a (' the the begining of a returned query and add a ') to the end i would like to do something like this select (' + tagname + ') from dbo.tags the problem is with the single quotation. I'm almost to the point of creating a table that has this punctuation Any Ideas
(comments are locked)
|
|
No you won't be laughed out of here - it's a very good question! try The basic rule is if you want a single quote inside a string, put two of them. This can in some situations lead to a whole lot of single quotes.
Apr 22 '10 at 12:56 PM
TimothyAWiseman
that worked perfectly thank you
Apr 22 '10 at 01:00 PM
Clint
This is great information and will be adding it to my notes thank you!
Apr 26 '10 at 11:50 AM
Clint
(comments are locked)
|
|
Sorry I'm late to the party but let's not forget the function actually built to accomplish such a thing... Quotename is your friend if building dynamic SQL.
Apr 24 '10 at 02:11 AM
TimothyAWiseman
This is great information and will be adding it to my notes thank you!
Apr 26 '10 at 11:49 AM
Clint
You bet. Thanks for the feedback folks.
May 02 '10 at 11:06 PM
Jeff Moden
(comments are locked)
|
|
Tim already posted a perfect answer where he mentioned that this technique "can lead to a whole bunch of single quotes". The good news is that because the keywords such as select, from etc come in blue color in the SSMS editor, you can immediately see that some quotes are missing in the bunch (when you don't see expected blue color). I personally prefer the bunch of single quotes approach, but if they are not comfortable then there is another technique to simply instruct the engine to print the single quote when needed by specifying its code. For example, in your case, the statement will look like this:
This will lead to the same result but the inclusion of quotes is easier to see. Oleg Oleg, the colour coding is true until you find a need for dynamic SQL and then the "count the quote" game goes to a whole new level!!
Apr 24 '10 at 04:06 AM
Fatherjack ♦♦
This is great information and will be adding it to my notes thank you!
Apr 26 '10 at 11:50 AM
Clint
(comments are locked)
|


It's a great question and one that causes no end of pain. As Kev showed, two single quotes gets read as one when they're inside another pair of single quotes. That can get really confusing, really fast, so this is an excellent question. Thanks for posting it.
Its a house rule - no laughing at sensible questions.