SQL 如何在 DB2 过程中将整数值与字符串连接起来
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7734757/
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
How to concat an integer value with a string in DB2 procedure
提问by user987880
I have a variable, price dec(5,0)
. How can I concat a static string "dollar" to that and save as a char(10)
?
我有一个变量,price dec(5,0)
. 如何将静态字符串“美元”连接到该字符串并另存为char(10)
?
If the price is 55555, the result should be 55555 Dollar and this should be saved as a char(11)
.
如果价格是 55555,结果应该是 55555 Dollar,这应该保存为char(11)
.
How can I do it? I tried casting and just concat using '+', but it was not working.
我该怎么做?我尝试使用“+”进行强制转换并仅连接,但它不起作用。
回答by MrG
The concat operator in DB2 is a double pipe, ||
.
DB2 中的 concat 运算符是一个双管道,||
.
Also, you'll need to cast
the decimal value to a char before you can concatenate.
此外,cast
在连接之前,您需要将十进制值转换为字符。
Something like:
就像是:
select cast(55555 as char(5)) || ' Dollar' from sysibm.sysdummy1
回答by Brian
No casting is needed - both of the exampples below work:
不需要强制转换 - 下面的两个示例都可以工作:
CONCAT(55555, ' Dollar') as "Test Column"
CONCAT(55555, 'Dollar') 作为“测试列”
OR
或者
55555 || ' Dollar' AS "Test Column 2"
55555 || “美元”作为“测试列 2”