how to set parameter for date plus days in oracle - sql

i have problem to add set parameter for date plus days in oracle. i have two parameter. that is one for date and one for day. I have tried that way but not successful.
This my query:
SELECT DISTINCT
mhn.id_mohon,
mhn.kod_urusan,
mhn.kod_caw,
nvl(to_char(mhn.trh_masuk, 'DD/MM/YYYY'), '-') trh_mohon,
nvl(to_char(mhn.trh_masuk, 'MM'), '-') bln
FROM mohon mhn
WHERE mhn.trh_masuk = to_date(:p_date1, 'DD/MM/YYYY') + INTERVAL :days DAY;
Can you check what the problem is my parameter?

I expect that you need:
to_date(:p_date1,'DD/MM/YYYY') + :days
or:
to_date(:p_date1,'DD/MM/YYYY') + :days * INTERVAL '1' DAY

An interval literal shows that the interval length has to be a quoted fixed value; it cannot be a variable.
You can use the numtodsinterval() function instead:
mhn.trh_masuk = to_date(:p_date1,'DD/MM/YYYY') + numtodsinterval(:days, 'DAY')
Or you could just add the number of days to the date:
mhn.trh_masuk = to_date(:p_date1,'DD/MM/YYYY') + :days

Related

Oracle APEX report - processing a field value

I have been creating Oracle APEX reports for a while.
I need to processes a column value to charge it's format.
The current SQL looks like this. All is good.
select PP_RUNDATA.ID as ID,
PP_RUNDATA.PP_START_TIME as PP_START_TIME,
PP_RUNDATA.PP_END_TIME as PP_END_TIME,
from PP_RUNDATA PP_RUNDATA
The time columns are in seconds. This PL/SQL converts the seconds into date / time. (this work well)
alter session set nls_date_format="dd/mm/yyyy - hh24:mi:ss";
select date '1970-01-01' + 1661596871 * interval '1' second result from dual;
I need to bring together the SQL and PL/SQL and change the 1661596871 to the column value.
I know I can use a "List Value" to process the PP_START_TIME / PP_END_TIME column using PL/SQL returning SQL.
A very poor, not working example,
declare
temp1 varchar2(500);
begin
EXECUTE IMMEDIATE 'alter session set nls_date_format="dd/mm/yyyy - hh24:mi:ss"';
select date '1970-01-01' + PP_START_TIME * interval '1' second into temp1 from dual;
return 'select temp2 from dual';
End;
I know there are more than two things wrong with this coding;
1) Syntax to access data in column PP_START_TIME (maybe &PP_START_TIME.)
2) the "return statement"
What am I doing wrong in the coding / my thinking in the above or maybe the overall approach in Oracle APEX Reports?
Thanks for looking
Pete
You can use:
select ID,
DATE '1970-01-01' + PP_START_TIME * INTERVAL '1' SECOND as PP_START_TIME,
DATE '1970-01-01' + PP_END_TIME * INTERVAL '1' SECOND as PP_END_TIME
from PP_RUNDATA
If you want a particular format then you can use TO_CHAR:
select ID,
TO_CHAR(
DATE '1970-01-01' + PP_START_TIME * INTERVAL '1' SECOND,
'dd/mm/yyyy - hh24:mi:ss'
) as PP_START_TIME,
TO_CHAR(
DATE '1970-01-01' + PP_END_TIME * INTERVAL '1' SECOND,
'dd/mm/yyyy - hh24:mi:ss'
) as PP_END_TIME
from PP_RUNDATA

Filtering based on end date being within the next 30 days [duplicate]

How do I add days to a timestamp? If my timestamp is 01-JAN-2011 11-09-05 and I add 2 days, I want 03-JAN-2011 11-09-05.
select '01-jan-2011 11-09-05' + interval '2' day
A completely Oracle-centric solution is to simply add 2 to the timestamp value as the default interval is days for Oracle dates/timestamps:
SELECT TO_TIMESTAMP('01-jan-2011 11-09-05','DD-Mon-YYYY HH24-MI-SS') + 2
FROM dual;
In a similar case, I used:
SELECT TO_TIMESTAMP('01-jan-2011 11-09-05','DD-Mon-YYYY HH24-MI-SS') + NUMTODSINTERVAL(2, 'DAY')
Because, othewise, the expression is converted to DATE and precission is lost. See: NUMTODSINTERVAL documentation

