sysdate-1 not working in Oracle Jobs - sql

I have a stored procedure in Oracle. I want to call it in JOBS of Oracle.
DECLARE
P_DATE DATE;
BEGIN
P_DATE := TO_DATE ('19/10/2016', 'DD/MM/YYYY');
MITRA.PENJUALAN_ANTAR_CABANG.REPORTKONSOLRK ( P_DATE );
COMMIT;
END;
the result is right when I execute P_DATE as '19/10/2016'. But when I change it like this below the result is not display.
DECLARE
P_DATE DATE;
BEGIN
P_DATE := TO_DATE (TRUNC (SYSDATE - 1), 'DD/MM/YYYY');
MITRA.PENJUALAN_ANTAR_CABANG.REPORTKONSOLRK (P_DATE);
COMMIT;
END;
What is my query mistake, while if I run select trunc(SYSDATE-1) from dual the date was right.

SYSDATE - 1 is already a date. Passing it to to_date will produce a syntax error. Just drop the to_date call and you should be fine:
P_DATE := TRUNC(SYSDATE - 1);

Related

Oracle APEX SQL Date Time Import Procedure

I have a field (v_date) that contains date and time value e.g.(30-07-2021 13:30)
I want to import to my table two new dates and time values according to this field.
The first will be seven days later (08-06-2021 13:30), and the second one will be 7 days and one hour later (08-06-2021 14:30).
This is my code:
create or replace PROCEDURE "PR_ADD_MASSIVE_SESSIONS"
(v_date date)
IS
v_counter_day number :=7;
v_session_temp_date date;
v_session_temp_end date;
BEGIN
v_session_temp_date := to_date(v_date, 'MM-DD-YYYY HH24:MI') + v_counter_day;
v_session_temp_end := to_date(v_date, 'MM-DD-YYYY HH24:MI') + (v_counter_day + (1/1440*60));
INSERT INTO SESSIONS (CLIENTS_ID, SESSION_AREA, SESSION_DATE, SESSION_STOP, PAYMENT_TYPE_ID, PRICE, STATUS, COMPLETED, PAYMENT_STATUS, SESSION_NO)
VALUES (5, 1, v_session_temp_date, v_session_temp_end, 2, 40, 1, 1, 1, 1);
END;
And I get this error: Ajax call returned server error ORA-01843: not a valid month.
I try with to_char but the compiler only wants to_date in order to have a successful procedure.
The variables v_session_temp_date and v_session_temp_date are defined as DATE. The function TO_DATE converts a string to a date. It should not be used on variables of type DATE. Remove the TO_DATE and you're good to go. Example code below (using an anonymous block, not a procedure)
CREATE TABLE sessions
(
clients_id NUMBER,
session_area NUMBER,
session_date DATE,
session_stop DATE,
payment_type_id NUMBER,
price NUMBER,
status NUMBER,
completed NUMBER,
payment_status NUMBER,
session_no NUMBER
);
Table SESSIONS created.
--original code
DECLARE
v_date date := SYSDATE;
v_counter_day number := 7;
v_session_temp_date date;
v_session_temp_end date;
BEGIN
v_session_temp_date := to_date(v_date, 'MM-DD-YYYY HH24:MI') + v_counter_day;
v_session_temp_end := to_date(v_date, 'MM-DD-YYYY HH24:MI') + (v_counter_day + (1/1440*60));
INSERT INTO SESSIONS (CLIENTS_ID, SESSION_AREA, SESSION_DATE, SESSION_STOP, PAYMENT_TYPE_ID, PRICE, STATUS, COMPLETED, PAYMENT_STATUS, SESSION_NO)
VALUES (5, 1, v_session_temp_date, v_session_temp_end, 2, 40, 1, 1, 1, 1);
END;
/
ORA-01843: not a valid month
ORA-06512: at line 7
01843. 00000 - "not a valid month"
*Cause:
*Action:
--fixed code
DECLARE
v_date date := SYSDATE;
v_counter_day number := 7;
v_session_temp_date date;
v_session_temp_end date;
BEGIN
v_session_temp_date := v_date + v_counter_day;
v_session_temp_end := v_date + (v_counter_day + (1/1440*60));
INSERT INTO SESSIONS (CLIENTS_ID, SESSION_AREA, SESSION_DATE, SESSION_STOP, PAYMENT_TYPE_ID, PRICE, STATUS, COMPLETED, PAYMENT_STATUS, SESSION_NO)
VALUES (5, 1, v_session_temp_date, v_session_temp_end, 2, 40, 1, 1, 1, 1);
END;
/
PL/SQL procedure successfully completed.

