STRING_SPLIT Function

SQL Server introduced the STRING_SPLIT function to directly split delimited strings into a table of values. It takes the input string and delimiter as parameters, returning a table with individual items. This method is efficient and straightforward, ideal for modern SQL Server environments.

Example: Splitting Delimited Strings into Individual Values in SQL

-- Create a sample table with a column containing delimited strings
CREATE TABLE SampleData (
ID INT,
Data VARCHAR(100)
);

-- Insert some sample data
INSERT INTO SampleData (ID, Data)
VALUES
(1, 'apple,banana,orange'),
(2, 'carrot,potato,tomato');

-- Use SUBSTRING_INDEX to split the delimited strings into individual values
SELECT ID,
SUBSTRING_INDEX(SUBSTRING_INDEX(Data, ',', n.n), ',', -1) AS SplitData
FROM SampleData
JOIN (
SELECT 1 n UNION ALL
SELECT 2 UNION ALL
SELECT 3 -- Add more if needed based on maximum elements in the list
) n ON LENGTH(Data) - LENGTH(REPLACE(Data, ',', '')) >= n.n - 1
ORDER BY ID, SplitData;

Output:

Using “STRING_SPLIT”

  • Uses the STRING_SPLIT function to split the delimited strings into individual values.
  • Orders the output by ID and SplitData for clarity.
  • Each row includes the original ID value and the corresponding split data (SplitData).
  • The output is sorted by ID and SplitData for easier interpretation.
  • Demonstrates how STRING_SPLIT converts delimited strings into individual values, facilitating data processing and analysis.

How to Split a Delimited String to Access Individual Items in SQL?

In SQL, dealing with delimited strings is a common task, especially when handling data that are not structured in a traditional tabular format. Whether it’s parsing a list of values separated by commas or any other delimiter, splitting these strings into individual items is crucial for various data manipulation tasks.

In SQL, sometimes we get data that’s all squished together, like a bunch of words separated by commas or other symbols. This article is all about learning how to do just that—take a long string of text and break it into pieces we can easily work with.

Similar Reads

Splitting Delimited Strings in SQL

A delimited string is a single string containing multiple values separated by a specific character or sequence of characters. Common delimiters include commas (‘,’), semicolons (‘;’), tabs (‘\t’), or any custom character. For example, Imagine having a list of fruits like “apple,banana,cherry” and you want to look at each fruit one by one....

1. String Functions

SUBSTRING and LOCATE: This method involves SQL string manipulation functions to extract individual items from a delimited string. The approach is quite versatile but can become complex and less efficient for strings with varying item lengths or for very long strings....

2. Recursive CTE (Common Table Expression)

Recursive CTE is a more elegant solution for splitting delimited strings recursively. It involves recursively breaking down the string until all individual items are extracted. Although efficient, it might not be supported in all SQL environments and can be resource-intensive for large datasets....

3. STRING_SPLIT Function

SQL Server introduced the STRING_SPLIT function to directly split delimited strings into a table of values. It takes the input string and delimiter as parameters, returning a table with individual items. This method is efficient and straightforward, ideal for modern SQL Server environments....

Conclusion

Splitting delimited strings in SQL is a fundamental task in data manipulation and analysis. Understanding various methods, including built-in functions like STRING_SPLIT and recursive CTEs, empowers SQL developers to efficiently access individual items within delimited strings. While newer SQL versions offer dedicated functions for this purpose, legacy systems may require alternative approaches such as custom functions or string manipulation techniques....

Contact Us