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

王朝oracle·作者佚名  2006-01-09
宽屏版  字体: |||超大  

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 。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
>>返回首页<<
© 2005- 王朝网络 版权所有