达梦数据库切割字符串
时间: 2024-01-03 08:03:40 浏览: 450
以下是在达梦数据库中切割字符串的方法:
1.使用正则表达式函数regexp_substr(),该函数可以从字符串中提取指定模式的子字符串。例如,以下查询将字符串拆分为多行:
```sql
WITH temp AS (
SELECT 108 NAME, 'test' Project, 'Err1, Err2, Err3' Error FROM DUAL
UNION ALL
SELECT 109, 'test2', 'Err1' FROM DUAL
)
SELECT DISTINCT NAME, Project, TRIM(REGEXP_SUBSTR(str, '[^,]+', 1, LEVEL)) str
FROM (
SELECT NAME, Project, Error str FROM temp
)
CONNECT BY INSTR(str, ',', 1, LEVEL - 1) > 0
ORDER BY NAME;
```
2.使用自定义函数,例如以下函数可以将字符串按指定分隔符拆分为多行:
```sql
CREATE OR REPLACE FUNCTION split_string(
p_string IN VARCHAR2,
p_delimiter IN VARCHAR2
) RETURN sys.odcivarchar2list PIPELINED
IS
l_idx PLS_INTEGER;
l_list sys.odcivarchar2list := sys.odcivarchar2list();
BEGIN
LOOP
l_idx := INSTR(p_string, p_delimiter);
IF l_idx > 0 THEN
PIPE ROW(SUBSTR(p_string, 1, l_idx - 1));
p_string := SUBSTR(p_string, l_idx + LENGTH(p_delimiter));
ELSE
PIPE ROW(p_string);
EXIT;
END IF;
END LOOP;
RETURN;
END;
```
使用该函数,可以将字符串拆分为多行,例如:
```sql
SELECT COLUMN_VALUE str
FROM TABLE(split_string('Err1, Err2, Err3', ','));
```
阅读全文