value ---------------------------------------------------------------------------------------------------------------- A00 A03 A06 B00 C00 C03
(6 行受影响)
[解决办法]
DECLARE @string VARCHAR(100)
SET @string = 'A00,A03-A06,B00,C00-C03'
;WITH cte AS ( SELECT rowid=ROW_NUMBER() OVER(ORDER BY GETDATE()), ch = LEFT(B.string, 1), beginstr = STUFF(LEFT(B.string, LEN(B.string)-CHARINDEX('-', B.string)),1,1,''), endstr = STUFF(SUBSTRING(B.string, CHARINDEX('-', B.string)+1,LEN(B.string)),1,1,'') FROM (SELECT string=CONVERT(XML, '<root><v>'+replace(@string,',','</v><v>')+'</v></root>')) a OUTER APPLY (SELECT string = C.v.value('.','NVARCHAR(MAX)') FROM a.string.nodes('/root/v') C(v)) b ) SELECT CH = ch + RIGHT('00'+LTRIM((CAST(beginstr AS INT)+ISNULL(number,0))), 2) FROM cte a OUTER APPLY ( SELECT number FROM master..spt_values WHERE type = 'p' AND number <= CAST(a.endstr AS INT)-CAST(a.beginstr AS INT) ) b ORDER BY a.rowid /* CH A00 A03 A04 A05 A06 B00 C00 C01 C02 C03 */
[解决办法]
create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10)) returns @temp table(a varchar(100)) --实现split功能 的函数 --date :2003-10-14 as begin declare @i int set @SourceSql=rtrim(ltrim(@SourceSql)) set @i=charindex(@StrSeprate,@SourceSql) while @i>=1 begin insert @temp values(left(@SourceSql,@i-1)) set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i) set @i=charindex(@StrSeprate,@SourceSql) end if @SourceSql<>'' insert @temp values(@SourceSql) return end select * from dbo.f_split('1,2,3,4',',')