问题
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