Solved

replace single quotes sql server

Posted on 2012-03-18
13
528 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 the fundamental information of how to create a table.

697 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