Figure: Output
Introduction
In this article, I will demonstrate how to split a string
using Transact-SQL.
Background
String
manipulation is pretty interesting for most software developers. To split a string
by using a user defined delimiter is nothing new for programmers. Even Microsoft .NET Frameworks provide us a huge "Standard Techniques" for doing this. But if we want to split a string
using Transact -SQL, then how can we achieve this?
Using the Code
It is very simple to use. We just need some basic concepts on the following:
CHARINDEX
Returns the starting position of the specified expression in a character string
.
Syntax
CHARINDEX ( expression1 , expression2 [ , start_location ] )
Arguments
expression1
: An expression containing the sequence of characters to be found. expression1
is an expression of the short character data type category.
expression2
: An expression, usually a column searched for the specified sequence. expression2
is of the character string
data type category.
start_location
: The character position to start searching for expression1
in expression2
. If start_location
is not given, is a negative number, or is zero, the search starts at the beginning of expression2
.
Return Types: int
Example
USE pubs
GO
SELECT CHARINDEX('wonderful', notes)
FROM titles
WHERE title_id = 'TC3218'
GO
More details can be found at this link.
SUBSTRING
Returns part of a character, binary, text, or image expression for more information about the valid Microsoft® SQL Server™ data types that can be used with this function.
Syntax
SUBSTRING ( expression , start , length )
Arguments
Expression
: A character string
, binary string
, text, image, a column, or an expression that includes a column. Do not use expressions that include aggregate functions.
Start
: An integer that specifies where the SUBSTRING
begins.
Length
: An integer that specifies the length of the SUBSTRING
(the number of characters or bytes to return).
Note: Because start
and length
specify the number of bytes when SUBSTRING
is used on text data, DBCS data, such as Kanji, may result in split characters at the beginning or end of the result. This behavior is consistent with the way in which READTEXT
handles DBCS. However, because of the occasional strange result, it is advisable to use ntext
instead of text
for DBCS characters.
Return Types: Returns character data if expression is one of the supported character data types. Returns binary data if expression is one of the supported binary data types.
Example
USE pubs
SELECT au_lname, SUBSTRING(au_fname, 1, 1)
FROM authors
ORDER BY au_lname
More details can be found at this link.
I wrote a simple function named SPLITE
which will split an expression using CHARINDEX
and SUBSTRING
functions. A sample code example is given below:
Sample Example
set QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[SPLIT]
( @DELIMITER VARCHAR(5),
@LIST VARCHAR(MAX)
)
RETURNS @TABLEOFVALUES TABLE
( ROWID SMALLINT IDENTITY(1,1),
[VALUE] VARCHAR(MAX)
)
AS
BEGIN
DECLARE @LENSTRING INT
WHILE LEN( @LIST ) > 0
BEGIN
SELECT @LENSTRING =
(CASE CHARINDEX( @DELIMITER, @LIST )
WHEN 0 THEN LEN( @LIST )
ELSE ( CHARINDEX( @DELIMITER, @LIST ) -1 )
END
)
INSERT INTO @TABLEOFVALUES
SELECT SUBSTRING( @LIST, 1, @LENSTRING )
SELECT @LIST =
(CASE ( LEN( @LIST ) - @LENSTRING )
WHEN 0 THEN ''
ELSE RIGHT( @LIST, LEN( @LIST ) - @LENSTRING - 1 )
END
)
END
RETURN
END
Note: I have used some other functions like LNE()
, LEFT()
, etc. which are very common and I hope that everyone is very much familiar with this. Hence, I did not include this. Actually I do not want to lose focus on the main objective of this article.
Conclusion
I hope that this article might be helpful to you. Enjoy!
History