How can i pass variable with in single quote oracle sql

select
to_char(sysdate + interval '2' hour,'hh12:mi AM') as Time
from dual
i have a query that will add 2 hours from current system time , but it may add aur subtract the time and also hour's value will also be dynamic
so i have to use operator value it may + or - and similarly hour value it may 2 ,3 ,4 or 5 so my query will be
select
to_char(sysdate :operator interval ':hourvalue' hour,'hh12:mi AM') as Time
from dual
it gives me error
ORA-00907: missing right parenthesis
please help me out i am using oracle 11g
You cannot have an INTERVAL literal of a variable amount; however, you can have a fixed INTERVAL literal and then multiply it by a bind variable:
SELECT TO_CHAR(
SYSDATE + :hourvalue * INTERVAL '1' HOUR,
'hh12:mi AM'
) AS TIME
FROM DUAL
If you want a negative amount then just specify a negative :hourvalue rather than using a separate :operator bind variable.
You can use NUMTODSINTERVAL(n, 'hour') where n can has a negative value as well.
For example
SELECT NUMTODSINTERVAL((-1)*10, 'hour') h FROM DUAL;
You can also create an interval -(1 hour 37 min 41 sec):
SELECT NUMTODSINTERVAL((-1) * (1*3600 + 37*60 + 41), 'second') hms FROM DUAL;
Note, n is a number (decimal), so this will work too:
SELECT NUMTODSINTERVAL((-1) * (1 + 37/60 + 41/3600), 'hour') hms FROM DUAL;
It's only possible to use bind variables for arguments, you cannot use operators as bind variables. A workaround is to multiply the interval by -1 for negative and 1 for positive.
SELECT
TO_CHAR(
SYSDATE + numtodsinterval((CASE WHEN :operator = '-' THEN -1 ELSE 1 END * :interval),'hour'),
'hh12:mi AM'
) as time
FROM DUAL;

how to replace date in postgres with a static number

I am writing a Postgres procedure and I want to replace only date part of date with some static no.
eg:-
varDate Date default '2018-05-21';
Say I want to make this date as '2018-05-08';
Can anyone tell how to achieve this.
Till now what i have tried is this
varDate := varDate - interval '1 day' * 21 + interval '1 day' * 8;
The above expression gives me proper results. But is there any shortcut to change only the date part of the date.
As far as I understand you want to change the day of the month to 8.
One way to do this is to "truncate" the date to the start of the month, then add 8 days:
vardate := date_trunc('month', vardate)::date + 8;
date_trunc returns a timestamp that's why the cast ::date is needed.
Another option is to "build" a date based on the existing date:
vardate := make_date(extract(year from vardate)::int, extract(month from vardate)::int, 8);
Another option is to add a number of days to the date so you land on the 8th day:
select vardate::date + (8 - extract(day from vardate) * interval '1 day'

sql query to fetch records between current date and minimum date

I am writing this sql query but it says now non-numeric character was found where a numeric was expected.Where is the mistake??
SELECT dr.*
from daily_data_reading dr
where pod_id='01611152005960'
and DAILY_READING_DATE BETWEEN to_date(' min(daily_reading_date)','DD-MM-YY HH24:MI:SS')+ interval '1' day
AND to_date('trunc(SYSDATE) 23:59:59','DD-MM-YY HH24:MI:SS') + interval '1' day;
You are using to_date() in the wrong way
It makes no sense to call to_date() on a date value to convert that date value to a date.
Also to_date() expects a string formatted as a valid date.
Neither 'min(daily_reading_date)' nor 'trunc(SYSDATE) 23:59:59' is a valid date string.
So
to_date('min(daily_reading_date)','DD-MM-YY HH24:MI:SS')+ interval '1' day
needs to be:
min(daily_reading_date) + interval '1' day
And
to_date('trunc(SYSDATE) 23:59:59','DD-MM-YY HH24:MI:SS') + interval '1' day
should be:
AND trunc(SYSDATE) + interval '1' day
So your complete query should be:
SELECT dr.*
from daily_data_reading dr
where pod_id = '01611152005960'
and DAILY_READING_DATE BETWEEN min(daily_reading_date) + interval '1' day
AND trunc(SYSDATE) + interval '1' day;
But the above query will just trigger the next error: you can't use min(daily_reading_date) without applying a group by clause.
But you should ask a new question for that - including sample data and table definitions.