Click here to Skip to main content
980 members
Click here to Skip to main content
Add your own
alternative version


Posted 12 Apr 2012
Licenced CPOL

An Easy But Effective Way to Split a String using Transact-SQL

, 12 Apr 2012
Rate this:
Please Sign up or sign in to vote.
This article will demonstrate how to split a string using Transact-SQL.

Figure: Output


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 string.

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 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

USE pubs
SELECT CHARINDEX('wonderful', notes)
FROM titles
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, 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.

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
-- =============================================
-- Author: Md. Marufuzzaman
-- Create date: 
-- Description: Split an expression. 
-- Note: If you are using SQL Server 2000, You need to change the 
-- length (MAX) to your maximum expression length of each datatype.
-- =============================================
SELECT * FROM [dbo].[SPLIT] (';','I love codeProject;!!!;Your development resources')
      @LIST      VARCHAR(MAX) 
         [VALUE] VARCHAR(MAX) 
      WHILE LEN( @LIST ) > 0 
            SELECT @LENSTRING = 
               (CASE CHARINDEX( @DELIMITER, @LIST ) 
                   WHEN 0 THEN LEN( @LIST ) 
                   ELSE ( CHARINDEX( @DELIMITER, @LIST ) -1 )
            SELECT @LIST = 
               (CASE ( LEN( @LIST ) - @LENSTRING ) 
                   WHEN 0 THEN '' 
                   ELSE RIGHT( @LIST, LEN( @LIST ) - @LENSTRING - 1 ) 

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.


I hope that this article might be helpful to you. Enjoy!


  • 9th August 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Md. Marufuzzaman
Bangladesh Bangladesh
A well experienced leader with successful track record of software development, product innovations, brand management and corporate communication etc. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by and also selected as a mentor of Published over 100 technical articles in various software development resource sites (i.e.,, Microsoft MSDN, and IEEE & IBM (In progress)) and various IT Forums, Blogs etc.

Over fourteen years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, development, services, data management and analytics, customer experience management, content services, digital analytics and optimization.I have also more than two years’ of strong experience in mobile-VAS (platform development).

An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint.Honest, believes in transparency, commitment and teamwork.

Expertise: Software/Solution Architect, Technical Research, MIS, Data Analytics, Data Mining, BI, SaaS platform base application development, Large scale Win32 Form/Web based business software solutions, Security, Enterprise applications development, integration, etc.

Technologies/Tools: Microsoft.Net, Microsoft SQL Server , Oracle, MySQL, ETL, Visual C#, VB.NET, ASP.NET, , Python, Java, API, MVC, Cloud Computing, SaaS, Open FaaS, AWS,AWS Lambda, MS Azure, WebAPI , WPF, WCF, PHP, Microsoft Power BI, SPSS, PS2, R, Add-In, Visual Basic etc.

.Net UI component: Telerik, DevExpress, Ext.Net etc.
Scripting language: JavaScript, AngularJS, node.JS etc.
Source control / Subversion: Git, Smart SVN, Assembla etc.
Development methodologies: Agile,RAD etc.
Project Management / Issues Tracking Tools: JIRA, Trello, Slack, Clockingit etc.

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
RA-Web1 | 2.8.190517.1 | Last Updated 12 Apr 2012
Article Copyright 2012 by Md. Marufuzzaman
Everything else Copyright © RootAdmin, 1999-2019
Layout: fixed | fluid