oracle sysdate 月份,如何将oracle中的sysdate月份值转换为数字?

Im trying to return the CARDS of my CARD table that will expire in the next month. But the problem is that the table has two columns to represent the card date.

假设:

要使用浮动个月(比如:从12月23日至1月23日)和

你的表不知何故只包含一个(浮动?)年的数据

为什么你不能使用简单的算术?这样的:

-- some constant definitions for testing purpose

with cst as (

select EXTRACT(DAY from TO_DATE('23/12','DD/MM')) as theDay,

EXTRACT(MONTH from TO_DATE('23/12','DD/MM')) as theMonth

from dual)

-- the actual query

select card.* from card,cst

where (expiremonth = theMonth AND expireday > theDay)

or (expiremonth = 1+MOD(theMonth,12) AND expireday <= theDay);

-- ^^^^^^^^^^^^^^^^^^

-- map [01 .. 12] to [02 .. 12, 01] (i.e.: next month)

这会简单地选择所有的“伪日期”从明天开始到月底,以及任何一个之前(含)当天#下个月。

对于一些有点更通用的,但可能不是将所有你的价值观TO_DATE更有效率,你可能会想尝试的是:

-- the calendar is the key part of the query (see below)

with calendar as (

select extract(month from sysdate + level) as theMonth,

extract(day from sysdate + level) as theDay

from DUAL connect by ROWNUM <= 8)

-- ^

-- adjust to the right number of days you are looking for

select card.* from card join calendar

on expiremonth = theMonth and expireDay = theDay

这里的想法是简单地建立日历与所有即将到来的日子,然后加入您的数据表在该日历上。查看example here。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值