在sql中将2列合并为1并在组合框中显示 [英] combine 2 columns as 1 in sql and show in combobox

查看:94
本文介绍了在sql中将2列合并为1并在组合框中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将2列合并为一个,然后对齐合并的第二列.

示例(我想将公司ID和名称合并为1列),

i want to combine 2 columns as one and align 2nd col combined.

Example (i want to combine company id and name as 1 column),

coid          coname

co1           company 1

comp2         company 2

companyid3    company 3

----------------------

如果我只是简单地从mytable中选择coid +''''+ coname,那么它给我的结果是,

----------------------

if i simply do select coid + '' '' + coname from mytable, then it gives me result as,

co1   company 1

comp2   company 2

companyid3   company 3



我想对齐第二列(即,将名称改成like),



i want to align 2nd col (i.e. coname to like ),

co1             company 1

comp2           company 2

companyid3      company 3


----------------

我的代码,(我正在尝试以下代码)-但仍未对齐

我试图获得最大coid长度,然后相应地添加.在上面例如在第3条记录中仅添加5个空格,在第2条记录中添加10个空格,并在第1条中添加12个空格.但是仍然不能使用单个字符的bucause宽度.任何人都有这样做的想法.



----------------

My code, (i am trying following code) - But still not aligned

i am trying to get max length of coid then add accordingly. In above e.g. add only 5 spaces to 3rd record, add 10 spaces in 2nd record and add 12 spaces in 1st. But still no use may be bucause width of individual character. Anyone have have any idea to do this.


Dim mySql As String = "declare @len1 int " & Chr(10)
mySql = mySql & "select @len1 =  max(len(coid)) from mytable" & Chr(10)
mySql = mySql & "select coid, coid + space(@len1-len(coyid)+5) + coname as coname from mytable order by coid" & Chr(10)
Dim sqladp As New SqlClient.SqlDataAdapter(mySql, _SqlCon.sqlCon)



我想在组合框中显示此内容.因此用户可以从列表中进行选择.



i want to show this in combobox. So user can select from list.

推荐答案

使用模拟填充将为您提供所需的效果.
例如
select compId + RIGHT(REPLICATE('' '',10) + compName ,10)

我在这里用了10个.您可以使用最适合您的列的数字.
Using simulated padding will give you the desired effect.
E.g.
select compId + RIGHT(REPLICATE('' '',10) + compName ,10)

I''ve used 10 here. You can use a number that is ideal for your column.


这篇关于在sql中将2列合并为1并在组合框中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