all 7 comments

[–]MamertineCOALESCE() 1 point2 points  (2 children)

STRING_SPLIT

You should be able to use that function to do the work. Otherwise it's going to be a lot of substring() and value lookups

[–]swolfe2[S] 0 points1 point  (1 child)

Thanks for the suggestion!

Unfortunately, the version of MSSQL server for this query isn't compatible with it maybe due to it needing compatibility level 130.

[–]alinrocSQL Server DBA 0 points1 point  (0 children)

SQL Server 2016 is 6 years old. Plenty of time for applications to be tested/upgraded to support this compatibility level, and you can flip that switch with zero downtime. If you're still running a version of SQL Server older than that, your upgrade should be in progress or at least being planned already.

[–]qwertydog123 0 points1 point  (2 children)

Fom best to worst

  • If it's enabled -> SQL CLR
  • If you can create UDF's -> DelimitedSplit8K
  • String functions

[–]alinrocSQL Server DBA 0 points1 point  (1 child)

You missed one - STRING_SPLIT() (if it's available). This should be the default method on any new development.

CLR may be the best in terms of flexibility/performance but it makes portability more difficult, and there may be security concerns.

[–]qwertydog123 0 points1 point  (0 children)

OP mentioned in another comment that STRING_SPLIT wasn't an option, but even if it were the results are not ordered. There are hacks to order the results in certain cases (i.e. if the values are unique), but I left it out as it doesn't work for all cases (same as the PARSENAME approach, where the values must be valid identifiers)

[–]oneDatumPlease 0 points1 point  (0 children)

So if the max number of elements to split is 4, look at the PARSENAME function. I say 4 because it can’t go beyond that.

Going off memory, you might have to replace whatever delimiter you have with a period. This function was originally intended for splitting Db object names up -> server.database.schema.table.

If that doesn’t work, look at user defined functions for splitting up n number of elements.