sql中某一个字段内容为用逗号分割的字符串转换成多条数据并附Id

WITH testtb2 AS 
(
  SELECT 
    A.ID, A.VALUE, CHARINDEX(',', A.VALUE) AS STA, CHARINDEX(',', A.VALUE) - 1 AS LENS
  FROM @table1 AS A 
  UNION ALL
  SELECT 
    ID, VALUE, CHARINDEX(',', VALUE, STA + 1) AS STA, CHARINDEX(',', VALUE, STA + 1) - STA - 1 AS LENS
  FROM  [表名] AS testtb2_2
  WHERE (STA <> 0)
)
SELECT TOP (100) PERCENT SUBSTRING(VALUE, STA - LENS, LENS) AS OrderRequest, ID 
FROM testtb2 AS testtb2_1
WHERE (STA <> 0)
ORDER BY VALUE

原文链接:https://www.cnblogs.com/MoMo1002/p/6594832.html

上一篇:还是DFS——new_bzoj Sta


下一篇:PAT (Advanced Level) 1016 Phone Bills