SQL 选择 2 列合二为一并组合它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5340064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:44:02  来源:igfitidea点击:

Select 2 columns in one and combine them

sqlsql-server-2005select

提问by Gondim

Is it possible to select 2 columns in just one and combine them?

是否可以仅选择 2 列并将它们组合起来?

Example:

例子:

select something + somethingElse as onlyOneColumn from someTable

select something + somethingElse as onlyOneColumn from someTable

采纳答案by gjvdkamp

Yes, just like you did:

是的,就像你做的那样:

select something + somethingElse as onlyOneColumn from someTable

If you queried the database, you would have gotten the right answer.

如果你查询数据库,你会得到正确的答案。

What happens is you ask for an expression. A very simple expression is just a column name, a more complicated expression can have formulas etc in it.

发生的事情是你要求一个表达。一个非常简单的表达式只是一个列名,一个更复杂的表达式可以包含公式等。

回答by surya

(SELECT column1 as column FROM table )
UNION 
(SELECT column2 as column FROM table )

回答by David Fox

Yes,

是的,

SELECT CONCAT(field1, field2) AS WHOLENAME FROM TABLE
WHERE ...

will result in data set like:

将导致数据集如下:

WHOLENAME
field1field2

回答by Brian

None of the other answers worked for me but this did:

其他答案都不适合我,但这确实适用:

SELECT CONCAT(Cust_First, ' ', Cust_Last) AS CustName FROM customer

回答by SupremeDud

Yes it's possible, as long as the datatypes are compatible. If they aren't, use a CONVERT() or CAST()

是的,只要数据类型兼容,就可以。如果不是,请使用 CONVERT() 或 CAST()

SELECT firstname + ' ' + lastname AS name FROM customers

回答by RubenM

The +operator should do the trick just fine. Keep something in mind though, if one of the columns is null or does not have any value, it will give you a NULLresult. Instead, combine +with the function COALESCEand you'll be set.

+运营商应该做的伎俩就好了。但是请记住,如果其中一列为空或没有任何值,它会给您一个NULL结果。相反,结合+该功能COALESCE,您将被设置。

Here is an example:

下面是一个例子:

SELECT COALESCE(column1,'') + COALESCE(column2,'') FROM table1. 

For this example, if column1is NULL, then the results of column2will show up, instead of a simple NULL.

对于此示例,如果column1NULLcolumn2则将显示的结果,而不是简单的NULL

Hope this helps!

希望这可以帮助!

回答by paxdiablo

Yes, you can combine columns easily enough such as concatenating character data:

是的,您可以很容易地组合列,例如连接字符数据:

select col1 | col 2 as bothcols from tbl ...

or adding (for example) numeric data:

或添加(例如)数字数据:

select col1 + col2 as bothcols from tbl ...

In both those cases, you end up with a single column bothcols, which contains the combined data. You may have to coerce the data type if the columns are not compatible.

在这两种情况下,您最终都会得到一个bothcols包含组合数据的列。如果列不兼容,您可能必须强制转换数据类型。

回答by user1313900

if one of the column is number i have experienced the oracle will think '+' as sum operator instead concatenation.

如果其中一列是我经历过的数字,则 oracle 会将“+”视为和运算符而不是串联。

eg:

例如:

select (id + name) as one from table 1; (id is numeric) 

throws invalid number exception

抛出无效号码异常

in such case you can || operator which is concatenation.

在这种情况下,您可以 || 运算符是连接。

select (id || name) as one from table 1;

回答by Hisham Shaaban

I hope this answer helps:

我希望这个答案有帮助:

SELECT (CAST(id AS NVARCHAR)+','+name) AS COMBINED_COLUMN FROM TABLENAME;

回答by Arnoldiusss

Your syntax should work, maybe add a space between the colums like

您的语法应该有效,也许在列之间添加一个空格,例如

SELECT something + ' ' + somethingElse as onlyOneColumn FROM someTable

SELECT something + ' ' + somethingElse as onlyOneColumn FROM someTable