Get first and last day of month into variables - Oracle

I would like to declare some variables that contain the first and last date of the current month in Oracle. I know how to get these values, but evidently not store or use them; I am more of a T-SQL guy.
In T-SQL, I could write:
DECLARE #startDate DATE = GETDATE();
DECLARE #endDate DATE = EOMONTH(GETDATE());
SELECT *
FROM SomeTable
WHERE SomeDate BETWEEN #startDate AND #endDate;
I cannot, for the life of me, work out how to do this in Oracle. I have tried several variations, like:
DECLARE END_DT DATE := TRUNC(LAST_DAY(SYSDATE))
END_DT DATE := TRUNC(LAST_DAY(SYSDATE))
DECLARE END_DT DATE;
SELECT TRUNC(LAST_DAY(SYSDATE)) INTO END_DT FROM DUAL;
I am mostly using 11g, but I would like to be able to use the same script on a 9i/10g server also.
You can use such a query to detect first and last days of the current month :
SELECT TRUNC(LAST_DAY(ADD_MONTHS(SYSDATE,-1)))+1,
TRUNC(LAST_DAY(SYSDATE))
INTO :startDate,:endDate
FROM DUAL;
with the contribution of ADD_MONTHS() function
Update : Alternatively use this PL/SQL code block :
DECLARE
startDate date;
endDate date;
BEGIN
startDate := TRUNC(LAST_DAY(ADD_MONTHS(SYSDATE,-1)))+1;
endDate := TRUNC(LAST_DAY(SYSDATE));
DBMS_OUTPUT.PUT_LINE ('startDate : '||startDate);
DBMS_OUTPUT.PUT_LINE ('endDate : '||endDate);
END;
/
Demo
You can use the LAST_DAY and TRUNC combination as following:
DECLARE
START_DT DATE := TRUNC(SYSDATE, 'MM');
END_DT DATE := TRUNC(LAST_DAY(SYSDATE));
BEGIN
--DBMS_OUTPUT.PUT_LINE(START_DT || ' ' || END_DT);
SELECT
*
INTO <...>
FROM
SOMETABLE
WHERE
SOMEDATE BETWEEN START_DT AND END_DT;
END;
/
Cheers!!

Calculating the working days between two dates in oracle

Below is my function where i tried to calculate the number of working days between two dates.
CREATE OR REPLACE function workingdays
( start_date IN DATE , end_date IN DATE)
return number
as
total_days varchar2(10);
v_count integer:= 0;
end_value date := to_date(end_date , 'YYYY-MM-DD');
start_value date := to_date(start_date , 'YYYY-MM-DD');
date_diff number(10);
begin
while(start_value <= end_value)
loop
if (to_char(TO_DATE(start_value,'YYYY-MM-DD') , 'D') = 1 or to_char(TO_DATE(start_value,'YYYY-MM-DD'), 'D') = 7)
then v_count := v_count+1;
end if;
start_value := start_value + 1;
end loop;
date_diff :=to_number( to_date( end_date, 'YYYY-MM-DD') - to_date (start_date , 'YYYY-MM-DD')) ;
total_days := to_char( (to_number(date_diff) - to_number(v_count)) + 1);
return (' The total working days is' || to_number((total_days)));
end;
/
The function compiles successfully, but on executing it, I get an error message at the return line. Could someone guide me. It is some issue the number/character conversion.
You make a lot of useless conversions, almost all of them can be skipped.
Result of TO_CHAR(..., 'D') depends on current user NLS_TERRITORY value, so you should not use it unless you ensure a certain NLS_TERRITORY setting.
This one is working:
CREATE OR REPLACE FUNCTION workingdays(start_date IN DATE , end_date IN DATE) RETURN VARCHAR2 AS
total_days NUMBER;
v_count INTEGER:= 0;
end_value DATE := TRUNC(end_date);
start_value DATE := TRUNC(start_date);
date_diff NUMBER;
BEGIN
WHILE start_value <= end_value LOOP
IF TO_CHAR(start_value, 'fmDay', 'NLS_DATE_LANGUAGE = american') IN ('Saturday','Sunday') THEN
v_count := v_count + 1;
END IF;
start_value := start_value + 1;
END LOOP;
date_diff := end_value - TRUNC(start_date);
total_days := date_diff - v_count + 1;
RETURN ' The total working days is ' || total_days;
END;
/
There are many other possibilities, this is just another way of doing it:
CREATE OR REPLACE FUNCTION workingdays(start_date IN DATE, end_date IN DATE) RETURN VARCHAR2 AS
next_run_date DATE := TRUNC(start_date);
total_days INTEGER := 0;
BEGIN
LOOP
DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING('FREQ=DAILY;INTERVAL=1;BYDAY=MON,TUE,WED,THU,FRI', NULL, next_run_date, next_run_date);
EXIT WHEN next_run_date >= end_date;
total_days := total_days + 1;
END LOOP;
RETURN ' The total working days is ' || total_days;
END;
select min(a_day)
as from_date
,max(a_day)
as till_date
,sum(decode(to_char(a_day,'D')
,1,0
,7,0
,1
)
)
as wrk_days_count
from (select trunc(sysdate,'iw') + level - 1
as a_day
from dual
connect by level < 8)
;

