Tokenizing a String Using PARSENAME

This article describes an alternative and rather interesting way of doing string tokenizing in T-SQL.

The process of splitting a string to its tokens using a pre-defined separator is a subject discussed in many cases. The Java and C languages have this facility built in the language definition. And I and others, in previous articles have shown the T-SQL classic implementation of tokenizing a string by "cutting" it to its pieces according to the separator.

I bring here another implementation of tokenizing using the PARSENAME function. We reach the Nth -1 position of the separator in the given string and then we replace the Nth position of the separator with a '.' character. Then we return the activate the PARSENAME T-SQL function with value = 2. SQL Server is fooled to believe that this is a valid SQL "object" and returns the desired token string as output.

Conclusion

The function I presented here can be used as a general tool for tokenizing strings as shown.

Eli Leiba works at Israel Electric Company as a Senior Application DBA in Oracle and MS SQL Server. He also has certifications from Microsoft and BrainBench in Oracle and SQL Server database administration and implementation. Mr. Leiba holds a B.S. in Computer Science since 1991 and has 13 years' experience working in the databases field. Additionally Mr. Leiba teaches SQL Server DBA and Development courses at Microsoft CTEC and also serves as a senior database consultant for several Israeli start-up companies. (e-mail: iecdba@hotmail.com)

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.