Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply like function on an array is SQL Server

I am getting array from front end to perform filters according that inside the SQL query.

I want to apply a LIKE filter on the array. How to add an array inside LIKE function?

I am using Angular with Html as front end and Node as back end.

Array being passed in from the front end:

 [ "Sports", "Life", "Relationship", ...]

SQL query is :

SELECT *
FROM Skills
WHERE Description LIKE ('%Sports%')

SELECT *
FROM Skills
WHERE Description LIKE ('%Life%')

SELECT *
FROM Skills
WHERE Description LIKE ('%Relationship%')

But I am getting an array from the front end - how to create a query for this?

like image 573
Amandeep Singh Avatar asked Mar 15 '26 10:03

Amandeep Singh


1 Answers

In SQL Server 2017 you can use OPENJSON to consume the JSON string as-is:

SELECT *
FROM skills
WHERE EXISTS (
    SELECT 1
    FROM OPENJSON('["Sports", "Life", "Relationship"]', '$') AS j
    WHERE skills.description LIKE '%' + j.value + '%'
)

Demo on db<>fiddle

like image 117
Salman A Avatar answered Mar 17 '26 23:03

Salman A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!