PostgreSQL - 如何用任何字符串替换空字段?

标签 postgresql null coalesce

我正在尝试用我想要的任何字符串替换一些空 (NULL) 字段,这些字段是我查询的结果。这些空字段被放置在“没有时区的时间戳”列中。所以我尝试使用 COALESCE 函数,但没有结果(我得到错误:时间戳的输入语法无效:“any_string”:

select column1, coalesce(date_trunc('seconds', min(date)), 'any_string') as column2

有什么问题吗?

表:

╔════╦═════════════════════╦═════════════════════╗
║ id ║        date         ║        date2        ║
╠════╬═════════════════════╬═════════════════════╣
║  1 ║ 2013-12-17 13:54:59 ║ 2013-12-17 09:03:31 ║
║  2 ║ 2013-12-17 13:55:07 ║ 2013-12-17 09:59:11 ║
║  3 ║ 2013-12-17 13:55:56 ║ empty field         ║
║  4 ║ 2013-12-17 13:38:37 ║ 2013-12-17 09:14:01 ║
║  5 ║ 2013-12-17 13:54:46 ║ empty field         ║
║  6 ║ 2013-12-17 13:54:46 ║ empty field         ║
║  7 ║ 2013-12-17 13:55:40 ║ empty field         ║
╚════╩═════════════════════╩═════════════════════╝

示例查询:

select q1.id, q2.date, q3.date2
from (select distinct id from table1) q1
left join (select id, date_trunc('seconds', max(time)) as date from table2 where time::date = now()::date group by id) q2 on q1.id = q2.id
left join (select id, date_trunc('seconds', min(time2)) as date2 from table1 where time2:date = now()::date group by id) q3 on q1.id = q3.id
order by 1

问题是用我想象的任何字符串替换上面的那些空字段。

最佳答案

您可以使用 ::text 简单地将时间戳转换为文本

select column1, coalesce(date_trunc('seconds', min(date))::text, 'any_string') as column2

关于PostgreSQL - 如何用任何字符串替换空字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20630413/

相关文章:

postgresql - 触发函数在 UPDATE 上刷新远程物化 View

google-bigquery - BigQuery Standard 分组时获取第一个非空值

java - 双向链表

javascript - 为什么这个 JavaScript 表达式的计算结果为 false?

mysql - 组函数的无效使用 - MySQL 使用 COALESCE

sql - 即使没有匹配查询的行,如何强制 postgres 返回 0,使用 coalesce,group by 和 join

mysql - 将 NULL 值插入 postgreSQL 中的 TIMESTAMP 字段

postgresql - 使用 Terraform 的 AWS RDS IAM 身份验证

sql - 一对多表确保两个表都来自链上的同一父表

.net - GetCustomAttribute() 对 AssemblyVersionAttribute 返回 null