Solved

Update Single Quote with Double Quote in SQL Server 2005

Posted on 2011-09-13
14
289 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 51

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 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 51

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 51

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 51

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

792 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