Tuesday, November 2, 2010

T-SQL - Split Long String into Several Small Strings

This is a fantastic little function:

CREATE FUNCTION dbo.Split (@sep char(1), @s varchar(8000))
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 8000 END) AS s
FROM Pieces
)

I found it here:

Stack Overflow

and here:

SQLTeam

1 comment:

  1. An equipment glitch brought down the gift page usefulness, back-end announcing apparatuses, and the sparkling new constant pioneer sheets and information representation, across the board singular motion.jogos friv gratis 2019
    Jogos 2019
    jogos friv

    ReplyDelete