Created
February 8, 2021 13:16
-
-
Save grishin/c32a05bd6f3d6b71b50386ce4a24b6ff to your computer and use it in GitHub Desktop.
STRING_SPLIT function in T-SQL with order
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
| ALTER FUNCTION [dbo].[fn_StringSplit] | |
| ( | |
| @Str varchar(MAX), | |
| @Delimeter char(1), | |
| @Position int | |
| ) | |
| RETURNS varchar(MAX) | |
| AS | |
| BEGIN | |
| DECLARE @T TABLE(Id int NOT NULL identity(1,1), [Value] varchar(MAX)); | |
| INSERT INTO @T([Value]) | |
| SELECT [Value] | |
| FROM STRING_SPLIT(RTRIM(LTRIM(@Str)), @Delimeter); | |
| DECLARE @ValOnPosition varchar(MAX); | |
| SELECT @ValOnPosition = [Value] | |
| FROM @T | |
| WHERE Id = @Position; | |
| RETURN @ValOnPosition; | |
| END | |
| -- Usage: | |
| -- SELECT dbo.fn_StringSplit([Email], '@', 2) AS EmailDomain FROM EmailsTable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment