计算当月从第 1 天到昨天的天数

count the number of days of current month from day 1 until yesterday

我正在尝试计算当前月份从第 1 天到昨天的天数,而无需手动更改计数。原文SQL如下:

select order_id 
from orders 
where date > dateadd(-23 to current_date) and date < 'today'

所需的代码类似于

select order_id 
from orders 
where date > dateadd(datediff(day,firstdayofthemonth,current_date) to current_date) and date < 'today'

感谢任何帮助

在 firebird 中你可以这样做:

WHERE 
    date >= DATEADD(1 - EXTRACT(DAY FROM CURRENT_DATE) DAY TO CURRENT_DATE) 
    AND date < CURRENT_DATE

除了 GMB 提供的答案之外,您还可以使用 Firebird 允许在日期中添加天数而无需使用 dateadd:

date > current_date - extract(day from current_date) 
and date < current_date

除了马克提供的答案外,您还可以使用BETWEEN(从Firebird 2.0.4开始)

WHERE 
   date BETWEEN current_date - extract(day from current_date) + 1 
            AND current_date - 1

P.S。所有这些答案都依赖于没有时间部分的 DATE 数据类型(因此,date 列和 CURRENT_DATE 变量)。这是为现代 SQL 方言 3 提供的。但是如果方言 1 会被使用,则没有提供。