Solved

Getting text from table with single and double quotes

Posted on 2014-01-22
4
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

739 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