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

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

标签:为用,STA,VALUE,LENS,CHARINDEX,testtb2,sql,Id,SELECT
来源: https://www.cnblogs.com/gsh0921/p/12397374.html