Solved

replace single quotes sql server

Posted on 2012-03-18
13
527 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

772 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