Proper Usage of STRING_SPLIT Function in Azure SQL Database and Compatibility Level Analysis

Dec 07, 2025 · Programming · 9 views · 7.8

Keywords: STRING_SPLIT | Azure SQL Database | Compatibility Level | Table-Valued Function | HTML Escaping

Abstract: This article provides an in-depth exploration of the correct syntax for using the STRING_SPLIT table-valued function in SQL Server, analyzing common causes of the 'is not a recognized built-in function name' error. By comparing incorrect usage with proper syntax, it explains the fundamental differences between table-valued and scalar functions. The article systematically examines the compatibility level mechanism in Azure SQL Database, presenting compatibility level correspondences from SQL 2000 to SQL 2022 to help developers fully understand the technical context of function availability. It also discusses the essential differences between HTML tags like <br> and character \n, ensuring code examples are correctly parsed in various environments.

Basic Concepts and Common Errors of STRING_SPLIT Function

In SQL Server 2016 and later versions, Microsoft introduced the STRING_SPLIT function, a powerful string processing tool that splits strings into multiple substrings based on specified delimiters. However, many developers frequently encounter the "'STRING_SPLIT' is not a recognized built-in function name" error when first using it. This error typically stems from two main causes: improper function syntax usage or incorrect database compatibility level settings.

Correct Syntax: Core Characteristics of Table-Valued Functions

According to the best answer analysis, the most common error is treating STRING_SPLIT as a scalar function. In reality, STRING_SPLIT is a table-valued function, meaning it returns a result set (table) rather than a single value. Understanding this distinction is crucial for proper function usage.

Incorrect usage example (scalar function approach):

-- This is incorrect usage
DECLARE @result VARCHAR(MAX);
SET @result = STRING_SPLIT('Lorem ipsum dolor sit amet.', ' ');

Correct usage example (table-valued function approach):

-- Correct syntax
SELECT Value 
FROM STRING_SPLIT('Lorem ipsum dolor sit amet.', ' ');

In this correct example, the STRING_SPLIT function returns a result set containing a Value column, with each row representing a substring from the original split string. Developers need to use this function like querying a regular table, typically through the FROM clause or APPLY operator.

In-depth Analysis of Compatibility Levels

Even with correct syntax, the STRING_SPLIT function remains unavailable if the database compatibility level is improperly set. This function is only available at compatibility level 130 (corresponding to SQL Server 2016) or higher. Compatibility levels determine the database engine's behavior patterns, including available functions, syntax support, and query optimizer characteristics.

Check current database compatibility level:

SELECT 
    database_id, 
    name, 
    compatibility_level 
FROM sys.databases
WHERE name = DB_NAME();

Set compatibility level to 130:

ALTER DATABASE [DatabaseName] 
SET COMPATIBILITY_LEVEL = 130;

Compatibility Level Correspondences Across SQL Server Versions

Understanding compatibility level correspondences across different SQL Server versions is crucial for cross-version development and migration:

For Azure SQL Database, the situation differs slightly. Newly created Azure SQL databases may default to compatibility level 150, but developers can adjust it to 130 to use the STRING_SPLIT function. Azure SQL Database supports compatibility levels from 100 to 160, providing flexible compatibility support for applications across different versions.

Practical Application Scenarios and Best Practices

The STRING_SPLIT function has various application scenarios in actual development. The following complex example demonstrates how to associate function results with other tables in queries:

-- Example: Processing comma-separated ID lists
DECLARE @idList VARCHAR(MAX) = '1,2,3,4,5';

SELECT 
    p.ProductID,
    p.ProductName,
    p.UnitPrice
FROM Products p
INNER JOIN STRING_SPLIT(@idList, ',') s
    ON p.ProductID = TRY_CAST(s.Value AS INT)
WHERE s.Value <> '';

In this example, we first use STRING_SPLIT to split the comma-separated string into multiple values, then associate these values with the products table through INNER JOIN. Note the use of the TRY_CAST function for type conversion and the WHERE condition for filtering empty values.

Importance of HTML Escaping in Code Examples

When correctly displaying code examples in technical documentation, HTML escaping is a critical consideration. For instance, when we need to discuss HTML tags themselves in articles, we must escape tag characters to prevent browsers from parsing them as actual HTML elements.

Consider this scenario: if we need to explain "the <br> tag is used here" in code comments, writing <br> directly would cause the browser to parse it as a line break tag. The correct approach is to use HTML entities:

-- Incorrect: This would be parsed as an HTML tag
-- The <br> tag is used here for line breaking

-- Correct: Using HTML entity escaping
-- The &lt;br&gt; tag is used here for line breaking

Similarly, in SQL strings containing HTML tags as data content, appropriate escaping is necessary:

-- Storing text containing HTML tags
INSERT INTO Articles (Content) 
VALUES ('This article discusses the difference between &lt;br&gt; tags and \n characters.');

This escaping ensures code examples display correctly in various environments, avoiding display issues or security vulnerabilities caused by special character parsing errors.

Summary and Recommendations

Proper use of the STRING_SPLIT function requires attention to both syntax correctness and environmental compatibility. Developers should:

  1. Always use STRING_SPLIT as a table-valued function, calling it through the FROM clause
  2. Verify compatibility level reaches 130 or higher in Azure SQL Database
  3. Understand compatibility level support ranges across different SQL Server versions
  4. Correctly use HTML escaping in code examples and documentation to ensure accurate technical communication

By following these best practices, developers can fully leverage the powerful capabilities of the STRING_SPLIT function while avoiding common errors and compatibility issues. As SQL Server versions continue to update, developers are advised to regularly check database compatibility settings to ensure access to the latest language features and performance optimizations.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.