Get first and last day of month into variables - Oracle - sql

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!!

Related

How do declare date variables in PL/SQL?

I am new to PL/SQL and don't understand how to declare date variables. How would I write this T-SQL script in PL/SQL?
declare #date1 as date
declare #date2 as date
set #date1 = '2022-06-01'
set #date2 = '2022-06-30'
select *
from example_table
where example_date between #date1 and #date2
Declaring variables is easy.
Problem comes when you want to do something with the select statement as PL/SQL requires you to select INTO something (a local variable, set of local variables, ref cursor, collection, ...). You didn't say what you'd want to do afterwards, so the following example
selects number of rows that satisfy condition
loops through the table and does nothing (null;) for each loop iteration.
You'd do something else, I presume.
declare
date1 date := date '2022-06-01';
date2 date := date '2022-06-30';
l_cnt number;
begin
select count(*)
into l_cnt
from example_table
where example_date between date1 and date2;
for cur_r in (select *
from example_table
where example_date between date1 and date2
)
loop
null;
end loop;
end;
/

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.

Oracle select with variables

Declare startdate date;
enddate date;
lastname nvarchar2(30);
Begin
Lastname := 'ZZZ';
startdate := '04-JAN-18';
enddate := '02-JUN-18';
insert into temp_feed
select empname, startdate, enddate , activityid, source, actname from emp_activity
where startdate >= startdate and enddate <= enddate and lower(LASTNM) like lower(lastname||'%')
End;
when I hardcode date values its working fine, but when I use variables the result is null.
am I missing something?
its recommended to use V_ before the name of variables and also P_ before the name of parameters, the problem in your code is you are comparing a value in table with itself at:
startdate >= startdate and enddate <= enddate
I do format your code and use the standard naming format here:
DECLARE
V_STARTDATE DATE;
V_ENDDATE DATE;
V_LASTNAME NVARCHAR2 (30);
BEGIN
V_LASTNAME := 'ZZZ';
V_STARTDATE := TO_DATE('04-JAN-18', 'DD-MON-YY');
V_ENDDATE := TO_DATE('02-JUN-18', 'DD-MON-YY');
INSERT INTO TEMP_FEED
SELECT EMPNAME, STARTDATE, ENDDATE, ACTIVITYID, SOURCE, ACTNAME
FROM EMP_ACTIVITY
WHERE STARTDATE >= V_STARTDATE
AND ENDDATE <= V_ENDDATE
AND LOWER (LASTNM) LIKE LOWER (V_LASTNAME || '%');
END;
I would recommend you to convert your variable to Date format first.
if you use function and it gets an parameter from outside, we can p_start_date for a date. Then you can try to convert out parameter to date format for sure.
v_start_date := to_date(p_start_date,'dd-MON-yyyy') ;
also in where clause
to_date(startdate,'dd-MON-yyyy') >= to_date(p_start_date,'dd-MON-yyyy')

Oracle - break dates into quarters

Given 2 dates (StartDate and EndDate), how to do i generate quarterly periods in Pl/SQL.
Example:
Start Date: 01-JAN-2009
End Date: 31-DEC-2009
Expected Output:
StartDate EndDate
01-JAN-2009 31-MAR-2009
01-APR-2009 30-JUN-2009
01-JUL-2009 30-SEP-2009
01-OCT-2009 31-DEC-2009
SELECT ADD_MONTHS( TRUNC(PARAM.start_date, 'Q'), 3*(LEVEL-1) ) AS qstart
, ADD_MONTHS( TRUNC(PARAM.start_date, 'Q'), 3*(LEVEL) ) -1 AS qend
FROM ( SELECT TO_DATE('&start_date') AS start_date
, TO_DATE('&end_date') AS end_date
FROM DUAL
) PARAM
CONNECT BY ADD_MONTHS( TRUNC(PARAM.start_date, 'Q'), 3*(LEVEL) ) -1
<= PARAM.end_date
Rules for params, you may need to adjust the query to suit your purposes:
If start_date is not exact quarter start it effectively uses the quarter contain start date.
If end_date is not exact quarter end then we end on the quarter that ended BEFORE end_date (not the one containing end date).
Here's one way that you can do it with PL/SQL
declare
startDate Date := '01-JAN-2009';
endDate Date := '31-DEC-2009';
totalQuarters number := 0;
begin
totalQuarters := round(months_between(endDate, startDate),0)/3;
dbms_output.put_line ('total quarters: ' || totalQuarters);
for i in 1..totalQuarters loop
dbms_output.put_line('start date: '|| startDate || ' end date:' || add_months(startDate -1,3));
startDate := add_months(startDate,3) ;
end loop;
end;

