oracle difference between two dates - sql

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.

Related

Select day or month from a timestamp and assign into a variable

I have a table with hundreds of rows and I want to assgin the day of the first row in a variable, also a month in other variable.
The column is a timestamp format.
Right now I have a stored procedure like this, but I think it doesn't work.
CREATE OR REPLACE PROCEDURE AUTO_FLTER_BY_DATE IS
startDay number;
startMonth number;
endDay number;
endMonth number;
BEGIN
SELECT EXTRACT(DAY FROM tstamp) INTO startDay FROM "SYSTEM"."TEST_DATES";
DBMS_OUTPUT.PUT_LINE(startDay);
END AUTO_FLTER_BY_DATE;
And this is an example of the timestamp column
01-FEB-20 01.30.05.024000000 AM
You can simply do the following
to_char(tstamp, 'DAY')
CREATE OR REPLACE PROCEDURE AUTO_FLTER_BY_DATE IS
startDay number;
startMonth number;
endDay number;
endMonth number;
BEGIN
SELECT to_char(tstamp, 'DAY') INTO startDay FROM "SYSTEM"."TEST_DATES";
DBMS_OUTPUT.PUT_LINE(startDay);
END AUTO_FLTER_BY_DATE;
How about this:
create or replace procedure auto_flter_by_date
as
startDay number;
startMonth number;
begin
select extract(day from min(tstamp)) as startday
, extract(month from min(tstamp)) as startmonth
into startDay, startMonth
from test_dates;
dbms_output.put_line('First day: '||startDay);
dbms_output.put_line('First Month: '||startMonth);
end;
I advise not touching the SYSTEM schema, or using double-quotes.

Function Friday13 sql

I am reviewing the functions but I cannot solve this one;
Write a function that returns every Friday 13th during a specific year.
Example:
SELECT * FROM martes13(2020);
13/01/2020
13/03/2020
13/08/2020
My unfortunate attempt, do not pay much attention.
DECLARE
diaInicial date;
diaFinal date;
anio1 date;
anio2 date;
auxData date;
dates date[];
BEGIN
diaInicial := ('01/' || '01/' || anio ) :: date;
diaFinal := diaInicial + '1 YEAR' :: interval;
anio2:= date_part('year',diaFinal);
FOR i IN 1..12 BY 1 LOOP
FOR j IN 1..30 BY 1 LOOP
diaInicial := anio || '-' || i || '-' || j;
if(date_part('dom',auxData)==13 and date_part('dow',auxData)==5)then
dates[j] := diaInicial;
end if;
end loop;
end loop;
return dates;
END;
There's no way to solve it no matter how hard I try, I understand that I have to use dates, years intervals and counters but it does not work out. Any help or information could be of use to me.
Thanks in advance.
That can be solved with a simple SQL statement:
SELECT CAST(d AS date)
FROM generate_series(
TIMESTAMP '2020-01-13',
TIMESTAMP '2020-12-13',
INTERVAL '1 month'
) AS thirteen(d)
WHERE EXTRACT (dow FROM d) = 5;
You could wrap that in an SQL function.

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)
;

sysdate-1 not working in Oracle Jobs

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);