ITMISS
我之前使用过这个SQL可能对你有用: -CREATE FUNCTION dbo.splitstring ( @stringToSplit VARCHAR(MAX) )RETURNS @returnList TABLE ([Name] [nvarchar] (500))ASBEGIN
DECLARE @name NVARCHAR(255)
DECLARE @pos INT WHILE CHARINDEX(',', @stringToSplit) > 0
BEGIN
SELECT @pos = CHARINDEX(',', @stringToSplit)
SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)
INSERT INTO @returnList
SELECT @name SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
END
INSERT INTO @returnList SELECT @stringToSplit RETURNEND并使用它: -SELECT * FROM dbo.splitstring('91,12,65,78,56,789')
Helenr
最简单的方法是使用XML格式。1.将字符串转换为不带表的行QUERYDECLARE @String varchar(100) = 'String1,String2,String3'-- To change ',' to any other delimeter, just change ',' to your desired oneDECLARE @Delimiter CHAR = ',' SELECT LTRIM(RTRIM(Split.a.value('.', 'VARCHAR(100)'))) 'Value' FROM ( SELECT CAST ('<M>' + REPLACE(@String, @Delimiter, '</M><M>') + '</M>' AS XML) AS Data ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a)结果 x---------x | Value | x---------x | String1 | | String2 | | String3 | x---------x2.从表中转换为具有每个CSV行的ID的行消息表 x-----x--------------------------x | Id | Value | x-----x--------------------------x | 1 | String1,String2,String3 | | 2 | String4,String5,String6 | x-----x--------------------------xQUERY-- To change ',' to any other delimeter, just change ',' before '</M><M>' to your desired oneDECLARE @Delimiter CHAR = ','SELECT ID,LTRIM(RTRIM(Split.a.value('.', 'VARCHAR(100)'))) 'Value' FROM ( SELECT ID,CAST ('<M>' + REPLACE(VALUE, @Delimiter, '</M><M>') + '</M>' AS XML) AS Data FROM TABLENAME) AS A CROSS APPLY Data.nodes ('/M') AS Split(a)结果 x-----x----------x | Id | Value | x-----x----------x | 1 | String1 | | 1 | String2 | | 1 | String3 | | 2 | String4 | | 2 | String5 | | 2 | String6 | x-----x----------x