53

In SQL Server 2016 I receive this error with STRING_SPLIT function

SELECT * FROM STRING_SPLIT('a,b,c',',')

Error:

Invalid object name 'STRING_SPLIT'.

gotqn
  • 42,737
  • 46
  • 157
  • 243
Victor Hugo Terceros
  • 2,969
  • 3
  • 18
  • 31

5 Answers5

85

Make sure that the database compatibility level is 130

you can use the following query to change it:

ALTER DATABASE [DatabaseName] SET COMPATIBILITY_LEVEL = 130

As mentioned in the comments, you can check the current compatibility level of a database using the following command:

SELECT compatibility_level FROM sys.databases WHERE name = 'Your-Database-Name';
Yahfoufi
  • 2,220
  • 1
  • 22
  • 41
  • 33
    "Simple, just change the gravitational constant of the universe" (permissions denied) – RJB Aug 08 '18 at 22:58
  • what if my sql server justo allow me 100,90 & 110 whitchshould i use? – E.Rawrdríguez.Ophanim Aug 30 '18 at 23:58
  • 3
    @E.Rawrdríguez.Ophanim then you cannot use `STRING_SPLIT` function. You can build your own function. check this link https://stackoverflow.com/questions/10914576/t-sql-split-string – Yahfoufi Sep 03 '18 at 11:09
  • 3
    Check your database compatibility by `SELECT compatibility_level FROM sys.databases WHERE name = 'Your-Database-Name';` – Muhammad Musavi May 21 '19 at 07:36
  • My mistake was to add single quote around my database name, e.g. `['DatabaseName']`, and you'll get error message "User does not have permission to alter database". Remember **DO NOT** add the single quote. Correct way: `[DatabaseName]` and you will be able to set the compatibility level – Coder Dev Sep 24 '21 at 05:05
11

I was using Split_string, which felt grammatically correct in English, but my old eyes didn't see that it should be STRING_SPLIT ... so, if you're a bit mentally challenged like me, then check you've typed the command the right way! :)

Muhammad Musavi
  • 2,512
  • 2
  • 22
  • 35
Toad
  • 348
  • 2
  • 12
4

If you can't change the COMPATIBILITY_LEVEL of the database you are working in, you can try looking at other databases on the same server to find one with a higher COMPATIBILITY_LEVEL. I found that the "master" database on my target server was at COMPATIBILITY_LEVEL = 140, so I connected to that database, and then executed my query, which actually ran against (fully qualified) tables in other databases on the server whose COMPATIBILITY_LEVEL was less than 130. It worked! Just query the various databases on your server with

SELECT compatibility_level  FROM sys.databases WHERE name = '<database_name>'; 

to find one that is >= 130.

tvarney
  • 41
  • 1
1

You need to change the Compatibility Level of your Database. to do so try below Query and Change you compatibility level to 150.

ALTER DATABASE [Database_Name] SET COMPATIBILITY_LEVEL = 150

  • 4
    SQL Server 2016 does not have compatibility level 150. It is limited to 130 there and Yahfoufi answer is correct – Alexander Volok Jan 13 '21 at 08:48
  • You should change the compatibility level value to 140 if you want to set your database compatibility to SQL Server 2017. – tanuk Mar 17 '23 at 13:07
0

I know that some big companies and their developers will be very careful about altering database since it would be total a chaos. Here I spent 30 minutes to get this done. This can be used with both old and new version of SQL Servers and their databases. I know it is a big query but gets the job done without problem.

DECLARE @String varchar(1024)
DECLARE @Pos int
DECLARE @Keywords TABLE(string varchar(25)) 
DECLARE @Key varchar(25)
SET @String = 'your string is going to come here'
SET @Pos = 1
WHILE (@Pos > 0)
BEGIN
    SET @Pos = PATINDEX('% %', @String)
    SET @Key = SUBSTRING(@String, 0, @Pos)
    IF (@Pos = 0)
        BEGIN
            INSERT INTO @Keywords (string) VALUES(@String)
        END
    ELSE
        BEGIN
            INSERT INTO @Keywords (string) VALUES(@Key)
        END
    SET @String = LTRIM(SUBSTRING(@String, @Pos, (SELECT LEN(@String))))
END 

SELECT * FROM @Keywords
esenkaya
  • 99
  • 4