0

I am using SQL Server 2012 - unfortunately STRING_SPLIT function is not implemented in this version.

My problem is this: I have a string @cRECE_STA = 'A,B,C' - would like to get values from that string like:

+------+
| Col1 |
+------+
| A    |
| B    |
| C    |
+------+

I need this because those values will be later inserted into table, one by one. How to do this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
FrenkyB
  • 6,625
  • 14
  • 67
  • 114
  • 3
    Read Aaron Bertrand's [Split strings the right way – or the next best way](https://sqlperformance.com/2012/07/t-sql-queries/split-strings) – Zohar Peled Dec 06 '18 at 12:39
  • [DelimitedSplit8k_lead](http://www.sqlservercentral.com/articles/SQL+Server+2012/106903/) – Thom A Dec 06 '18 at 12:45

1 Answers1

1

If you have just chars in the string you can use

DECLARE @Str VARCHAR(45) = 'A,B,C,D,E,F,G,H,I';

WITH LN(IDX) AS
(
  SELECT 1
  UNION ALL
  SELECT IDX + 1
  FROM LN
  WHERE IDX <= LEN(@Str)
)
,
Str(ID, V) AS
(
  SELECT 1, @Str
)
SELECT SUBSTRING(V, IDX, ID) Results
FROM Str CROSS JOIN LN
WHERE IDX % 2 = 1;

Demo

If not, you need to create your own STRING_SPLIT() function.

Ilyes
  • 14,640
  • 4
  • 29
  • 55