Solved

Update Single Quote with Double Quote in SQL Server 2005

Posted on 2011-09-13
14
286 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now