Home >Database >Mysql Tutorial >How Can I Extract the Last Element from a String in SQL?
Question:
In a data table containing strings such as:
Articles/Search/ArtMID/2681/ArticleID/2218/Diet.aspx OurStory/MeettheFoodieandtheMD.aspx TheFood/OurMenu.aspx
How can we isolate and extract the final element (e.g., "Diet.aspx", "MeettheFoodieandtheMD.aspx", "OurMenu.aspx") from each string?
Answer:
In SQL, this can be achieved using the following syntax:
SELECT SUBSTRING(string, LEN(string) - CHARINDEX('/', REVERSE(string)) + 2, LEN(string)) FROM SAMPLE;
Explanation:
Here's a JSFiddle for reference: http://sqlfiddle.com/#!3/41ead/11
The above is the detailed content of How Can I Extract the Last Element from a String in SQL?. For more information, please follow other related articles on the PHP Chinese website!