Solved

Update Single Quote with Double Quote in SQL Server 2005

Posted on 2011-09-13
14
292 Views
Last Modified: 2012-05-12
Hello Experts,

I have string like  john'King, whenever I tried to Insert/Update my query fails. Due to single quote in the string, I need to replace all my single quote with double.


Thanks,
ASPDEV
0
Comment
Question by:ASPDEV
[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
  • 5
  • 4
  • 4
  • +1
14 Comments
 
LVL 10

Expert Comment

by:dwe761
ID: 36532362
Something like this:

update [YourTable]
set [YourField]= replace([YourField], '','''')
where patindex('',[YourField]) <>0
0
 
LVL 10

Expert Comment

by:dwe761
ID: 36532427
Actually, I had better luck with CharIndex on this one and you need 4 single quotes rather than 2:

update [YourTable]
set [YourField]= replace([YourField], '','''')
where charindex('''',[YourField]) <>0
0
 

Author Comment

by:ASPDEV
ID: 36532486
I tried both , it didn't work
0
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.

 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 36532511
what do you have in table and what column are you trying to update?
0
 
LVL 10

Accepted Solution

by:
dwe761 earned 500 total points
ID: 36532512
So sorry, just trying to go too fast:
Use this instead.

The REPLACE command must have 4 single quotes and then 6 single quotes
The WHERE clause must have 4 single quotes:

update [YourTable]
set [YourField]= REPLACE ([YourField], '''','''''')
WHERE charindex('''',[YourField]) <>0
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 36532528
post your query... if you are creating sql in your app like

sql = "update mytable set full_name = '" & full_name & "' where id = " & id

then you should modify it like

sql = "update mytable set full_name = '" & replace(full_name,"'","''") & "' where id = " & id


0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 36532533
Are you using dynamic SQL to update the database?  Would you be able to post your update code?  You should be allowed to put a single quote into the database.
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 36532538
looks like insufficient info here... without related code, everybody will waste time :)
0
 

Author Comment

by:ASPDEV
ID: 36532573
Okay,

I 'm trying to update table [EMP] with field [NAME] as per above request..

TABLE-EMP

NAME          
----------
JOHN 'King
GREY 'JOEL
KIMS 'LEE


I need like this

NAME
------
JOHN ''KING
GREY "JOEL
KIMS "LEE
0
 
LVL 10

Expert Comment

by:dwe761
ID: 36532586
My last query will do that.  Just replace it with your table name and your field name.
0
 

Author Comment

by:ASPDEV
ID: 36532597
dwe761:,

I tried you sql statement :

update [YourTable]
set [YourField]= REPLACE ([YourField], '''','''''')
WHERE charindex('''',[YourField]) <>0

Now it again updates all the records, which already updated with double quotes to 2X Double quotes..
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 36532612
look at this:

declare @name varchar(100)='Hain''Kurt';
select @name, REPLACE(@name,'''','''''')

Hain'Kurt      Hain''Kurt

what query are you using...

update myTable set name = replace(name,'''','''''')
where name <> replace(name,'''','''''')

should work...
0
 

Author Comment

by:ASPDEV
ID: 36532616

Okay, finally this worked

update [YourTable]
set [YourField]= REPLACE ([YourField], '''','"')
WHERE charindex('''',[YourField]) <>0
0
 

Author Closing Comment

by:ASPDEV
ID: 36532623
Thanks.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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 explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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