Skip to content

Instantly share code, notes, and snippets.

@MawCeron
Created July 3, 2024 17:17
Show Gist options
  • Select an option

  • Save MawCeron/b335f296e21ca642c93f7c7de280111d to your computer and use it in GitHub Desktop.

Select an option

Save MawCeron/b335f296e21ca642c93f7c7de280111d to your computer and use it in GitHub Desktop.
Split string function for SQL Server 2008
CREATE FUNCTION dbo.SplitString(
@String varchar(8000),
@Delimiter char(1),
@Column int = 1
)
returns varchar(8000)
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return null
declare @ColCnt int
set @ColCnt = 1
while (@idx != 0)
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0 begin
if (@ColCnt = @Column) return left(@String,@idx - 1)
set @ColCnt = @ColCnt + 1
end
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return @String
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment