SDU Tools: Extracting tokens from CSV rows in T-SQL

There are a few things in IT that seem to rarely change. One is the use of CSV (comma-separated value) files. It's a pretty basic file format and sometimes we see other delimiters like tabs (aka TSV files) or pipe symbols but these types of files are still everywhere.

So it's hardly surprising that people want to work with them in T-SQL as well. In our free SDU Tools for developers and DBAs, we added two functions to help: NumberOfTokens and ExtractToken.

You can use the tools as a set or as an example of how to write the T-SQL required.

NumberOfTokens calculates the number of values (tokens) that are present in a string. It takes two parameters: the string that you want to tokenize, and the delimited to use. Because functions in T-SQL can't have optional parameters, you have to provide the delimiter value, even if it's a comma.

ExtractToken retrieves a specific value from the string. It takes four parameters: the string, the delimiter, the token number, and a bit that says if the output should be trimmed.

Find out more

You can see them in action in the main image above, and in the video here:

Access to SDU Tools is one of the benefits of being an SDU Insider, along with access to our other free tools and eBooks. Please just visit here for more info:


Leave a Reply

Your email address will not be published. Required fields are marked *