Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

replace field value using wildcard

Posted on 2014-02-22
7
Medium Priority
?
363 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 46

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 46

Accepted Solution

by:
Kent Olsen earned 2000 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 46

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 46

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

636 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