将 varchar 列与 int 列组合 [英] Combine varchar column with int column

查看:38
本文介绍了将 varchar 列与 int 列组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL 表中有两列,fooId (int) 和 fooName (varchar).

I have two columns in a SQL table, fooId (int) and fooName (varchar).

有没有办法将它们都选择为一列,它们之间有一个空格?

Is there a way to select them both as one column with a space between them?

select fooId + ' ' + fooName as fooEntity
from mytable

它们是不同的类型,所以我收到了错误消息.

They're different types so I'm getting an error.

此字段将直接在 Web 应用程序的控件中进行数据绑定.

This field will be databound directly in a control in the web app.

SQL Server 2008

SQL Server 2008

(我是一个 sql 初学者)

(I'm a bit of a sql beginner)

推荐答案

不同数据库之间的字符串连接是不同的,所以知道是哪个数据库是有帮助的,因为你需要知道:

String concatenation is different between databases, so it helps to know which database because you need to know:

  1. 连接方法/运算符
  2. 如果数据库处理隐式数据类型转换

SQL Server 不会将数字隐式转换为字符串值:

SQL Server doesn't do implicit conversion of numeric into string values:

SELECT CAST(fooid AS VARCHAR(10)) + ' ' + fooname

...所以你需要使用 CAST (或 CONVERT) 将数据类型显式更改为基于文本的数据类型.

...so you need to use CAST (or CONVERT) to explicitly change the data type to a text based data type.

对于 甲骨文 &PostgreSQL,使用双管道连接字符串:

For Oracle & PostgreSQL, use the double pipe to concatenate strings:

SELECT fooid || ' ' || fooname

对于 MySQL,您可以使用 CONCAT 函数:

For MySQL, you can use the CONCAT function:

SELECT CONCAT(fooid, ' ', fooname)

这篇关于将 varchar 列与 int 列组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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