Created
July 3, 2024 17:17
-
-
Save MawCeron/b335f296e21ca642c93f7c7de280111d to your computer and use it in GitHub Desktop.
Split string function for SQL Server 2008
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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