Have data in the database like the below table.
| ID | Data |
| 1 | 4963,4960 |
| 2 | 4961,4963 |
User input values can be like 4963,4960 or 4960,4963 both should match.
Please share the SQL query which matches both cases.
Thanks
Have data in the database like the below table.
| ID | Data |
| 1 | 4963,4960 |
| 2 | 4961,4963 |
User input values can be like 4963,4960 or 4960,4963 both should match.
Please share the SQL query which matches both cases.
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 1, 2023, 5:20 AM EST
Hello Vishal,
Try below
SELECT STRING_AGG(value, ',') WITHIN GROUP (ORDER BY CAST(value AS INT)) FROM ( SELECT value FROM STRING_SPLIT('4963,4960,4962,4961', ',') ) AS tThanks
Naimish Makwana
Vishal JoshiRank #377 · 6.7K reputationPosted Mar 2, 2023, 12:00 AM EST
Hello Naimish and Henry,
Thanks for the answers. Naimish answer matched my requirement.
Henry your answer also matches but we need to create a dynamic query based on user input.
Again thank you so much.
horald Henry18 reputationPosted Mar 1, 2023, 8:02 AM EST
To find comma-separated values in a string, you can use FIND_IN_SET() function. Here's a sample query that should work in both scenarios:
SELECT * FROM table_name WHERE (FIND_IN_SET('4963', Data) > 0 AND FIND_IN_SET('4960', Data) > 0) OR (FIND_IN_SET('4960', Data) > 0 AND FIND_IN_SET('4963', Data) > 0)Using the FIND IN SET function, this query first determines if the values '4963' and '4960' are present in the 'Data' column. The first criterion will be met if they are present. If not, it uses the second condition to check the reverse order '4960' and '4963'.