我正在尝试用我想要的任何字符串替换一些空 (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/