Solved

replace single quotes sql server

Posted on 2012-03-18
13
525 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

895 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

11 Experts available now in Live!

Get 1:1 Help Now