Error: PL/SQL: Compilation unit analysis terminated?

Keep Getting the same error on both codes!!
DROP TABLE Date_Dimension CASCADE CONSTRAINTS ;
CREATE TABLE Date_Dimension
(
date_key NUMBER NOT NULL ,
full_date DATE ,
day_of_week NUMBER ,
day_num_in_month NUMBER ,
day_num_overall NUMBER ,
day_name VARCHAR2 (9) ,
day_abbrev VARCHAR2 (3) ,
week_num_in_year NUMBER ,
week_num_overall NUMBER ,
week_begin_date DATE ,
MONTH NUMBER ,
month_number_overall NUMBER ,
month_name VARCHAR2 (9) ,
month_abbrev VARCHAR2 (3) ,
quarter NUMBER ,
YEAR VARCHAR2 (20) ,
century NUMBER
) ;
ALTER TABLE Date_Dimension ADD CONSTRAINT Date_Dimension_PK PRIMARY KEY ( date_key ) ;
Create or replace PROCEDURE sp_DATE_DIMENSION(v_STARTDATE IN INT, v_END_YEAR IN INT) IS
v_STARTDATE DATE;
v_ENDDATE DATE;
v_STARTDATE Date := to_date('2005/01/01' || v_START_YEAR, 'YYYY/MM/DD');
v_ENDDATE Date := to_date('2020/12/31' || v_END_YEAR,'YYYY/MM/DD');
BEGIN
INSERT INTO
Date_Dimension
(date_key,full_date, day_of_week, day_num_in_month, day_num_overall, day_name, day_abbrev, week_num_in_year, week_num_overall, month, month_name, month_abbrev, quarter, year, century)
VALUES
(
'1',TO_DATE(v_STARTDATE, 'yyyy/mm/dd'), TO_NUMBER(v_STARTDATE, 'D'), TO_NUMBER(v_STARTDATE, 'DD'), TO_NUMBER(v_STARTDATE, 'DDD'), TO_CHAR(v_STARTDATE, 'DAY'), TO_CHAR(v_STARDATE, 'DY'), TO_NUMBER(v_STARTDATE, 'IW'), TO_NUMBER(v_STARTDATE, 'WW'), TO_NUMBER(v_STARTDATE, 'MM'), TO_CHAR (v_STARTDATE, 'MONTH'), TO_CHAR (v_STARTDATE, 'MON'), TO_NUMBER (v_STARTDATE, 'Q'), TO_CHAR (v_STARTDATE, 'YEAR'), TO_NUMBER (v_STARTDATE, 'CC')
)
;
IF v_STARTDATE > v_ENDDATE THEN
DBMS_OUTPUT.PUT_LINE ('ERROR IN CODE REGARDING DATES CHOSEN');
ELSE
WHILE v_STARTDATE <= V_ENDDATE LOOP
DBMS_OUTPUT.PUT_LINE ('Date : '||to_char(v_StartDate,'YYYY / MM / DD'));
v_STARTDATE := v_STARTDATE + 1;
END LOOP;
END IF;
END;
Your code gets
PLS-00410: duplicate fields in RECORD,TABLE or argument list are not permitted
You have duplicated the name startdate from the procedure's format argument list as a local variable; and then repeated both of them again. I think you meant the formal argument to just be the year, since it's a number. Your conversion to a date is also then wrong:
to_date('2005/01/01' || v_START_YEAR, 'YYYY/MM/DD')
... doesn't make sense, as you already have the year 2005 hard-coded.
I think for that part you want something more like:
create or replace PROCEDURE sp_DATE_DIMENSION(p_START_YEAR IN NUMBER, p_END_YEAR IN NUMBER) IS
l_START_DATE Date := to_date(p_START_YEAR ||'-01-01', 'YYYY-MM-DD');
l_END_DATE Date := to_date(p_END_YEAR ||'-01-01', 'YYYY-MM-DD');
BEGIN
with other variables references tweaked to match. In your insert you're passing the first value as the string '1' instead of the number 1. You then call to_date() against variables which are already dated; and you call to_number() with a format mask for a date element - for those you need to convert to a string, and then to a number. So that insert woudl become more like:
INSERT INTO Date_Dimension (date_key, full_date, day_of_week, day_num_in_month,
day_num_overall, day_name, day_abbrev, week_num_in_year, week_num_overall,
month, month_name, month_abbrev, quarter, year, century)
VALUES (1,
l_START_DATE,
TO_NUMBER(TO_CHAR(l_START_DATE, 'D')),
TO_NUMBER(TO_CHAR(l_START_DATE, 'DD')),
TO_NUMBER(TO_CHAR(l_START_DATE, 'DDD')),
TO_CHAR(l_START_DATE, 'DAY'),
TO_CHAR(l_START_DATE, 'DY'),
TO_NUMBER(TO_CHAR(l_START_DATE, 'IW')),
TO_NUMBER(TO_CHAR(l_START_DATE, 'WW')),
TO_NUMBER(TO_CHAR(l_START_DATE, 'MM')),
TO_CHAR(l_START_DATE, 'MONTH'),
TO_CHAR(l_START_DATE, 'MON'),
TO_NUMBER(TO_CHAR(l_START_DATE, 'Q')),
TO_CHAR (l_START_DATE, 'YEAR'),
TO_NUMBER(TO_CHAR(l_START_DATE, 'CC'))
);
It isn't good practice to use dbms_output for error messages, as (a) a calling program has no other indication that something is wrong, and (b) even in a simple client call a user may well not even have capture or display of those enabled. It's better to throw an exception:
IF l_START_DATE > l_END_DATE THEN
RAISE_APPLICATION_ERROR (-20001, 'ERROR IN CODE REGARDING DATES CHOSEN');
END IF;
WHILE l_START_DATE <= l_END_DATE LOOP
DBMS_OUTPUT.PUT_LINE ('Date : ' || to_char(l_START_DATE, 'YYYY / MM / DD'));
l_START_DATE := l_START_DATE + 1;
END LOOP;
END;
/
The exception will cause the procedure to terminate early, so you don't need the else part as nothing beyond that is reached anyway if the dates are wrong.
Even so, you probably really want to do the insert inside the loop so you create all the relevant rows; and it would make sense to do the check and exception throw right at the start (maybe comparing the years rather than the dates, but doesn't really matter. And probably other things I've forgotten - hopefully this will put you more towards the right track. You don't really need a procedure for this, or even PL/SQL, as it could be done in plain SQL and a single insert, but hopefully this is an exercise.

oracle difference between two dates

I need to find number of days between two dates.
Here's code:
declare
datum DATE;
sdate DATE;
shpord NUMBER;
rozdiel NUMBER;
MESSAGE VARCHAR(2000) := 'Nemozete Uvolnit VO c.';
begin
datum := TO_DATE('&OLD:NEED_DATE','DD.MM.YYYY');
sdate := TO_DATE(sysdate,'DD.MM.YYYY');
rozdiel := datum - sdate;
IF rozdiel > 5 THEN
ERROR_SYS.Record_General('Nemozete Uvolnit VO c','');
END IF;
end;
I get an error:
ORA-01858: a non-numeric character was found where a numeric was expected
Don't call to_date on a date. sysdate is already a date. If you want to subtract midnight on the current day from datum
declare
datum DATE;
sdate DATE;
shpord NUMBER;
rozdiel NUMBER;
MESSAGE VARCHAR(2000) := 'Nemozete Uvolnit VO c.';
begin
datum := TO_DATE('&OLD:NEED_DATE','DD.MM.YYYY');
sdate := trunc(sysdate);
rozdiel := datum - sdate;
IF rozdiel > 5 THEN
ERROR_SYS.Record_General('Nemozete Uvolnit VO c','');
END IF;
end;
Change this line:
sdate := TO_DATE('sysdate','DD.MM.YYYY');
to:
sdate := sysdate;
You're trying to convert the STRING 'sysdate' to a date, rather than the function result of sysdate.
also, watch your nls settings, if you date format isn't set to 'dd.mm.yyyy' you're going to get an error.