对不同的数据类型使用COALESCE? [英] Using COALESCE with different data types?
问题描述
我有一个使用 COALESCE(timestamp_type :: date,charachter_varying)
的查询,由于数据类型不匹配而失败:
I have a query using COALESCE(timestamp_type::date,charachter_varying)
which fails due to the mismatched data types:
错误:COALESCE类型的日期和字符变化无法匹配
ERROR: COALESCE types date and character varying cannot be matched
如果我将时间戳转换为 text $ c $,则该方法有效c>:
It works if i cast the timestamp as text
:
COALESCE(timestamp_type :: text,charachter_varying)
但是,当我只想要 YYYY-MM-DD
时,它返回完整的时间戳(而不是完整的时间戳 YYYY) -MM-DD HH:MM:SS.000000 + 00
)
However, now it is returning the full timestamp when I only want YYYY-MM-DD
(instead of the full timestamp, YYYY-MM-DD HH:MM:SS.000000+00
)
如何使用COALESCE 和仅返回时间戳的日期部分?
How can I use COALESCE and return only the date portion of the timestamp?
推荐答案
可以使用适当的to_char转换时间戳格式掩码:
You can use to_char to convert the timestamp using appropriate format mask:
COALESCE(to_char(timestamp_type, 'YYYY-MM-DD'), varchar_col)
这篇关于对不同的数据类型使用COALESCE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!