查询的时候如果不加上 and hqfhz.begindate <= v_enddate_date and hqfhz.enddate > v_begindate_date; 的话,就能查询出来呀,所以我觉得是这有问题,关键是 v_begindate_date := to_date(begindate,'yyyy-mm-dd'); 这个转变对不对,我在test的时候,v_begindate_date变成了 2月2004年19日,是不是有什么关系??
to_char(hqfhz.begindate,'yyyymmdd')然后比
你的两个日期返回的形式不相同,你要么把v_begindate_date := to_date(begindate,'yyyy-mm-dd');改成v_begindate_date := to_date(begindate,'m月yyyy年dd日');这样,如果客户端session的日期format变了,你也会受到影响。所以建议你这样修改: and to_char(hqfhz.begindate,'yyyymmdd') <= to_char(v_enddate_date,'yyyymmdd') and to_char(hqfhz.enddate,'yyyymmdd') > to_char(v_begindate_date,'yyyymmdd'); 这样就不会有问题了。
同意leborety, 变成字符串比较更加保险!!!
select count(*) from hqfhz,zhdz where zhdz.accountclass ='1' and zhdz.accountatt = deptcode and zhdz.accountno = hqfhz.accountno and zhdz.accounttype ='2' and hqfhz.begindate <= to_date(2004-03-19,'yyyy-mm-dd') and hqfhz.enddate > to_date(2004-02-19,'yyyy-mm-dd'); ........--日期之间可以直接比较的.你把上面的一句单独拿出来调试一下,看是否有返回/
and hqfhz.begindate <= v_enddate_date
and hqfhz.enddate > v_begindate_date;
的话,就能查询出来呀,所以我觉得是这有问题,关键是
v_begindate_date := to_date(begindate,'yyyy-mm-dd');
这个转变对不对,我在test的时候,v_begindate_date变成了 2月2004年19日,是不是有什么关系??
and to_char(hqfhz.begindate,'yyyymmdd') <= to_char(v_enddate_date,'yyyymmdd')
and to_char(hqfhz.enddate,'yyyymmdd') > to_char(v_begindate_date,'yyyymmdd');
这样就不会有问题了。
where zhdz.accountclass ='1'
and zhdz.accountatt = deptcode
and zhdz.accountno = hqfhz.accountno
and zhdz.accounttype ='2'
and hqfhz.begindate <= to_date(2004-03-19,'yyyy-mm-dd')
and hqfhz.enddate > to_date(2004-02-19,'yyyy-mm-dd');
........--日期之间可以直接比较的.你把上面的一句单独拿出来调试一下,看是否有返回/
v_begindate_date := to_date(begindate,'yyyy-mm-dd');
这个转变对不对,我在test的时候,v_begindate_date变成了 2月2004年19日,
-----
如果hqfhz.enddate,begindate这两个字段是date类型的
不用考虑转换后日期的形式,都是date类型的就可以进行比较了。
and to_char(hqfhz.enddate,'YYYY-MM-DD') > v_begindate_date;这时一定要注意转换的格式了,一定要使用大写的‘YYYY-MM-DD’才能保证转换成2004-09-15,形式的字符串。
to_date(to_char(hqfhz.begindate, 'YYYY-MM-DD'),'YYYY-MM-DD')
to_date(to_char(hqfhz.enddate , 'YYYY-MM-DD'),'YYYY-MM-DD');
日期型比较比字符型比较更精确些。