Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

t-sql function to parse string data

By kmlakov,

The below function can be used to parse or split out data from a delimited string. The function takes in 3 parameters. The text you want to parse, the 1 character delimiter, and the total count of objects in the text.

Text for function is set at size of varchar(2000), feel free to make bigger if you wish. Also, delimiter is set to be 1 character, you may use any character you wish.

When calling function make sure to pass objects in that order as in below example. The function will then return a table datatype of size varchar(300) breaking out all objects in text. Again you may make this bigger as well.

Lastly, make sure when passing in text to function you end your text with delimiter.

Example of call to function, call assumes function resides in master database. delimiter is a comma, total records = 6.

SELECT * FROM [master].[dbo].[parsestring]('abcd,efg,hij,klm,no,pq,',',',6)


Total article views: 1757 | Views in the last 30 days: 5
 
Related Articles
SCRIPT

Function to find non-printable characters

The function is used to find non-printable ASCII characters in an input string.

FORUM

character limitations on sp name, & function name

character limitations on sp name, & function name

SCRIPT

Charpad - Character Padding Function

A generic character padding function. Useful for situations where you need fixed width formatting e....

SCRIPT

Using a function to parse/return the Nth field of a delimited column

A useful SQL function to parse a single string with delimited values for the "N"th occurrence

 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones