关于Oracle日期转换的一点小经验:运用union的一种奇怪现象及解决

Oracle9i安装默认的日期格式是‘DD-MM-RR’,这个可以通过
    select * from sys.nls_database_parameters;
进行查看。

因此对于输入参数为DATE类型的存储过程就出现问题了,其中查询基本表(tranficstat
)里记录日期格式为‘yyyy-mm-dd’。原码如下:

--比较某两个车站相同时间段的运力情况

create or replace procedure HY_CONTRAST_PERIOD(
   depotcode1 in varchar2,
   depotcode2 in varchar2,
   startdate1 in date,
   enddate1 in date,
   cur_return out CUR_DEFINE.GB_CUR) is -- CUR_DEFINE.GB_CUR 是自定义的游标类型
begin
  case
  when (depotcode1 is null) or (depotcode2) is null then 
  return;
  else
  open cur_return for
    select              
          sum(NORMAL_SCHEMES) as 正班班次,
          sum(OVERTIME_SCHEMES) as 加班班次,
          sum(NORMAL_SEATS) as 正班座位数,
          sum(OVERTIME_SEATS) as 加班座位数 
          from tranficstat
     where senddate >= startdate1 
              and senddate < enddate1+ 1
      and depot = depotcode1
    group by depot
   
    union
   
    select
          sum(NORMAL_SCHEMES) as 正班班次,
          sum(OVERTIME_SCHEMES) as 加班班次,
          sum(NORMAL_SEATS) as 正班座位数,
          sum(OVERTIME_SEATS) as 加班座位数
          from tranficstat
     where senddate >= startdate1 
              and senddate < enddate1 + 1
      and depot = depotcode2
    group by depot;
    end case;  
end HY_CONTRAST_PERIOD;

通过union,你期望返回两条记录,却发现永远总是只返回一条记录。问题症结发生在日期格式转换上,参数传进的格式为‘dd-mm-rr’,而条件左侧的记载格式为‘yyyy-mm-dd’,只要把所有右侧条件更改成如
  where senddate >= to_date(to_char( startdate1,'yyyy-mm-dd'),'yyyy-mm-dd')
           and senddate < to_date(to_char( enddate1,'yyyy-mm-dd'),'yyyy-mm-dd') + 1;
即可消除症状。

当然也可以修改左侧的格式,总之使两边的日期格式匹配;另外当然也可以直接修改系统的NLS_DATE_FORMAT 。
 



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值