[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to replace the specific pattern in the string.

Posted on 2009-07-14
11
Medium Priority
?
800 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
[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
  • 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 168 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 17

Assisted Solution

by:pssandhu
pssandhu earned 168 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 164 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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 …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

649 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