66

The new STRING_SPLIT method is not available in my Azure SQL database. I had already ran ALTER DATABASE [DatabaseName] SET COMPATIBILITY_LEVEL = 130 a couple days ago, and I have verified the compatibility level is indeed set to 130. SELECT database_id, name, compatibility_level FROM sys.databases

Has anyone else been able to use the new method, and if so, did you need to do anything else to get it working?

3 Answers 3

136

It was a syntax error. I was attempting to use the function as scalar and not as table valued.

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

2
  • 5
    How would I split a string in one of my columns though? That is where the use is rather than substring indexing it and manipulating the string.
    – ThinkCode
    Jun 6, 2016 at 18:26
  • 19
    @ThinkCode you need to use 'APPLY' e.g. something a little like this maybe SELECT t.SomeColumn, s.value FROM dbo.MyTable t CROSS APPLY STRING_SPLIT(t.SomeColumn, ',') s
    – padigan
    Jan 29, 2018 at 11:34
33

The STRING_SPLIT function is available at compatibility level 130 or higher. If your database compatibility level is lower than 130, SQL Server will not be able to find and execute STRING_SPLIT function. You can change a compatibility level of database using the following command:

ALTER DATABASE DatabaseName SET COMPATIBILITY_LEVEL = 130

EDIT: Note that compatibility level 150 might be default even in new Azure SQL Databases.

For reference:

Version - Highest Compatibility Level - Lowest Available Level

  • Azure SQL Database - 160 - 100
  • Azure SQL Managed Instance - 160 - 100
  • SQL 2022 - 160 - 100
  • SQL 2019 - 150 - 100
  • SQL 2017 - 140 - 100
  • SQL 2016 - 130 - 100
  • SQL 2014 - 120 - 100
  • SQL 2012 - 110 - 90
  • SQL 2008 - 100 - 80
  • SQL 2005 - 90 - 80
  • SQL 2000 - 80 - 80
2
  • I tried to execute above query, but getting this message: Msg 15048, Level 16, State 1, Line 3 Valid values of the database compatibility level are 100, 110, or 120. May 16, 2017 at 14:25
  • @Prateek-Systematix Are you using SQL Server 2014? See my edit May 16, 2017 at 14:38
19

The string_split function must be placed after the FROM clause. Either as a table value (as in the OP's answer) OR use the following to split each string in a column by "," (credit comment from @padigan):

SELECT * 
FROM infoTable
CROSS APPLY STRING_SPLIT(infoTable.myStrings, ',') 

I am unsure how to name the resulting column and it takes the default name value

1
  • 1
    For me simply using [VALUE] AS [categoryCodes] allows me to rename the resulting column to whatever I like and that is in-line with the code that you have, you would just need to write out all columns as opposed to using the asterisk. Nov 22, 2020 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.