I am using the STRING_SPLIT function to generate rows from a scalar value @ValueData VARCHAR(512) = "One, Two, Three" as follows
In my development database it works perfectly but in production I can't run it because I can't alter the COMPATIBILITY_LEVEL of the database. I would also like to learn an alternative way to do this without using a function.
There are several alternatives, and all have their complexity.
In the first part, each of the items is separated, doing a replace to convert the separator into a varchar that contains the data and between them we see the xml elements.
Then, since it is already an xml, it is queried using xQuery.
Disadvantages: The data source cannot contain "<>", since it would return an XML Parsing error. Invalid character in full name.
There is a lot of controversy in specialized forums with this function created by Jeff Moden, about whether its performance analyzes are exaggerated or prepared for an execution in which it benefits, but what is certain is that it works very well and is very efficient. .
now to use it
Limited Split 8k
We will now use a combination of two "master" functions. The first created by Itzik Ben-Gan , is very usable for many other things, it returns a column of the numbers that are among those defined as parameters.
And the second, that I cannot put the link of its author, basically because I do not know him, but it is based on this first one.
Now with the two table-type functions, their consumption would be:
Summary:
Although there are other ways, these three are very good in performance, don't use cursors, aren't limited to a finite number of elements, and aren't limited to scalar values either. All three allow you to treat columns as sets with the apply operator.
Additional note: That you don't want to use functions, for some specific reason, doesn't mean that you can't reform a function to use the same code directly in your developments.
--Another option, although very caveman but another option after all:
/* Function to replace the use of String_Split (With SqlServer 2019)
*/
Create Or Alter Function dbo.fn_String_Split( @ValueDatos VarChar(512) ) Returns @TablaDatos Table (Value VarChar(512))
ACE
Begin Declare @Delimiter Char(1) = ',',
@From Int,
@To Int, @Value VarChar(512)
End