Solved

replacing multiple types of substring with a single substring in MS SQL 2005

Posted on 2014-02-24
4
324 Views
Last Modified: 2014-03-10
Hi

 I wanted to replace any of the below variations  in a string with the single sting RPT. How can I do it in one line?

Any number immediately following a "RPT#<n> -" should be replaced with RPT#<n>. We can assume <n> is a single digit.

string = 'Adam reports RPT#2-XYZ' should look like Adam reports RPT#2XYZ
string = 'Adam reports RPT#3-ABC' should look like Adam reports RPT#3ABC

As the "-" occurs else where in the longer strings I can not simply replace "-" with "".
0
Comment
Question by:LuckyLucks
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Steve Wales
Comment Utility
Ok, so maybe not overly graceful, but as long as the fixed string is "RPT#x-" and you need to get rid of the "-", and the string only appears once ....

This appears to work.

update table1
set col1 = stuff(col1, charindex('RPT#', col1)+5, 1, '') 
where col1 like '%RPT#_-%'

Open in new window


It specifically searches for the starting position of 'RPT#' - adds 5 bytes (since the "-" is 5 characters after the R) and replaces it with nothing.

Wouldn't use it all the time, but if it's a one off data fix, it might just do the trick - however review your expected outcome carefully.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
One minor enhancement to insure accuracy with the stated requirements -- NO pts for me just for this please.


where col1 like '%RPT#[0-9][-]%'
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
Comment Utility
Any chance that the number following RPT will have more than one digit?  If so, Scott's suggestion may not always work.

Is it true that the sequence <digit><hyphen><letter> only appears in instances where you would want to remove the hyphen?  If so, you may want to go with:

where col1 like '%[0-9][-][A-Z]%'

Open in new window

0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
>> Any chance that the number following RPT will have more than one digit?  If so, Scott's suggestion may not always work. <<

The original pattern from Steve also only allowed for a single character.  The main difference is that I limited that character to being a digit (0-9).
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.

771 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

10 Experts available now in Live!

Get 1:1 Help Now