sql split string by delimiter

Galaxy Glossary

How can I split a string into multiple parts based on a delimiter in SQL?

SQL doesn't have a built-in string splitting function. This concept explores common workarounds to achieve this using string functions like SUBSTRING, CHARINDEX, and recursive CTEs.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

Splitting strings by delimiters is a common task in SQL, but unlike some programming languages, SQL doesn't have a direct 'split' function. To achieve this, we need to leverage string manipulation functions. One common approach involves using functions like `SUBSTRING` and `CHARINDEX` to extract substrings based on the delimiter's position. Another method, particularly useful for handling multiple splits, is using a recursive Common Table Expression (CTE). This approach iteratively extracts substrings until the delimiter is no longer found. This allows for more dynamic and flexible string splitting, especially when dealing with varying numbers of substrings within a single string.

Why sql split string by delimiter is important

Understanding string manipulation techniques like splitting is crucial for data cleaning and transformation tasks. It allows you to extract meaningful information from strings stored in your database, enabling more complex queries and data analysis.

Example Usage

```sql -- Using SUBSTRING and CHARINDEX DECLARE @inputString VARCHAR(100) = 'apple,banana,orange'; DECLARE @delimiter CHAR(1) = ','; DECLARE @startIndex INT = 1; DECLARE @endIndex INT; SELECT SUBSTRING(@inputString, @startIndex, CHARINDEX(@delimiter, @inputString, @startIndex) - @startIndex) AS Fruit FROM (VALUES (1)) AS x WHILE CHARINDEX(@delimiter, @inputString, @startIndex) > 0 BEGIN SET @endIndex = CHARINDEX(@delimiter, @inputString, @startIndex); SELECT SUBSTRING(@inputString, @startIndex, @endIndex - @startIndex) AS Fruit; SET @startIndex = @endIndex + 1; END; -- Using Recursive CTE WITH SplittedStrings AS ( SELECT 1 AS ID, @inputString AS String, CAST(CHARINDEX(@delimiter, @inputString) AS INT) AS DelimiterPosition, SUBSTRING(@inputString, 1, CHARINDEX(@delimiter, @inputString) - 1) AS Fruit WHERE CHARINDEX(@delimiter, @inputString) > 0 UNION ALL SELECT ID + 1, @inputString, CAST(CHARINDEX(@delimiter, @inputString, DelimiterPosition + 1) AS INT), SUBSTRING(@inputString, DelimiterPosition + 1, CHARINDEX(@delimiter, @inputString, DelimiterPosition + 1) - DelimiterPosition - 1) FROM SplittedStrings WHERE DelimiterPosition > 0 ) SELECT Fruit FROM SplittedStrings; ```

Common Mistakes

Want to learn about other SQL terms?