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
Solved

How to replace the specific pattern in the string.

Posted on 2009-07-14
11
795 Views
Last Modified: 2012-05-07
I have a column in the table of type varchar and usually the column contains the values like :

389238473
387983483/A
74239384
291034734
8034834/A2
238905934/A1
609834
4638340738475/A5
001298434

In the above values, I want to remove/replace the values which end with '/A1', '/A2' ........  '/A9'  

i.e   238905934/A1   -->  238905934

The number followed by '/A' can be between 1 to 9.  Can I do this with replace function using pattern matching.

0
Comment
Question by:nazeemunnisa
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 17

Expert Comment

by:pssandhu
ID: 24850812
UPDATE MyTable
SET Column = SUBSTRING(Column,1,CHARINDEX('/',Column)-1)
This will update all the value in your table by removing the / and the values after it. Please test first.
P.
0
 
LVL 8

Accepted Solution

by:
MartinCMS earned 42 total points
ID: 24851782
Great code pssandhu!

nazeemunnisa - you will need a where clause along with pssandhu code otherwise you will get an error for any column that does not contains '/' char.

UPDATE MyTable
SET Column = SUBSTRING(Column,1,CHARINDEX('/',Column)-1)
where CHARINDEX('/', Column) <> 0
0
 

Author Comment

by:nazeemunnisa
ID: 24857527
Thanks a lot for solution.  Between I'm also interested to know if there is some solution which can be done through pattern matching.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 17

Assisted Solution

by:pssandhu
pssandhu earned 42 total points
ID: 24859212
Is this what you mean:

UPDATE MyTable
SET  Column = SUBSTRING(Column,1,PATINDEX('%/A%[0-9]', Column)-1)
WHERE  PATINDEX('%/A%[0-9]', Column) <> 0

Open in new window

0
 

Author Comment

by:nazeemunnisa
ID: 24860738
This would not work if the string ends with '/A' Ex:   387983483/A
0
 
LVL 41

Assisted Solution

by:ralmada
ralmada earned 41 total points
ID: 24860825
I would do it like this:
UPDATE yourTable
SET  yourColumn = LEFT(yourColumn,PATINDEX('%/A%', yourColumn)-1)
WHERE  PATINDEX('%/A%', yourColumn) <> 0

Open in new window

0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24860827
<<In the above values, I want to remove/replace the values which end with '/A1', '/A2' ........  '/A9' >>
When I re-read your question I think you only wanted where /A was ending with a number.
Anyways, in that case wouldn't something like this work for you?

UPDATE MyTable
SET  Column = SUBSTRING(Column,1,PATINDEX('%/A%', Column)-1)
WHERE  PATINDEX('%/A%', Column) <> 0

Open in new window

0
 
LVL 41

Expert Comment

by:ralmada
ID: 25609531
I feel I've also contributed to this question.
0
 
LVL 41

Expert Comment

by:ralmada
ID: 25609533
I feel I've also contributed to this question.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

765 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