问题描述
我正在使用VS2008 C#和SQL2008 R2。
我想从多个列中检索数据并合并它们,用逗号分隔它们。
例如我的SQL表中的数据主题:
No. | CourseCode |主题|
1 | Cur001 |英语|
2 | Cur001 |数学|
3 | Cur001 |印地语|
数据我想在我的GridView GridView1中显示:
No. | CourseCode |主题|
1 | Cur001 |英语,数学,印地语|
如何在sql2008中编写SELECT命令,为GrideView编写C#代码
I am using VS2008 C# and SQL2008 R2.
I would like to retrieve data from multiple columns and merge them, separating them by comma.
E.g. Data in my SQL Table Subject:
No. | CourseCode| subject |
1 | Cur001| English|
2 | Cur001| math|
3 | Cur001| Hindi|
Data I would like to display in my GridView GridView1:
No. | CourseCode| subject |
1 | Cur001 | English, math, Hindi|
how i write "SELECT" Command for this in sql2008 and C# code for GrideView
推荐答案
select no,username,role1 +','+role2+','+role3 as ROLE from UserData
如果你需要将数值数据类型与varchar数据类型组合,那么你需要使用它。
in case you need to combin a numeric data type with varchar datatype then you need to use this.
select no,username,cast(no as varchar)+','+role1 +','+role2+','+role3 as ROLE from UserData
这篇关于从多个列检索数据并合并它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!