Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem using substr() function in MySql

Posted on 2014-03-20
8
Medium Priority
?
330 Views
Last Modified: 2014-03-25
Hi

I need to extract a string A from string B where B is stored in MySQL table.

This is string B:
#/schema#/CT[Items]#/sequence[0]#/element[item]#/CT[]#/sequence[0]#/element[productImage]

And this is A:
#/schema#/CT[Items]#/sequence[0]#/element[item]#/CT[]#/sequence[0]

The delimiter in my case is '#' and I need to remove it along with the following characters '/element[productImage]'.

I tried different functions like SUBSTR(str, pos, len), POSITION(substr IN str), and REVERSE(str) but cannot solve the problem.

Note that index of the last occurrence of '#' is unknown. And I can not find a way to locate the last index of the '#' character (like lastIndexOf() function in JAVA).
 
I believe that there is a way to do it by reversing the whole string first, cutting the unnecessary part then reversing again to get the desired result.

Can any one help please
0
Comment
Question by:abaqasah
  • 5
  • 2
8 Comments
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 39941959
Hi!

If you know the string to remove then you can use

REPLACE(org_string, string_to_replace, replace_str)

where replace_str is the '' (empty string).

Note that this replaces all occurences of the string_to_replace in org_string

http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_replace

Regards,
    Tomas Helgi
0
 

Author Comment

by:abaqasah
ID: 39942001
Hi TomasHelgi

I don't know exactly the string_to_replace. All I know is the org_string and the delimiter '#' which should be used to remove the last part '#/element[productImage]' from the org_string.

The actual problem is how to locate the last occurrence of '#' character.

Regards
0
 

Accepted Solution

by:
abaqasah earned 0 total points
ID: 39942037
Hi

I found a solution in other place:
Here it is:

LEFT(str, CHAR_LENGTH(str) - LOCATE('#', REVERSE(str)))

Cheers
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 39942083
The SUBSTRING_INDEX() function has a parameter that allows you to select the LAST occurrence of a string.

So if you use something like this:

StringA = SUBSTRING(StringB,"#",-1)

that will give you everything to the left of the last # in String B.
0
 

Author Comment

by:abaqasah
ID: 39944169
Hi yodercm

Applying
SUBSTRING(StringB,"#",-1)
will not result anything.

You probably mean
SUBSTRING_INDEX(StringB,"#",-1)

And that solution actually return everything in the right part of '#' and NOT in the left.
Result of applying this in my string B (mentioned in the question):
/element[productImage]
Which is not what I want

Regards,
Abdullah
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 39944223
You are right, I got in a rush and omitted a main part of the line.

String A =  SELECT SUBSTRING(StringB FROM 1 FOR (LENGTH(StringB)-1-LENGTH(SUBSTRING_INDEX(StringB,"#",-1)))) FROM Table...

That works as you wish.
0
 

Author Comment

by:abaqasah
ID: 39944306
Hi  yodercm

That is another solution :)
I already apply the first solution
LEFT(str, CHAR_LENGTH(str) - LOCATE('#', REVERSE(str)))
Found it easy to write

Thanks anyway
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month6 days, 23 hours left to enroll

783 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