In this article, I will demonstrate how to split a
string using Transact-SQL.
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:
Returns the starting position of the specified expression in a character
CHARINDEX ( expression1 , expression2 [ , start_location ] )
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
start_location is not given, is a negative number, or is zero, the search starts at the beginning of
SELECT CHARINDEX('wonderful', notes)
WHERE title_id = 'TC3218'
More details can be found at this link.
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.
SUBSTRING ( expression , start , length )
Expression: A character
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
Length: An integer that specifies the length of the
SUBSTRING (the number of characters or bytes to return).
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.
SELECT au_lname, SUBSTRING(au_fname, 1, 1)
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
SUBSTRING functions. A sample code example is given below:
set QUOTED_IDENTIFIER ON
CREATE FUNCTION [dbo].[SPLIT]
( @DELIMITER VARCHAR(5),
RETURNS @TABLEOFVALUES TABLE
( ROWID SMALLINT IDENTITY(1,1),
DECLARE @LENSTRING INT
WHILE LEN( @LIST ) > 0
SELECT @LENSTRING =
(CASE CHARINDEX( @DELIMITER, @LIST )
WHEN 0 THEN LEN( @LIST )
ELSE ( CHARINDEX( @DELIMITER, @LIST ) -1 )
INSERT INTO @TABLEOFVALUES
SELECT SUBSTRING( @LIST, 1, @LENSTRING )
SELECT @LIST =
(CASE ( LEN( @LIST ) - @LENSTRING )
WHEN 0 THEN ''
ELSE RIGHT( @LIST, LEN( @LIST ) - @LENSTRING - 1 )
Note: I have used some other functions like
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.
I hope that this article might be helpful to you. Enjoy!
A highly experienced leader with successful track record of brand management, product innovations and corporate communication. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by CodeProject.com and also selected as a mentor of CodeProject.com. Published over 100 technical articles in various software development resource sites (i.e,CodeProjetc.com, Microsoft, IEEE & IBM -In progress) and verious IT Forums, Blogs as well.
Over ten years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, services, data management and analytics, customer experience management, content services, digital analytics and optimization.
An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint. Believes in transparency, commitment and teamwork.
Specialties: Developing client-server with multi-tier application, database design, web based business software solutions, enterprise applications, google analytics, etc., for Windows / Mac using Microsoft SQL Server, Oracle, MySql, PS, Visual C#, R, VB.NET, ASP.NET, PHP, API, MVC, WebAPI , Add-In Visual Basic etc,. I have also more than two years’ of strong experience in mobile-VAS (platform development).
Points of Interest: Research and development where technologies used in business research areas, including (few stuff):
1. Urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc.
2. Biometric device integration with several platforms.
3. Social security services.
4. Eager to work with new technology.