1

I have two SQL tables:

User:

  • UserId (nvarchar)
  • Tags (nvarchar)

Profile:

  • UserId (nvarchar)
  • Tags (nvarchar)

Example:

User table:

UserId      Tags
----------------------------------
1           tag1,tag2,tag3,tag4
2           tag1,tag2
3           tag1
4           tag2,tag3,tag4

after the Profile table tags should be

Profile table:

UserId      Tags
-------------------------------------
1           ["tag1","tag2","tag3","tag4"]
2           ["tag1","tag2"]
3           ["tag1"]
4           ["tag2","tag3","tag4"]

Currently the user tags are stored in User table in the Tags column like this, separated by comma: tag1,tag2,tag3,tag4. There can be also one single tag listed: tag1 (no comma present).

I need to "move/copy" those tags in the Profile table Tags column in the following format (string array): ["tag1","tag2","tag3","tag4"]

So basically in my update statement bellow, second line: SET Profile.Tags = UA.TAGS, UA.TAGS value should be converted into an array of strings.

UPDATE Profile
SET Profile.Tags = UA.TAGS (UA.TAGS value should be an array) 
FROM Profile UP INNER JOIN User UA ON UP.UserId = UA.UserId

Any ideas how I can achieve that?

1
  • You should never ever store multiple values in a single column - this violates even the first normal form of database design. Since you're using a relational database - use the relational capabilities of it - define a separate table Tags for the tags, and a "link" table between either User and Tags, or Profile and Tags ... Commented Oct 26, 2016 at 5:11

1 Answer 1

2
UPDATE Profile
SET Profile.Tags = '["'+ REPLACE(UA.TAGS,',','","') + '"]'
FROM Profile UP
INNER JOIN User UA
on UP.UserId = UA.UserId
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.