Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Single quote in NVARCHAR string?
How do I get a single quote (') in a NVARCHAR string in MS SQL Server?

e.g. SELECT @strsql = "SELECT * FROM tblTest WHERE Field1 Like 'blah''

Obviously this is invalid as the single quote before "blah" would end the
varchar string.

How do I get round this?



Report this thread to moderator Post Follow-up to this message
Old Post
Steve
09-20-05 12:23 PM


Re: Single quote in NVARCHAR string?
You have to double quote that, or use the CHAR expression:

SELECT '''SELECT * FROM tblTest WHERE Field1 Like ''blah'''''
OR
SELECT CHAR(39) + 'SELECT * FROM tblTest WHERE Field1 Like ' + CHAR(39)
+ 'blah' + CHAR(39) + CHAR(39)


HTH, Jens Suessmeyer.


Report this thread to moderator Post Follow-up to this message
Old Post
Jens
09-20-05 12:23 PM


Re: Single quote in NVARCHAR string?
Use two single quotes - see "Using char and varchar Data" in Books
Online.

SELECT @strsql = 'SELECT * FROM tblTest WHERE Field1 Like ''blah'

Also, avoid using double quotes around strings - they're treated as
identifier delimiters when SET QUOTED_IDENTIFIER is ON, and since you
need that option ON to use indexed views and indexed computed columns,
it's best to stick to single quotes in all cases. See "Delimited
Identifiers" in Books Online for more details.

Simon


Report this thread to moderator Post Follow-up to this message
Old Post
Simon Hayes
09-20-05 12:23 PM


Re: Single quote in NVARCHAR string?
Use the below code with  "set quoted_identifier off"


set quoted_identifier off
declare @strsql nvarchar(500)
SELECT @strsql = "SELECT * FROM tblTest WHERE Field1 Like 'blah%'"

Thanks
Hari
SQL Server MVP

"Steve" <steve@hello.com> wrote in message news:432ff8b5_3@x-privat.org...
> How do I get a single quote (') in a NVARCHAR string in MS SQL Server?
>
> e.g. SELECT @strsql = "SELECT * FROM tblTest WHERE Field1 Like 'blah''
>
> Obviously this is invalid as the single quote before "blah" would end the
> varchar string.
>
> How do I get round this?
>



Report this thread to moderator Post Follow-up to this message
Old Post
Hari Prasad
09-20-05 12:23 PM


Re: Single quote in NVARCHAR string?
Use single-quotes to delimit the string.  Use double single-quotes inside
the string:

SELECT @strsql = ''SELECT * FROM tblTest WHERE Field1 Like ''blah'''


--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada
www.pinpub.com
.
"Steve" <steve@hello.com> wrote in message news:432ff8b5_3@x-privat.org...
How do I get a single quote (') in a NVARCHAR string in MS SQL Server?

e.g. SELECT @strsql = "SELECT * FROM tblTest WHERE Field1 Like 'blah''

Obviously this is invalid as the single quote before "blah" would end the
varchar string.

How do I get round this?



Report this thread to moderator Post Follow-up to this message
Old Post
Tom Moreau
09-20-05 12:23 PM


Re: Single quote in NVARCHAR string?
Typo:

SELECT @strsql = 'SELECT * FROM tblTest WHERE Field1 Like ''blah'''


--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada
www.pinpub.com
.
"Tom Moreau" <tom@dont.spam.me.cips.ca> wrote in message
news:eZ0HNxdvFHA.3000@TK2MSFTNGP12.phx.gbl...
Use single-quotes to delimit the string.  Use double single-quotes inside
the string:

SELECT @strsql = ''SELECT * FROM tblTest WHERE Field1 Like ''blah'''


--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada
www.pinpub.com
.
"Steve" <steve@hello.com> wrote in message news:432ff8b5_3@x-privat.org...
How do I get a single quote (') in a NVARCHAR string in MS SQL Server?

e.g. SELECT @strsql = "SELECT * FROM tblTest WHERE Field1 Like 'blah''

Obviously this is invalid as the single quote before "blah" would end the
varchar string.

How do I get round this?



Report this thread to moderator Post Follow-up to this message
Old Post
Tom Moreau
09-20-05 12:23 PM


Re: Single quote in NVARCHAR string?
> Use the below code with  "set quoted_identifier off"

This would not be my recommendation.  This is not a typical setting and is
likely to cause confusion for other users of the system...



Report this thread to moderator Post Follow-up to this message
Old Post
Aaron Bertrand [SQL Server MVP]
09-20-05 12:23 PM


Re: Single quote in NVARCHAR string?
Xref: number1.nntp.dca.giganews.com comp.databases.ms-sqlserver:158204 micro
soft.public.sqlserver.programming:593281 microsoft.public.sqlserver.server:4
17247

> e.g. SELECT @strsql = "SELECT * FROM tblTest WHERE Field1 Like 'blah''

Why is this in a variable?  Why don't you just execute

SELECT * FROM tblTest WHERE Column1 = 'blah'
or
SELECT * FROM tblTest WHERE Column1 LIKE 'blah%'

?



Report this thread to moderator Post Follow-up to this message
Old Post
Aaron Bertrand [SQL Server MVP]
09-20-05 02:23 PM


Re: Single quote in NVARCHAR string?
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in mess
age
news:uneus9dvFHA.612@TK2MSFTNGP10.phx.gbl... 
>
> Why is this in a variable?  Why don't you just execute
>
> SELECT * FROM tblTest WHERE Column1 = 'blah'
> or
> SELECT * FROM tblTest WHERE Column1 LIKE 'blah%'
>
> ?

That isn't my actual code and was just an example.

Thanks to all who have responded.



Report this thread to moderator Post Follow-up to this message
Old Post
Steve
09-20-05 02:23 PM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 01:20 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006