Skip to content
Advertisement

Where clause from array column

I have a column with value ["NONCODE","OFFCR", "SPVR", "CORD"] inside.

I want to return a value when one of the values ​​in the column matches.

In that field column, I have value

If I query

the result shows the only row contain ‘SPVR’

the result shows the only row contain ‘MGR’ (the row with ‘ASST-MGR’ do not appear)

If I use the ‘LIKE’ command, all the rows containing MGR will appear

Advertisement

Answer

Original answer:

If you use SQL Server 2016+, you may try to parse this data using OPENJSON() function, because ["NONCODE","OFFCR", "SPVR", "CORD"] is a valid JSON array. OPENJSON() is a table-valued function that parses JSON text, so you can use any set-based approach to get your expected results.

Table:

Statement:

Output:

Update:

For SQL Server 2012, you may try to split the data using XML:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement