求DB2数据库查询本月内所有数据,哪位大神知道这样的SQL应该怎么写?

例如T表中有个交易日期date。。我想统计本月内所有交易的明细
2024-11-24 01:49:55
推荐回答(3个)
回答1:

SELECT * FROM T WHERE YEAR(DATE) = YEAR(CURREN T DATE) AND MONTH(DATE) = MONTH(CURRENT DATE);
如果数据量太大,这个可能有效率问题建议用下面的
SELECT * FROM T WHERE DATE BETWEEN CURRENT DATE - (DAY(CURRENT DATE -1) AND CURRENT DATE;

回答2:

select * from 表
where date>=本月首日 and date<下月首日
这样效率最高,可以正常使用date列的索引

回答3:

select

*
from
t
where
year(current timestamp)=year(date)
and
month(current timestamp)=month(date);