我有两栏:
31)
我正在尝试编写一个CASE语句,如果DayOfMonth发生在' date‘和今天的日期之间,则返回true。
例如,如果DayOfMonth = 18和Date = '2020-04-14',则CASE语句将返回True,因为4月18日发生在今天到4月14日之间。
谢谢
发布于 2020-04-28 17:14:43
大多数数据库都支持day()函数来返回一个月的日期。
where (DayOfMonth >= day('2020-04-14') and
DayOfMonth <= day(current_date)
) or
'2020-04-14' <= datedd(month, -1, current_date)一些数据库将day()拼写为extract(day from <date>)。有些人有编写current_date的替代方式。
编辑:
这比我想象的要复杂得多,因为月中的一天可能是31天--但一个月可能没有31天。下面不处理二月,但它给出了这样的想法:
where -- first the trivial case
(dayOfMonth <= day(current_date) and
(@date <= date_trunc('month', current_date) or
dayOfMonth >= day(@date)
)
) or
-- then the previous month case
(date_trunc('month', @date) = date_trunc('month', current_date) - interval '1 month' and
dayOfMonth >= day(@date) and
dayOfMonth <= day(last_date(@date))
) or
-- Days at the end of month but not necessarily in the previous month.
@date < date_trunc('month', current_date) - interval '1 month'https://stackoverflow.com/questions/61485969
复制相似问题