Click here to monitor SSC
SQLServerCentral is supported by Redgate
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: 1772 | Views in the last 30 days: 1
Related Articles

Function to find non-printable characters

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


character limitations on sp name, & function name

character limitations on sp name, & function name


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


Charpad - Character Padding Function

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