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

posted on 2020-03-02 18:57  哦呵呵G  阅读(1693)  评论(0编辑  收藏  举报

导航