Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLServer function for single quotes

Tags:

sql

tsql

I need to wrap a few strings in single quotes for a dynamic TSQL statement in a stored procedure. I am absolutely certain that no single quote values will be passed(these fields are not "editable" at the application level, only selectable) hence my requirements are pretty mild in this aspect.

The solution I came up with is simple but nice:

declare @SingleQuote nvarchar(1)

select @SingleQuote = ''''

Then use it all over the place :-)

It would be helpful, however, if there was a better way, i.e. an SQL function just like the newid() one to generate a new GUID.

I would then just need to do something like:

select.....SingleQuotes(MyField)....

Any suggestion?

Thank you for your time reading this,

Andrew

like image 926
Andrea Raimondi Avatar asked Jan 23 '09 10:01

Andrea Raimondi


People also ask

How do I select a single quote in SQL Server?

The simplest method to escape single quotes in SQL is to use two single quotes. For example, if you wanted to show the value O'Reilly, you would use two quotes in the middle instead of one. The single quote is the escape character in Oracle, SQL Server, MySQL, and PostgreSQL.

Can you use single quotes in SQL?

Single quotes are used to indicate the beginning and end of a string in SQL. Double quotes generally aren't used in SQL, but that can vary from database to database. Stick to using single quotes. That's the primary use anyway.


1 Answers

select QUOTENAME(FieldName, CHAR(39))
like image 142
cmsjr Avatar answered Sep 20 '22 01:09

cmsjr