Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Update Single Quote with Double Quote in SQL Server 2005

Posted on 2011-09-13
14
Medium Priority
?
306 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
  • 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
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!

 
LVL 61

Expert Comment

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

Accepted Solution

by:
dwe761 earned 2000 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 61

Expert Comment

by:HainKurt
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 61

Expert Comment

by:HainKurt
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 61

Expert Comment

by:HainKurt
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

824 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