Difference between two dates

I have a table that has the following data
fromDate | toDate
20JAN11 | 29DEC30
Both dates are for the 21st Century (i.e. 2011 and 2030) but only the last two characters are stored.
Why is the following statement (when run from within a PL/SQL module) against the above data always returns a positive value
dateDifference := (fromDate - toDate)
If i run the following statement from sqlplus i get the correct negative value which is correct.
select to_date('20JAN11','DDMONYY')-to_Date('29DEC30','DDMONYY') from dual;
I remember reading somewhere that Oracle would sometimes use the wrong century but i dont quite remember the exact scenario where that would happen.
Assuming those columns are of DATE datatype, which seems to be the case: Oracle always stores DATE values in an internal format which includes the full year. The fact that you are seeing only a 2-digit year has to do with the date format used to convert the date to a string for display. So most likely the stored century values are not what you think they are.
Try selecting the dates with an explicit format to see what you really have stored:
SELECT TO_CHAR( fromDate, 'DD-MON-YYYY' ), TO_CHAR( toDate, 'DD-MON-YYYY' )
Seems to work for me either way on my 10g database:
SQL> set serveroutput on
SQL>
SQL> DECLARE
2 d1 DATE := to_date('20JAN11','DDMONRR');
3 d2 DATE := to_date('29DEC30','DDMONRR');
4 diff INTEGER;
5 BEGIN
6 diff := d1 - d2;
7 dbms_output.put_line(diff);
8 END;
9 /
-7283
PL/SQL procedure successfully completed
SQL>
EDIT: works for YY instead of RR year format as well.
EDIT2: Something like this, you mean?
SQL> create table t (d1 date, d2 date);
Table created
SQL> insert into t values (to_date('20JAN11','DDMONYY'), to_date('29DEC30','DDMONYY'));
1 row inserted
SQL> commit;
Commit complete
SQL>
SQL> DECLARE
2 R t%ROWTYPE;
3 diff INTEGER;
4 BEGIN
5 SELECT d1, d2
6 INTO R
7 FROM t;
8 diff := R.d1 - R.d2;
9 dbms_output.put_line(diff);
10 END;
11 /
-7283
PL/SQL procedure successfully completed
SQL>
As #Alex states, you may want to verify your data.
works without formatting as well
CREATE TABLE DATETEST(FROMDATE DATE, TODATE DATE);
insert into DATETEST (fromdate,todate) values (to_date('20Jan11','ddMonrr'),to_date('29DEC30','ddMonrr'));
SELECT TO_CHAR(FROMDATE,'ddMonrrrr hh24:mi:ss') FROMDATE,
TO_CHAR(TODATE,'ddMonrrrr hh24:mi:ss') TODATE
from datetest ;
/*
FROMDATE TODATE
------------------ ------------------
20Jan2011 00:00:00 29Dec2030 00:00:00
*/
set serveroutput on
DECLARE
l_FROMDATE DATETEST.FROMDATE%type ;
L_TODATE DATETEST.TODATE%TYPE;
dateDifference number;
BEGIN
--notice -- no formatting just putting them into a variable for test
SELECT FROMDATE, TODATE
INTO L_FROMDATE, L_TODATE
from datetest;
DATEDIFFERENCE := L_FROMDATE - L_TODATE ;
DBMS_OUTPUT.PUT_LINE('DATEDIFFERENCE = ' || DATEDIFFERENCE );
end ;
--DATEDIFFERENCE = -7283
SELECT FROMDATE-TODATE
from datetest ;
/* --still not formatting
FROMDATE-TODATE
----------------------
-7283
*/
SELECT (FROMDATE - TODATE) DATEDIFF,
TO_CHAR(FROMDATE,'ddMonrrrr') FROMDATE,
to_char(todate,'ddMonrrrr') todate
from (
SELECT TO_DATE('20JAN11','DDMONYY') FROMDATE,
TO_DATE('29DEC30','DDMONYY') TODATE
FROM DUAL)
;
/*
DATEDIFF FROMDATE TODATE
---------------------- --------- ---------
-7283 20Jan2011 29Dec2030
*/
try running the first query on your table:
SELECT TO_CHAR(FROMDATE,'ddMonrrrr hh24:mi:ss') FROMDATE,
TO_CHAR(TODATE,'ddMonrrrr hh24:mi:ss') TODATE
from datetest ;
see if the years are what you actually expect.
(Edit: changed to use two digit years)