什么!! (双感叹号)运算符在 DB2 中做什么?

What does the !! (double exclamation) operator do in DB2?

我找到了一个看起来像这样的 sql 代码。删除双感叹号语句 (!! '') returns 完全相同的结果:

Select * from SomeTable a
where a.SOMEFIELD !! '' = 'SomeString'

!! '' 有什么作用?

!! 运算符是一种串联形式。 在您的情况下,它用于强制将 a.SOMEFIELD 转换为字符。

在 DB2 LUW 中这是不可能的:

db2 "create table SomeTable (SOMEFIELD int, col2 int)"
db2 "insert into SomeTable values (1,1),(2,2),(3,3)"
db2 "select * from SomeTable a where a.SOMEFIELD !! '' = '1'"
SQL0007N  The character "!" following "om t2 t where t.col1" is not valid.
SQLSTATE=42601

相反,@mustaccio 所说的是有效的:

db2 "select * from t2 t where t.col1 || '' = '1'"

COL1        COL2
----------- -----------
          1           1

  1 record(s) selected.

但是,null 与 something 的连接结果为 null; coalesce 检查第一个值是否为 null,然后 returns 第二个值:

db2 -x "values 'a' || 'b'"
ab
db2 -x "values NULL || ''"
-                                                                                                                                  

db2 -x "values NULL || 'b'"
-                                                                                                                                  

db2 -x "values coalesce(NULL, '')"

db2 -x "values coalesce(NULL, 'b')"
b