有没有办法提取列中的列表项并将每个项放在自己的行中?我也想废除列表括号[]!有人可以帮忙吗?
输入:
name doc_id type
JD [409839589143224] 1843
JD [470573363064028,239564999464566] 778
BK [426580780775177,342730259144025] 202
期望的输出:
name doc_id type
JD 409839589143224 1843
JD 470573363064028 778
JD 239564999464566 778
BK 426580780775177 202
BK 342730259144025 202
答案 0 :(得分:0)
这是使用tsql的解决方案,我确信你可以用MySql做同样的事情
CREATE FUNCTION ufn_split (@list nvarchar(MAX))
RETURNS @tbl TABLE (part nvarchar(max) NOT NULL) AS
BEGIN
DECLARE @pos int,
@nextpos int,
@valuelen int
SELECT @pos = 0, @nextpos = 1
WHILE @nextpos > 0
BEGIN
SELECT @nextpos = charindex(',', @list, @pos + 1)
SELECT @valuelen = CASE WHEN @nextpos > 0
THEN @nextpos
ELSE len(@list) + 1
END - @pos - 1
INSERT @tbl (part)
VALUES (substring(@list, @pos + 1, @valuelen))
SELECT @pos = @nextpos
END
RETURN
END;
GO
SELECT t.name, st.part, t.type
FROM @table AS t
CROSS APPLY ufn_split( LEFT(RIGHT(t.doc_id, LEN(t.doc_id) - 1),LEN(t.doc_id) - 2) ) AS st;
第一个功能是从这里Split function equivalent in T-SQL?
在TSQL上实现Split功能