SQL Server & PostgreSQL DBA & Dev Tips | MyTechMantra

STRING_SPLIT Function and STRING_ESCAPE Function in SQL Server 2016

SQL Server 2016 introduced STRING_SPLIT and STRING_ESCAPE to simplify string manipulation. While STRING_SPLIT replaces inefficient User Defined Functions (UDFs) for parsing delimited lists, STRING_ESCAPE provides a native way to sanitize data for JSON. This guide covers syntax, critical limitations like the missing ordinal column, and expert performance workarounds to help you master these essential T-SQL enhancements.

STRING_SPLIT and STRING_ESCAPE Function in SQL Server 2025?

STRING_SPLIT is a table-valued function introduced in SQL Server 2016 that divides a string into rows based on a character separator. It replaces complex XML and UDF methods, offering higher performance for processing delimited data. Use STRING_ESCAPE to sanitize text for JSON payloads, ensuring data integrity and security.

Series

You are reading Part 8 of the T-SQL Enhancements in SQL Server 2016 for Developers and DBAs. View All 10 Parts

Continue exploring T-SQL Enhancements in SQL Server 2016 for Developers and DBAs:

Introduction

For years, SQL Server developers relied on complex User Defined Functions (UDFs), XML hacks, or recursive CTEs just to split a simple comma-separated string. SQL Server 2016 finally addressed this pain point by introducing two native system functions: STRING_SPLIT and STRING_ESCAPE.

These functions aren’t just about cleaner code; they are built for performance. In this article, we will explore how to implement these functions and, more importantly, how to solve the common limitations found in the 2016 implementation.

The Prerequisites: Compatibility Level 130

Before you begin, there is a common “gotcha.” Even if you are running SQL Server 2016, you might receive an “Invalid object name” error. This is because STRING_SPLIT requires your database to be at Compatibility Level 130 or higher.

To check and update your level, use the following:

-- Check current level
SELECT name, compatibility_level FROM sys.databases WHERE name = 'MyTechMantra';

-- Update to SQL Server 2016 level
ALTER DATABASE MyTechMantra SET COMPATIBILITY_LEVEL = 130;

STRING_SPLIT Function

The STRING_SPLIT function is a table-valued function (TVF) that breaks a string into a single-column table.

Syntax

STRING_SPLIT (string , separator)

Example

SELECT value FROM STRING_SPLIT('MyTechMantra Helps DBAs and Developers Daily', ' ');

Output of STRING_SPLIT Function in SQL Server 2016

MyTechMantra
Helps
DBAs
and
Developers
Daily


The Expert Challenge: The “Missing Ordinal” Problem

In the SQL Server 2016 version of STRING_SPLIT, the output rows are not guaranteed to be in the same order as the input string. Furthermore, there is no “index” column.

The Workaround: If order matters, you can use OPENJSON as a clever alternative in SQL Server 2016 to get an index (key):

DECLARE @jsonInput NVARCHAR(MAX) = '["MyTechMantra","Helps","DBAs"]';
SELECT [key] AS [Index], [value] AS [Word] FROM OPENJSON(@jsonInput);

STRING_ESCAPE Function

While STRING_SPLIT handles incoming data, STRING_ESCAPE is designed for outgoing data—specifically for JSON. It prevents malformed JSON by escaping special characters like backslashes, quotes, and control characters.

Example: Sanitizing for JSON

SELECT STRING_ESCAPE('C:\MyTechMantra\Reports"2026"', 'json') AS EscapedPath;

Output of STRING_ESCAPE Function in SQL Server 2016

C:\\MyTechMantra\\Reports\"2026\"

This is critical when building dynamic JSON strings to prevent “JSON Injection” or parsing errors in your front-end applications.

Performance Comparison: Native vs. XML

Why should you migrate your old code to STRING_SPLIT?

Swipe left to view full metrics ↔️
Method Performance Complexity 2016 Native Support?
T-SQL Loop (UDF) Poor (High CPU) High No (Custom)
XML Method Moderate Very High No (Workaround)
STRING_SPLIT (Native) Excellent Very Low Yes
OPENJSON (Workaround) Very Good Low Yes



Conclusion

STRING_SPLIT and STRING_ESCAPE represent a major leap forward for T-SQL developers. By moving away from custom UDFs and XML parsing, you can significantly reduce the CPU overhead on your production servers.

Next Steps: Now that you’ve mastered string manipulation with native functions, join us for Part 9, where we explore the FORMATMESSAGE Statement. Learn how this enhancement simplifies custom message formatting and error handling in SQL Server 2016.

Read Next: Part 9: FORMATMESSAGE T-SQL Enhancement in SQL Server 2016

Frequently Asked Questions (FAQs)

1. Can STRING_SPLIT use a multi-character delimiter like ‘||’?

No, in SQL Server 2016, the separator must be a single character. For multi-character delimiters, you must still use the REPLACE workaround or a custom UDF.

2. Does STRING_SPLIT return a specific order?

No. The documentation explicitly states that the order is not guaranteed. If you need a specific order in 2016, consider the OPENJSON workaround.

3. What happens if the input string is NULL?

The function will return an empty table.

4. Can I use STRING_SPLIT in a JOIN?

Yes, it is most commonly used with CROSS APPLY to split values from a table column.

5. Does STRING_ESCAPE support XML escaping?

No, currently STRING_ESCAPE only supports the 'json' type.

6. Why am I getting “Invalid object name ‘STRING_SPLIT'”?

Check your database compatibility level. It must be 130 or higher.

7. Is STRING_SPLIT faster than XML splitting?

Yes, in most production benchmarks, the native C++ implementation of STRING_SPLIT outperforms XML-based splitting by 2x to 3x.

8. Can I filter the results of STRING_SPLIT?

Yes, since it returns a table, you can use a WHERE clause (e.g., WHERE value <> '' to remove empty entries).

9. Does STRING_ESCAPE handle emojis?

Yes, it correctly handles and escapes Unicode characters according to JSON standards.

Enjoyed this guide? Continue exploring the 2016 T-SQL Series:

Ashish Kumar Mehta

Ashish Kumar Mehta is a database manager, trainer and technical author. He has more than a decade of IT experience in database administration, performance tuning, database development and technical training on Microsoft SQL Server from SQL Server 2000 to SQL Server 2014. Ashish has authored more than 325 technical articles on SQL Server across leading SQL Server technology portals. Over the last few years, he has also developed and delivered many successful projects in database infrastructure; data warehouse and business intelligence; database migration; and upgrade projects for companies such as Hewlett-Packard, Microsoft, Cognizant and Centrica PLC, UK. He holds an engineering degree in computer science and industry standard certifications from Microsoft including MCITP Database Administrator 2005/2008, MCDBA SQL Server 2000 and MCTS .NET Framework 2.0 Web Applications.

AdBlocker Message

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.


Newsletter Signup! Join 15,000+ Professionals




Be Social! Like & Follow Us

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Advertisement