Solved

replace single quotes sql server

Posted on 2012-03-18
13
531 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
[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
  • 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
Independent Software Vendors: 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 42

Expert Comment

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

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
 
LVL 34

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 39

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 34

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 39

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

724 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