Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting text from table with single and double quotes

Posted on 2014-01-22
4
Medium Priority
?
210 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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

661 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