Click here to Skip to main content
16,005,552 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have an Ms-sql db, and my table goes like this

CityList Table
ID | City
001| Mumbai
002| Ahmedabad
003| Delhi
004| Baroda


Student Table
Rollno | ExamsAppearedinCityIDList
1 | 001,002,004


i want report like following

Roll | Cities
1 | Mumbai,Ahmedabad,Baroda


What should i do to fetch this result?


or what is the equallent of following in sql

public string getCityList()
{
string cityList=""
foreach(row in dat.DataRows)
{
foreach(string ID in row.Split(","))
{
CityList +=dtCity["ID"].ToString();
}

}
return CityList;
}
Posted

The best solution is: don't. Store the values separately in a separate table that uses the student table roll no and Citylist table ID as foreign keys, and each row references a pair of keys - you can then use JOIN to populate your result.

You can do what you want using your method, but it's a lot of work and it doesn't solve the problems it's going to give you later (which are another huge pile of work) - it's much, much easier to change your table design now while it's still early and do it right.

If you want to do all the work each time you access the table (and not just for this query) then this will give you an idea where to start: Converting comma separated data in a column to rows for selection[^] - but it really is better to do the job right instead.
 
Share this answer
 
As OriginalGriff[^] mentioned in solution 1, you shouldn't store such of data into single row! Rather than it, store every single city for each student in single row:
Rollno | ExamsCities
1 | 001
1 | 002
1 | 004
2 | 003


How to achieve that? Use CTE to split comma separated data into several rows:
How a split a String using delimiter in Sql[^]
How to split a string in sql server 2008 using stored procedure and insert the data to table[^]
 
Share this answer
 
Firstly as per your Database it is storing comma separated value. So it is bit difficult to manage data. Instead of storing data in comma separated you can store multiple records for coma separated.

Secondly as you already developed and have existing data, please find solutions below:
Create a custom function using below code:
SQL
CREATE FUNCTION [dbo].[fnSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1
       
        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)
        
    END 
    RETURN 
END


Write below query to achieve the result:
SQL
SELECT S.Rollno, STUFF((SELECT distinct ', ' + t1.City
         from (SELECT CL.City
FROM CityList CL INNER JOIN 
(SELECT splitdata from dbo.fnSplitString(S.ExamsAppearedinCityIDList,',')) AS Temp
ON CL.ID = Temp.splitdata) t1
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)') 
,1,2,'') Cities
FROM Student S
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900