在 Oracle SQL 中连接字符串而中间没有空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7945328/
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-19 00:24:48  来源:igfitidea点击:

Concatenate strings in Oracle SQL without a space in between?

sqlstringoracleoracle10gconcatenation

提问by brainless

I am trying to concatenate strings in oracle.

我正在尝试在 oracle 中连接字符串。

The following is my query:

以下是我的查询:

insert into dummy values('c'||to_char(10000,'99999'));

The expected result is:

预期的结果是:

c10000

But the output I get is with a space in between 'c' and the value 10000:

但是我得到的输出在 'c' 和值 10000 之间有一个空格:

c 10000 

How to concat without spaces?

如何在没有空格的情况下进行连接?

回答by Erwin Brandstetter

This is not an issue with the concatenation operator but with the function to_char(). Try instead:

这不是连接运算符的问题,而是函数的问题to_char()。试试吧:

to_char(10000,'FM99999')

I quote the manual here:

在这里引用手册

FM .. Returns a value with no leading or trailing blanks.

FM .. 返回一个没有前导或尾随空格的值。

回答by PaulK

There are two solutions:

有两种解决方案:

  1. Fill Mode ('FM') formatting prefix that suppresses the additional blank character prefix for the to_charnumber conversion. I suggest this one is preferred, because it is integrated with the to_char format and does not require an additional function call;
  2. LTRIMof the returned value from the to_charnumber conversion.
  1. 填充模式 (' FM') 格式前缀,用于抑制to_char数字转换的额外空白字符前缀。我建议首选这个,因为它集成了 to_char 格式,不需要额外的函数调用;
  2. LTRIMto_char数字转换的返回值。

The code below shows the results of both solutions:

下面的代码显示了两种解决方案的结果:

Select concat('NTA', to_char(1,'FM0000000000000')), 
concat('NTA', ltrim(to_char(1,'0000000000000'))), 
concat('NTA', to_char(1,'0000000000000'))
from dual;

"CONCAT('NTA',TO_CHAR(1,'FM0000000000000'))": "NTA0000000000001" "CONCAT('NTA',LTRIM(TO_CHAR(1,'0000000000000')))": "NTA0000000000001" "CONCAT('NTA',TO_CHAR(1,'0000000000000'))": "NTA 0000000000001"

"CONCAT('NTA',TO_CHAR(1,'FM0000000000000'))":“NTA0000000000001” "CONCAT('NTA',LTRIM(TO_CHAR(1,'0000000000000')))":“NTA0000000000001” "CONCAT('NTA',TO_CHAR(1,'0000000000000'))":“NTA 0000000000001”