Skip to content
Advertisement

Get data from other table based on column with concatenated values

I have two tables:

category with columns:

article with columns:

I know there should be a separate table for article categories but I was given this.

Is it possible to write a query that returns:

I thought of splitting the string in article -> categories column, then use in query to extract name from category table but couldn’t figure it out.

Advertisement

Answer

You should fix your data model. But, you can do this in SQL Server:

Here is a db<>fiddle.

Presumably, you already know the shortcomings of this data model:

  • SQL Server has poor string handling functionality.
  • Numbers should be stored as numbers not strings.
  • Foreign key references should be properly declared.
  • Such queries cannot make use of indexes and partitions.
  • If you really want to store multiple values in a field, SQL Server offers both JSON and XML. Strings are not the right approach.
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement