Solved

Getting text from table with single and double quotes

Posted on 2014-01-22
4
207 Views
Last Modified: 2014-03-12
Hello,

I would like to create a function that will be passed in quite a lot of text and which will replace any single and/or double quotes with the html value for them.  I have tried the code below but I get an error in compiling it....

I am not sure what I am doing wrong.  Any help would be much appreciated.

Cheers,

DECLARE @X nvarchar(100)
DECLARE @MyText nvarchar(100)
SET @MyText = 'he's not "very" good'

SET @X = REPLACE(@MyText,'"','"')
SET @X = REPLACE(@X,'''''',''')

PRINT @X

Open in new window

0
Comment
Question by:1Cougar
4 Comments
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39799798
In a single-quote delimited value (Line 3), you need 2 single-quotes to represent a single-quote:
SET @MyText = 'he''s not "very" good'

Open in new window

0
 

Author Comment

by:1Cougar
ID: 39799843
Hello and thank you....but the text in the database (entered by a user) has only single quote.  I want to retrieve this text but I need to change the single quote to '''

?

Thanks again,
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39799977
I just corrected your code check the new one below

DECLARE @X nvarchar(100)
DECLARE @MyText nvarchar(100)
SET @MyText = 'he''s not "very" good'

SET @X = REPLACE(@MyText,'"','"')
SET @X = REPLACE(@X,'''',''')

PRINT @X

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 39800885
replace(replace(mytext,char(34),'"'),char(39),'&#39')
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question