views:

425

answers:

1

I have a table for orders and it has 2 column with DATE types: delivery_date and order_date. In my sqldeveloper, all the dates I've inserted via Java or by hand are in the format 10.01.25 for the 25th Jan of this year. When I try to see the the total_price for orders between one date and another, I force the format like this:

create or replace function calc_Outlays (init_date IN DATE,final_date IN date)return number is

v_total_enc FORNECIMENTO.TOTAL_ENC_FORNEC%TYPE;

begin
select f.total_enc_fornec into v_total_enc from fornecimento f where f.data_entrega between init_date and final_date;
return v_total_enc;

Exception 
When no_data_found then
Insert Into errors Values (0,'No supplyment costs found for dates between '||to_char(init_date)||' and '||to_char(final_date),systimestamp);
return -1;
end;

but I get -1 returned. I even try to do the query like this: select f.total_enc_fornec from fornecimento f where f.data_entrega = '10.01.24';
like this: select f.total_enc_fornec from fornecimento f where f.data_entrega = to_date('10.01.24','yy-mm-dd');
like this: select f.total_enc_fornec from fornecimento f where f.data_entrega < to_date('10.01.24'); and NOTHING is returned! but if I execute : select f.total_enc_fornec from fornecimento f where f.data_entrega <= sysdate; it prints a result as it's supposed to...

How can I do this? I'm clearly not passing correctly the parameters nor in the function, neither executing the query itself

Btw, and if I wanted to select ALL the orders of some year/month/day? Say, using extract function for example. Could you please show me how? I've tried, but I'm having the same problems and I it's simple in concept, at least, lol.

+3  A: 

The Oracle DATE type stores a date+time down to the nearest second - so if you say f.data_entrega = '10.01.24' it will not match a record where f.data_entrega is 10.01.24 at 6am. SYSDATE, similarily, returns the current time as well as today's date.

If you want your function to match on the date portion only (i.e. ignore any time values), you may need to change the function:

select f.total_enc_fornec into v_total_enc
from fornecimento f
where f.data_entrega between TRUNC(init_date) and TRUNC(final_date) + 0.99999;
Jeffrey Kemp
+1, but syntax with between not clear, at least for illustration purpose, `(f.data_entrega >= trunc(init_date)) and (f.data_entrega < (trunc(final_date) + 1))`
ThinkJet
Thanks, that worked just fine. But what if I want to extract only the orders based on the year (month or day)?
neverMind
You can restrict the trunc to year/month/day: trunc(date) for beginning of day; trunc(date, 'MM') for beginning of month; trunc(date, 'YYYY') for beginning of year.
Nick Pierpoint
you could use `TRUNC(f.data_entrega,'MM')` to filter by month (`'MM'`) or year (`'YYYY'`), however if there is an index on `f.data_entrega` and you want to have a chance of using it, you could use `ADD_MONTHS` - e.g. `f.data_entrega BETWEEN TRUNC(init_date) AND ADD_MONTHS(TRUNC(init_date), 1) - 0.00001` (for a year, change 1 to 12).
Jeffrey Kemp