Split In Parts



-->

Split a string in equal parts (grouper in Python) Python Server Side Programming Programming In this tutorial, we are going to write a program that splits the given string into equal parts. Split is used to split a string variable into two or more component parts, for example, “words”. You might need to correct a mistake, or the string variable might be a genuine composite that you wish to subdivide before doing more analysis. The basic steps applied by split are, given one or more separators, to find those separators. Use the Split feature to create multiple parts from an existing part. You can create separate part files, and form an assembly from the new parts. Assignment of Split or Saved Bodies Features. When you edit split or saved bodies features, you can assign bodies by selecting from a list of derived parts, or by assigning them to other stored part.

APPLIES TO: SQL Server 2016 and later Azure SQL Database Azure Synapse Analytics Parallel Data Warehouse

A table-valued function that splits a string into rows of substrings, based on a specified separator character.

Compatibility level 130

STRING_SPLIT requires the compatibility level to be at least 130. When the level is less than 130, SQL Server is unable to find the STRING_SPLIT function.

Kolor vr player. To change the compatibility level of a database, refer to View or Change the Compatibility Level of a Database.

Split In Parts String

Note

Compatibility configuration is not needed for STRING_SPLIT in Azure Synapse Analytics.

Syntax

Note

To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.

Arguments

string
Is an expression of any character type (for example, nvarchar, varchar, nchar, or char).

separator
Is a single character expression of any character type (for example, nvarchar(1), varchar(1), nchar(1), or char(1)) that is used as separator for concatenated substrings.

Return Types

Returns a single-column table whose rows are the substrings. The name of the column is value. Returns nvarchar if any of the input arguments are either nvarchar or nchar. Otherwise returns varchar. The length of the return type is the same as the length of the string argument.

Remarks

STRING_SPLIT inputs a string that has delimited substrings, and inputs one character to use as the delimiter or separator. STRING_SPLIT outputs a single-column table whose rows contain the substrings. The name of the output column is value.

The output rows might be in any order. The order is not guaranteed to match the order of the substrings in the input string. You can override the final sort order by using an ORDER BY clause on the SELECT statement (ORDER BY value).

0x0000 (char(0)) is an undefined character in Windows collations and cannot be included in STRING_SPLIT.

Empty zero-length substrings are present when the input string contains two or more consecutive occurrences of the delimiter character. Empty substrings are treated the same as are plain substrings. You can filter out any rows that contain the empty substring by using the WHERE clause (WHERE value <> '). If the input string is NULL, the STRING_SPLIT table-valued function returns an empty table.

As an example, the following SELECT statement uses the space character as the separator:

In a practice run, the preceding SELECT returned following result table:

value
Lorem
ipsum
dolor
sit
amet.

Brain Split In Parts

Examples

A. Split comma-separated value string

Parse a comma-separated list of values and return all non-empty tokens:

STRING_SPLIT will return empty string if there is nothing between separator. Condition RTRIM(value) <> ' will remove empty tokens.

B. Split comma-separated value string in a column

Product table has a column with comma-separate list of tags shown in the following example:

ProductIdNameTags
1Full-Finger Glovesclothing,road,touring,bike
2LL Headsetbike
3HL Mountain Framebike,mountain

Following query transforms each list of tags and joins them with the original row:

Here is the result set.

ProductIdNamevalue
1Full-Finger Glovesclothing
1Full-Finger Glovesroad
1Full-Finger Glovestouring
1Full-Finger Glovesbike
2LL Headsetbike
3HL Mountain Framebike
3HL Mountain Framemountain

Note

The order of the output may vary as the order is not guaranteed to match the order of the substrings in the input string.

C. Aggregation by values

Users must create a report that shows the number of products per each tag, ordered by number of products, and to filter only the tags with more than two products.

D. Search by tag value

Developers must create queries that find articles by keywords. They can use following queries:

Split In Parts

To find products with a single tag (clothing):

Find products with two specified tags (clothing and road):

E. Find rows by list of values

Developers must create a query that finds articles by a list of IDs. They can use following query:

Snake Flag Split In Parts

The preceding STRING_SPLIT usage is a replacement for a common anti-pattern. Such an anti-pattern can involve the creation of a dynamic SQL string in the application layer or in Transact-SQL. Or an anti-pattern can be achieved by using the LIKE operator. See the following example SELECT statement:

Split In Past Form

See Also