[SQL 初学者]
我有这样的 sql 查询:
select * from
(select '' as dates from table1
union
select dates from table2)
当我运行此查询时出现错误 UNION types text and timestamp without time zone cannot be matched
。
我试图将表 2 中的 dates
列转换为 varchar,但这不是我想要的。如何将 table1 中的空字段转换为日期时间?
我试过这个:
select * from
(select cast(('' as dates) as date) as dates from table1
union
select dates from table2)
但这也不起作用。
最佳答案
给它 null
值。这可以转换为任何数据类型。
SELECT *
FROM (
SELECT null::timestamp AS dates FROM table1
UNION
SELECT dates FROM table2
) t
注意:您还可以使用 VALUES
,而不是“空”日期的 SELECT
。
关于postgresql - 到目前为止,如何在 union 中转换空列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57898274/