Solved

Problem using substr() function in MySql

Posted on 2014-03-20
8
313 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 24

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
 
LVL 27

Expert Comment

by:yodercm
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
IT, Stop Being Called Into Every Meeting

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!

 

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:yodercm
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
 

Author Closing Comment

by:abaqasah
ID: 39952722
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

15 Experts available now in Live!

Get 1:1 Help Now