1

I have a table in with a column that has values like this:

["L", "L", "L", "D", "L", "D", "L", "L", "L", "D"] , 

I have to add it in another table which will be 10 columns and response will be 1 for character "L" , 0 for character "D".

My column names will be dynamic as the string contains 240 items of L and D. In the above example, only 10 are present. Column name will be item1, item2 and so on.

6
  • Will this string always contain the same number of items? Commented Nov 13, 2017 at 10:05
  • Firstly, why do you need to do this? What are the different columns representing and why is this data not being recorded as rows? Commented Nov 13, 2017 at 10:07
  • @ldo Gal - Yes Same no of items Commented Nov 13, 2017 at 10:08
  • @iamdave - Client want to the data in that format to analysis . the columns representing the boolean value 1 for L and 0 for D . The one string recorded as one row, I want to split in columns with that column names like item1, item2 and so on. Commented Nov 13, 2017 at 10:10
  • There are numerous questions and answers on SO about string splitting. Have you tried anything? Commented Nov 13, 2017 at 10:11

1 Answer 1

1

What about XML node method have you tried ever

select distinct
      case (a.value ('/A[1]', 'VARCHAR(MAX)')) when '"L"' THEN 1 WHEN ' "D"' THEN 0 END  [Column1],
      case (a.value ('/A[2]', 'VARCHAR(MAX)')) when ' "L"' THEN 1 WHEN ' "D"' THEN 0 END  [Column2],
      case (a.value ('/A[3]', 'VARCHAR(MAX)')) when ' "L"' THEN 1 WHEN ' "D"' THEN 0 END  [Column3],
      case (a.value ('/A[4]', 'VARCHAR(MAX)')) when ' "L"' THEN 1 WHEN ' "D"' THEN 0 END  [Column4],
      case (a.value ('/A[5]', 'VARCHAR(MAX)')) when ' "L"' THEN 1 WHEN ' "D"' THEN 0 END  [Column5],
      case (a.value ('/A[6]', 'VARCHAR(MAX)')) when ' "L"' THEN 1 WHEN ' "D"' THEN 0 END  [Column6],
      case (a.value ('/A[7]', 'VARCHAR(MAX)')) when ' "L"' THEN 1 WHEN ' "D"' THEN 0 END  [Column7],
      case (a.value ('/A[8]', 'VARCHAR(MAX)')) when ' "L"' THEN 1 WHEN ' "D"' THEN 0 END  [Column8],
      case (a.value ('/A[9]', 'VARCHAR(MAX)')) when ' "L"' THEN 1 WHEN ' "D"' THEN 0 END  [Column9],
      case (a.value ('/A[10]', 'VARCHAR(MAX)')) when ' "L"' THEN 1 WHEN ' "D"' THEN 0 END  [Column10]

      from 
(
    SELECT CAST('<A>'+REPLACE(REPLACE(REPLACE(@DATA, '[', ''), ']', ''), ',', '</A><A>')+'</A>' AS XML) AS Data 
    --FROM <table_name>
) A CROSS APPLY Data.nodes ('/A') as split(a)

Result :

Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8 Column9 Column10
    1       1       1       0       1       0       1        1      1       0
Sign up to request clarification or add additional context in comments.

3 Comments

yes , its helped me at initial stage , but now i have to convert that "L" to 1 and "D" to 0
use case or replace
@KulOmkar try edited solutions with case expression

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.