The Fn_Split() and CamelCase() Functions

Downloads
24006.zip

Burton Roberts' SQL Server Magazine article "Treat Yourself to Fn_Split()" (July 2001, InstantDoc ID 21071) reminded me of two user-defined functions (UDFs) I use regularly. Roberts' fn_Split() UDF emulates the Visual Basic (VB) Split() function and lets developers incorporate variable-length parameter arrays into their T-SQL programming. The first of my UDFs, ReturnVarcharTableFromList(), is a simpler version of the fn_Split() function. This UDF, which Listing 1 shows, returns a one-column table of values by parsing a string into individual words. The following command returns a result set containing four rows:

SELECT  keyword
FROM    dbo.fn_ReturnVarcharTableFromList
('This Is A List', 1)

This UDF isn't remarkable, but it leads me to the second UDF that I use: CamelCaseToSpace(). This UDF, which Listing 2 shows, parses input into individual words. Issuing the following command returns one column that has the value Firstname Lastname.

SELECT  dbo.fn_CamelCaseToSpace
('FirstnameLastname')

Data transformations are often difficult and nearly always involve special parsing and translation logic. UDFs are perfect for solving such challenges. Using multiple functions in the same command demonstrates some real possibilities of UDFs. For example, the following command returns the result set that Figure 1 shows:

SELECT dbo.fn_CamelCaseToSpace(keyword)
FROM dbo.fn_ReturnVarcharTableFromList
('SQLServer2000EnterpriseCosts$20000Per
CPU DotNETStuff FirstnameLastname', 1)

You might also use these functions to avoid dynamic SQL inside stored procedures. Imagine a stored procedure that accepts a single parameter that contains a list of values to search for. SQL Server doesn't support the syntax WHERE column IN @parameter but does support the syntax WHERE column IN (select keyword from dbo.fn_ReturnVarcharTableFromList(@parameter, 1)).

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Mike Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
Vision road sign
May 6, 2013
blog

Cheaters Never Win, Even in TPC Benchmarks

In this portion of the series on database benchmarking, I want to tell you about one of my favorite aspects of the TPC benchmarks – CHEATING....More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.