Oracle SQL:以日期格式的表演月份

发布于 2025-02-13 16:06:16 字数 218 浏览 0 评论 0原文

我想显示一个使用以下内容创建的计算列:

to_char(
        COALESCE(
            date1,
            date2,
            date3
        ),
        'MONTH - YYYY'
    ) month_year_column

但是问题是返回字符串需要一个日期字段

I would like to show a calculated column that is created using the following:

to_char(
        COALESCE(
            date1,
            date2,
            date3
        ),
        'MONTH - YYYY'
    ) month_year_column

but the problem is this returns a string need a date field instead

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖风昔人 2025-02-20 16:06:16

在Oracle中,日期是一种二进制数据类型,由7个字节组成:代表:世纪,世纪,月,日,小时,小时,分钟和秒。 总是具有所有这些组件,并且从未以任何特定(人类可读)格式存储。

因此,不可能拥有date数据类型和特定格式。


如果要date值,而将日和时间组件设置为一个月的第一天的午夜,则可以使用:

TRUNC( COALESCE(date1, date2, date3 ), 'MM') AS month_year_column

注意:然后,这将由客户端应用程序格式化您用于根据其默认日期格式规则访问数据库(可能不会显示日期的所有组件);这不会改变数据库将值存储为7字节二进制值,无论客户端应用程序如何选择显示它。


如果您想要使用特定格式的格式,则不能使用(未格式化) date,需要将值转换为可以格式化的另一种数据类型,例如字符串,您可以使用代码:

TO_CHAR( COALESCE( date1, date2, date3 ), 'MONTH - YYYY' ) AS month_year_column

In Oracle, a DATE is a binary data type that consists of 7 bytes representing: century, year-of-century, month, day, hours, minutes and seconds. It ALWAYS has all of those components and is NEVER stored with any particular (human-readable) format.

Therefore, it is impossible to have a DATE data type and a particular formatting.


If you want a DATE value where the day and time components are set to midnight of the first day of the month then you can use:

TRUNC( COALESCE(date1, date2, date3 ), 'MM') AS month_year_column

Note: this will then be formatted by the client application you are using to access the database according to its default date formatting rules (which may not show all the components of the date); this does not change that the database will store the value as a 7-byte binary value regardless of how the client application choses to display it.


If you do want it with a particular formatting then you cannot use a (unformatted) DATE and need to convert the value to another data type that can be formatted, such as a string and you can use your code:

TO_CHAR( COALESCE( date1, date2, date3 ), 'MONTH - YYYY' ) AS month_year_column
笑看君怀她人 2025-02-20 16:06:16

我想您可以使用NVL而不是Colesce来避免丢失日期数据类型。

nvl(date1,nvl(date2,date3))

I guess you can use NVL instead of COALESCE to avoid losing the DATE data type.

NVL (date1, NVL (date2, date3))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文