T-SQL 實現 Split

來源:互聯網
上載者:User
 1 DECLARE @list VARCHAR(100), @cnt INT, @idxStart INT, @idxEnd INT 2 DECLARE @sites TABLE(id INT) 3   4 SET @list = '23, 34, 44, 56, 78'; 5 --SET @list = '23'; 6   7 SET @list = replace(@list, ' ', '') 8   9 IF(RIGHT(@list, 1)<>',')10       SET @list = @list + ','11  12 SET @cnt = len(replace(@list, ',', ',,'))-len(@list);13 SET @idxStart = 014 SET @idxEnd = 015  16 WHILE @cnt > 017 BEGIN18       SET @idxEnd = charindex(',', @list, @idxStart)19       INSERT INTO @sites VALUES(convert(INT, substring(@list, @idxStart, @idxEnd-@idxStart)));20      21       SET @idxStart = @idxEnd + 122       SET @cnt = @cnt - 123 END24  25 SELECT * FROM @sites;

其實很簡單, 應該也比較好理解. 網上包括園子裡還有N多其它的實現, 可以看看這個(T-SQL的Split)感覺也不錯的. 關鍵是想法.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.