Solved

Problem using substr() function in MySql

Posted on 2014-03-20
8
317 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
[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
  • 5
  • 2
8 Comments
 
LVL 25

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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
 

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

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