1

Server : SQL Server 2016

I am trying to split a delimited string in one column in DB to JSON Array for easy joins in ETL Job

shirt|~*~|trouser|~*~|t-Shirt|~*~|towels|~*~| 

into JSON Value

   {"values":["shirt", "trouser", "t-Shirt", "towels"]}

One of the article I followed here helped a little, but could not get me far enough, I want to convert using select instead of procedure/function

Any Pointers is helpful

SQL to JSON - array of objects to array of values in SQL 2016

6
  • 2
    Do you have to do this in SQL Server? I mean, it would be much easier to handle unnormalized CSV data somewhere like C# or Java than in a database. Commented Sep 5, 2019 at 15:39
  • We are reading the Data from SQL Views in Batch Jobs as a part of ETL Commented Sep 5, 2019 at 15:45
  • 3
    OK, any time you find yourself pulling in large amounts of CSV data into your database, it might be time to step back and question your pipeline. Commented Sep 5, 2019 at 15:46
  • Why is there a delimiter at the end? Shouldn't it therefore be '{"values":["shirt", "trouser", "t-Shirt", "towels",""]}'? Commented Sep 5, 2019 at 15:57
  • 2
    I think "step back and question your pipeline" should be the accepted answer. Commented Sep 5, 2019 at 16:01

1 Answer 1

1

Seems like REPLACE would be better option here:

DECLARE @CSV varchar(MAX) = 'shirt|~*~|trouser|~*~|t-Shirt|~*~|towels|~*~|';

SELECT '{"values":["' + REPLACE(V.csv,'|~*~|','","') + '"]}'
FROM (VALUES(@CSV))V(csv);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.