Connie DeCinko Connie DeCinko - 2 months ago 8
SQL Question

Save delimited string from SQL query as string array

I am hoping to provide just enough information so as not to bore anyone.

I have a model that creates a user, which has various properties including an array of specializations:

public class User
{
...
public string[] specializations { get; set; }
}


Part of my query retrieves the specializations as a delimited list:

STUFF(( SELECT ';' + csc.description
FROM certified_specialization_code csc
INNER JOIN certified_specialization cs ON csc.specialization_code = cs.specialization_code
WHERE cs.member_number = m.entity_number)
FOR XML PATH('')), 1, 1, '' ) AS specializations,


I then take the query values and assign them to my model:

while (reader.Read())
{
...
member.specializations = reader["specializations"];
...
}


What is the proper syntax to take that string and assign it to member.specializations, the string[]?

Answer

Just like you'd split any other string:

member.specializations = reader["specializations"].ToString ().Split(';');
Comments