Is there a way to find a last substring?

大兔子大兔子 提交于 2020-01-06 08:12:56

问题


my brain might be really tired but i coudn't seem to work my head around this simple query.. I want to extract a last substring within delimiters such as ',' from a column? i was thinking about using REVERSE or RIGHT.. but the results are coming off as really bizarre...

Lets say i have a table column 'DESCRIPTION' in a table LOANS with an entry

Changed Loan Laptop from "IT-X130E-10" to "IT-X130E-9".

and i want the last substring

IT-X130E-9

within delimiters '"' hope its clearer now


回答1:


Can try SUBSTRING INDEX(str,delim,count)

SELECT SUBSTRING_INDEX(
    SUBSTRING_INDEX('yourString', '"', -2), '"', 1);



回答2:


The key to this is reverse() and some brute force:

select replace(replace(right(val, instr(substring(reverse(val), 3, 100), '"')+2), '"', ''), '.', '')
from (select 'Changed Loan Laptop from "IT-X130E-10" to "IT-X130E-9".' as val) t

This assumes that the string does not contain either '"' or '.'.



来源:https://stackoverflow.com/questions/17226668/is-there-a-way-to-find-a-last-substring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!