2
some1-some2-som3-123
some1-some2-143
some1-1235
如何刪除最後一部分-123
?從該字符串與SQL statment
some1-some2-som3-123
some1-some2-143
some1-1235
如何刪除最後一部分-123
?從該字符串與SQL statment
您可以使用簡單的SUBSTRING
從開始到最後-
:
SELECT col, SUBSTRING(col, 0, LENGTH(col) - INSTR(REVERSE(col), '-')) AS result
FROM tab;
輸出:
╔═══════════════════════╦══════════════════╗
║ col ║ result ║
╠═══════════════════════╬══════════════════╣
║ some1-some2-som3-123 ║ some1-some2-som3 ║
║ some1-some2-143 ║ some1-some2 ║
║ some1-1235 ║ some1 ║
╚═══════════════════════╩══════════════════╝
首先找到的最後一個索引 ' - '
SELECT LENGTH("first-middle-last") - LOCATE('-', REVERSE("first-middle-last"));
然後將解壓後的左側部分 -
SELECT LEFT("first-middle-last", LENGTH("first-middle-last") - LOCATE('-', REVERSE("first-middle-last")));