sqlserver 拆分逗号分隔的字符串


CREATE TABLE TA(id INT,[value] VARCHAR(100), class VARCHAR(10));

INSERT INTO TA(id,[value],class)
SELECT 1,'3,2,4,5','no' UNION ALL
SELECT 2,'5,1,1+2','yes'  


SELECT T.id
  ,SUBSTRING(T.[value],R.number,CHARINDEX(',',T.[value]+',',R.number)-R.number) AS value
  ,T.class
  ,ROW_NUMBER() OVER (PARTITION BY T.id ORDER BY T.id) AS code
FROM TA AS T
JOIN (
    SELECT TOP 10 number FROM  [master].dbo.spt_values  
    WHERE [type]='P' ORDER BY number)AS R ON (CHARINDEX(',',','+T.[value],R.number)=R.number);

你可能感兴趣的:(SQLServer)