小编Ane*_*try的帖子

为什么 postgres 中 null||null 返回 null 值,而 concat(null,null) 返回空字符串?

我试图理解 concat() 和 || 之间的区别 在 postgres 中使用 null 时。如果 concat 两边都为 null,为什么 concat() 返回空字符串?

以此查询为例:

SELECT concat(NULL,NULL) AS null_concat, NULL||NULL AS null_pipes, 
concat(NULL,NULL) IS NULL is_concat_null, NULL||NULL IS NULL is_pipe_null
Run Code Online (Sandbox Code Playgroud)

将返回:

在此输入图像描述

我知道 concat() 会忽略空值,但是如果 concat 中的所有值都为空,那么预期结果不是空值吗?这是 postgres 中所有函数的典型行为吗?我在文档中找不到有关此场景的任何内容。

编辑:

我有一个想法,也许这是任何字符串函数的预期结果,但事实似乎并非如此。如果传递 null 值,upper() 和 left() 都会返回 null:

SELECT concat(NULL), NULL||NULL, UPPER(null), left(NULL,1)
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

sql postgresql

5
推荐指数
1
解决办法
1317
查看次数

标签 统计

postgresql ×1

sql ×1