Solved

replace field value using wildcard

Posted on 2014-02-22
7
359 Views
Last Modified: 2014-02-25
How can I replace a string value that uses a wildcard?  For example, I'm trying to update a field on a table and replace the field value where it starts with a specific two character pattern followed by any series of characters and just replace it with just a two character pattern.  Something like this:

UPDATE myTable SET myField = REPLACE(myField,'XX%','XX')

But, the above doesn't work?

Any help is appreciated.
Thanks.
0
Comment
Question by:-Dman100-
[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
  • 4
  • 2
7 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39879893
You're close.  :)

UPDATE mytable SET myfield = 'something'
WHERE myfield like 'XX%';

or

UPDATE mytable SET myfield = 'something'
WHERE substring (myfield, 1, 2) = 'XX';


They're equivalent.

Good Luck,
Kent
0
 

Author Comment

by:-Dman100-
ID: 39880817
Hi Kent,

Thanks for replying to my post.  My apologies, my explanation was not correct.  Here is an example of what I'm trying to do.  Let's say my field value is this:

;00236;08231;06106 Washington DC;06106 CCA Wash DC;05120;A0106;

Open in new window


I want the result to be this:

;00236;08231;06106 Washington DC;06106 CCA Wash DC;05120;

Open in new window


The field is a delimited string and I want to replace any occurrence of ;A0% or ;A0* with a null value.   I don't want to replace the entire field with a null value.

Does that help explain?

Thanks.
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 39880836
Hi Dman,

The short answer is, "it depends on what version of SQL Server you're using".

There's a long-hand method that will work on any version of SQL Server, but it's a bit cumbersome and ugly.  If you're running 2012, the REPLACE command is trivial.

Let's hope that you're using 2012.  :)

UPDATE mytable SET myfield = REPLACE (myfield, oldvalue, newvalue)
WHERE myfield like '%oldvalue%';


Kent
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39883402
If you're running 2012, the REPLACE command is trivial.
As far as I know REPLACE (Transact-SQL) has not changed since its introduction with SQL Server 7.

Is there something I am missing?
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39883478
Hi Anthony,

I thought that REPLACE came in with 2012.  Or am I completely off in space???
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39884379
If you check the link I posted you will see that it includes documentation for 2005 (the oldest supported version), however I believe that function was first introduced with SQL Server 7 (If I recall correctly it did not exist in SQL Server 6.5)
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39884410
Too many databases and too many versions to keep in my head.  :(


Apologies, Dman.  As Anthony points out, it may well work with the version that you have.


Kent
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

726 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