You are on page 1of 2

Technical

Notes

Purpose

The purpose of this document is to provide a step-by-step guide on how to split_part


arrays on t-sql.

How To Split_Part Arrays On T-SQL

Note: This function uses the ids from the shift_activities table; the shift_activities table
must be populated for this function to work.

Function:

Dbo.fn_DelimitToArray

Parameters:

@String(varchar(8000), No default)
@Delimiter(varchar(1), No default)

Examples:

select * from dbo.Fn_delimittoarray('Delimit,To,Array',',');

Pos | Value
--------------
1 | Delimit
2 | To
3 | Array

select * from dbo.Fn_delimittoarray((select rotations from rotations),',') order by Pos;

Pos | Value
--------------
1 |364
2 |365
3 |364
4 |365
5 |364
6 |365
7 |364
8 |365

1
Technical
Notes

Code:

--
-- fn_DelimitToArray
--
-- Created by Daniel Soto on 2009-08-11
-- Copyright (c) 2009 Jigsaw Technologies. All rights reserved.
--
--

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[fn_DelimitToArray]


(
@String VarChar(8000),
@Delimiter VarChar(1)
) RETURNS TABLE
AS

RETURN
(
SELECT
i - LEN(REPLACE(LEFT(@String, i-1), @Delimiter, '')) AS Pos,
SUBSTRING(@String+@Delimiter, i, CHARINDEX(@Delimiter,
@String+@Delimiter, i)-i) AS Value
FROM (select id as i from shift_activities) consecutive
WHERE i >0 AND i<LEN(@String)+LEN(@Delimiter) AND
SubString(@Delimiter + @String + @Delimiter, i, 1)=@Delimiter
)

You might also like