countrymeister
asked on
SQL Server Split function
is there a Sql split function
I have data in a column that is pipe delimited, example
Region|1339794638 |0
I need to get each value via sql query
I have data in a column that is pipe delimited, example
Region|1339794638 |0
I need to get each value via sql query
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ALTER FUNCTION [dbo].[fn_StringToTable] (@string VARCHAR(MAX), @delimiter CHAR(1))
RETURNS @output TABLE(data varchar(256))
BEGIN
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
WHILE @start < LEN(@string) + 1 BEGIN
IF @end = 0
SET @end = LEN(@string) + 1
INSERT INTO @output (data)
VALUES(SUBSTRING(@string, @start, @end - @start))
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)
END
RETURN
END
RETURNS @output TABLE(data varchar(256))
BEGIN
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
WHILE @start < LEN(@string) + 1 BEGIN
IF @end = 0
SET @end = LEN(@string) + 1
INSERT INTO @output (data)
VALUES(SUBSTRING(@string, @start, @end - @start))
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)
END
RETURN
END
Open in new window
Function:
Open in new window