How do you query a specific value from an Sql column containing multiple values, separated by comma?

Castamir

BANNED
Joined
Sep 4, 2019
Messages
1,200
Reaction score
1,058
How do you query a specific value from an Sql column containing multiple values, separated by comma?

Example
Let the table below represent a sql database with two columns named student and results. Results column contain two comma separated values. How can I select the "percentage" in an an Sql query?



Student | Results
---
Student 1| "marks": 12, "percentage"=2
Student 2 |"marks": 32, "percentage"=5
Student 3 |"marks": 52, "percentage"=9
 
Stackoverlow or expertsexchange may have the answers for you. That's where I've found a lot of my coding answers over the years.
 
This may help: https://stackoverflow.com/questions/10581772/how-to-split-a-comma-separated-value-to-columns

It might be easier to just grab the value and parse it manually.
Thanks for the link. I got this from there, will this work?

SELECT value,
PARSENAME(REPLACE(String,',','.'),2) 'Name' ,
PARSENAME(REPLACE(String,',','.'),1) 'Surname'
FROM table WITH (NOLOCK)
 
How do you query a specific value from an Sql column containing multiple values, separated by comma?

Example
Let the table below represent a sql database with two columns named student and results. Results column contain two comma separated values. How can I select the "percentage" in an an Sql query?



Student | Results
---
Student 1| "marks": 12, "percentage"=2
Student 2 |"marks": 32, "percentage"=5
Student 3 |"marks": 52, "percentage"=9

Since the actual percentage is in the end, you can just do:

select right(results, 1) from table - this will separate the last digit of the string
 
Code:
SELECT
    Student,
    SUBSTRING_INDEX(SUBSTRING_INDEX(Results, 'percentage=', -1), ',', 1) AS Percentage
FROM
    your_table_name;
 
Back
Top