简体   繁体   English

在 BigQuery 中获取当月的最后一天 IFNULL

[英]Getting the last day of the current month IFNULL in BigQuery

I have an end_date column that I need to map, the problem is that it has some null values and I would like to have the last day of the month in case it's null.我有一个end_date列,我需要 map,问题是它有一些 null 值,我想有一个月的最后一天,以防它是 null。

This is what I've been trying but for some reason it doesn't allow me to:这是我一直在尝试的,但由于某种原因它不允许我:

IFNULL(CAST(end_date) AS STRING, LAST_DAY(CURRENT_DATE(), month))

What am I might be doing wrong?我可能做错了什么?

I managed to solve it, I was using the CAST syntax wrong and I also needed to CAST the last day of the current month as a string too so I could use the IFNULL.我设法解决了这个问题,我使用了错误的 CAST 语法,我还需要将当月的最后一天作为字符串进行 CAST,这样我就可以使用 IFNULL。

This is how the correct query would look like:这是正确查询的样子:

DATE(
    IFNULL(
        CAST(end_date AS STRING),
        CAST(LAST_DAY(CURRENT_DATE(), month) AS STRING)
    )
)

@Jaytiger end_date is DATE type, @Jaytiger end_date 是 DATE 类型,

Below is more concise one if you want the result as DATE type, I guess.我猜如果您希望结果为DATE类型,下面是更简洁的一个。

IFNULL(DATE(end_date), LAST_DAY(CURRENT_DATE(), month))

Example:例子:


SELECT IFNULL(DATE '2022-08-26', LAST_DAY(CURRENT_DATE(), month)) not_null,
       IFNULL(NULL, LAST_DAY(CURRENT_DATE(), month)) `null`,

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM