SQL Server字符串拆分函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server字符串拆分函数相关的知识,希望对你有一定的参考价值。
From StackOverflow. I'm still not quite happy with my understanding of it, but it looks an order of magnitude more elegant than most I've found.
CREATE FUNCTION dbo.Split (@sep CHAR(1), @s VARCHAR(512)) RETURNS TABLE AS RETURN ( WITH Pieces(pn, START, stop) AS ( SELECT 1, 1, CHARINDEX(@sep, @s) UNION ALL SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1) FROM Pieces WHERE stop > 0 ) SELECT pn, SUBSTRING(@s, START, CASE WHEN stop > 0 THEN stop-START ELSE 512 END) AS s FROM Pieces ) --Here's a simpler demo of the CTE Recursion with the string parsing removed for clarity: --Part before the UNION is called the Anchor expression. BEGIN WITH Pieces(START, stop) AS ( SELECT 2, 2 UNION ALL SELECT START + 1, stop + 1 FROM Pieces WHERE stop < 5 ) SELECT START, stop FROM Pieces END --Returns --start stop --2 2 --3 3 --4 4 --5 5
以上是关于SQL Server字符串拆分函数的主要内容,如果未能解决你的问题,请参考以下文章
在 SQL Server 2008 的拆分函数中使用连接表列
MS SQL Server 在空格上拆分单词,但仅在不在双引号中时