從 23.4 版本開始, LightDB 支持 mysql 的substring_index 函數。下面對這個函數進行介紹
substring_index(str, delim, count )
這個函數用于從指定字符串str中返回到達分隔符delim出現次數(count)之前的子字符串。。具體見之后用例:
mysql 中介紹:
Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.
LightDB 中函數定義
substring_index(str varchar, delim varchar, count integer) RETURNS varchar
注意點
- 如果count為正,則返回最后一個分隔符左側的所有元素(從左側開始計數)。
- 如果count為負數,則返回最后一個分隔符右側的所有內容(從右側開始計數)。
- SUBSTRING_INDEX()在搜索delim時區分大小寫。
- 這個函數是多字節安全的。
- 如果SUBSTRING_INDEX()的任何參數為NULL,則返回NULL。
示例
效果與mysql相同,不再列舉mysql用例
-- 從左邊開始返回第二個分隔符'.' 之前的子串
lightdb@test_m=# SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);substring_index
-----------------www.mysql
(1 row)lightdb@test_m=# SELECT SUBSTRING_INDEX('www.mysql.com', '.', -1);substring_index
-----------------com
(1 row)lightdb@test_m=# SELECT SUBSTRING_INDEX('mysql.Mysql.mysql', 'm', 2);substring_index
-----------------mysql.Mysql.
(1 row)lightdb@test_m=# SELECT SUBSTRING_INDEX('mysql.Mysql.mysql', 'M', 1);substring_index
-----------------mysql.
(1 row)lightdb@test_m=# SELECT SUBSTRING_INDEX('www.mysql.com', '', 1) is null;?column?
----------f
(1 row)lightdb@test_m=# SELECT SUBSTRING_INDEX('測試.測試mysql.com測試', '.', 1);substring_index
-----------------測試
(1 row)lightdb@test_m=# SELECT SUBSTRING_INDEX('測試.測試mysql.com測試', '測', 2);substring_index
-----------------測試.
(1 row)