2015-12-12 69 views

回答

3

您可以使用簡單的SUBSTRING從開始到最後-

SELECT col, SUBSTRING(col, 0, LENGTH(col) - INSTR(REVERSE(col), '-')) AS result 
FROM tab; 

SqlFiddleDemo

輸出:

╔═══════════════════════╦══════════════════╗ 
║   col   ║  result  ║ 
╠═══════════════════════╬══════════════════╣ 
║ some1-some2-som3-123 ║ some1-some2-som3 ║ 
║ some1-some2-143  ║ some1-some2  ║ 
║ some1-1235   ║ some1   ║ 
╚═══════════════════════╩══════════════════╝ 
1

DEMO

首先找到的最後一個索引 ' - '

SELECT LENGTH("first-middle-last") - LOCATE('-', REVERSE("first-middle-last")); 

然後將解壓後的左側部分 -

SELECT LEFT("first-middle-last", LENGTH("first-middle-last") - LOCATE('-', REVERSE("first-middle-last")));