Solved

replace single quotes sql server

Posted on 2012-03-18
13
524 Views
Last Modified: 2012-06-27
we are trying this

UPDATE table1
SET description = REPLACE([description], '''', '') where username ='user123'

error:

Argument data type text is invalid for argument 1 of replace function.
0
Comment
Question by:goodluck11
  • 3
  • 3
  • 3
  • +2
13 Comments
 

Author Comment

by:goodluck11
ID: 37735415
this is on SQl server 2008
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 37735447
Convert the text before doing the replace:

UPDATE table1
SET description = REPLACE(CONVERT(varchar(max), [description]), '''', '') 
where username ='user123'

Open in new window

0
 
LVL 42

Expert Comment

by:dqmq
ID: 37735452
This might work:

UPDATE table1
SET description = cast(REPLACE(cast([description] as varchar(max)), '''', '') as text) where username ='user123'
0
 
LVL 42

Expert Comment

by:dqmq
ID: 37735454
or the better long term solution: permanently convert the column to varchar(max)
0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 250 total points
ID: 37735457
I think it could be a problem with the quotes, which is why you posted the question I suppose.

Anyway, try using CHAR(39) where 39 is the ASCII code for a single quote.

UPDATE table1
SET description = REPLACE([description], CHAR(39), '') where username ='user123'
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 37735471
dqmq wrote:

or the better long term solution: permanently convert the column to varchar(max)

Agreed!

imnorie wrote:

I think it could be a problem with the quotes

Unlikely.  The error message is consistent with passing a text or ntext value into the REPLACE function, which REPLACE does not support.  dqmq and I sidestep that by converting the text value to a varchar(max) first.

:)
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 33

Expert Comment

by:Norie
ID: 37735482
Yes, I realised the error message wasn't down to the single quote after reading the other posts.
0
 

Author Comment

by:goodluck11
ID: 37735679
>or the better long term solution: permanently convert the column to varchar(max)

But this has an 8,000 char limitation correct ?
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 37735924
Yes. Varchar(max) is the same as varchar(8000). And if you ever import/export using excel varchar(8000) is actually better as max does funny things sometimes.
0
 
LVL 33

Expert Comment

by:Norie
ID: 37735965
How many characters do you have in the field?
0
 

Author Comment

by:goodluck11
ID: 37736291
these are long html files... text would be best ?
0
 
LVL 42

Expert Comment

by:dqmq
ID: 37736404
>Varchar(max) is the same as varchar(8000).

That is not true.  Varchar(max) can hold up to 2GB and is not subject to the numerous limitations of text.  Also, the text datatype has been deprecated and MS has announced their intention to drop it in some unspecified future release.
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 37737541
I stand corrected
http://msdn.microsoft.com/en-us/library/ms176089.aspx

Apparently it's been that way since mssql 2005 and I never notice the change. Nice to lean something new.
0

Featured Post

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

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

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

13 Experts available now in Live!

Get 1:1 Help Now