I want to insert some data into a table associated with dates for the next year. I actually only need workdays inserted.
BEGIN
FOR i IN 1..365 LOOP
INSERT INTO MY_TABLE (ID, MY_DATE)
VALUES (i, (to_date(sysdate,'DD-MON-YY')-1)+i);
END LOOP;
END;
I can solve my problem by going back and deleting rows that are weekend days, but that seems rather inelegant - can anyone think of a way to modify my loop so that it skips weekends?
You could always check the day of the week before inserting the row (the names of the days of the week will depend on your NLS settings so this isn't the most robust solution possible)
BEGIN
FOR i IN 1..365 LOOP
IF( to_char(sysdate-1+i,'fmDAY') NOT IN ('SATURDAY', 'SUNDAY') )
THEN
INSERT INTO MY_TABLE (ID, MY_DATE)
VALUES (i, (to_date(sysdate,'DD-MON-YY')-1)+i);
END IF;
END LOOP;
END;
I would suggest using to_date(your_date,'d') as #Jeff Moore mentions. However, I'd also suggest getting rid of the for..loop. As a bonus, this will add all days of any given year, unlike your version, which will generate an extra day on leap years:
INSERT INTO MY_TABLE (ID, MY_DATE)
SELECT lvl, dt
FROM ( SELECT LEVEL lvl,
TO_DATE('1/1/2011', 'mm/dd/yyyy') + LEVEL - 1 dt
FROM DUAL
CONNECT BY TO_DATE('1/1/2011', 'mm/dd/yyyy') + LEVEL - 1 <
ADD_MONTHS(TO_DATE('1/1/2011', 'mm/dd/yyyy'), 12))
WHERE TO_CHAR(dt, 'd') NOT IN (1, 7)
If you want your "ID" column to be contiguous, you can use rownum instead of lvl in the outer query.
You can use one of the following date formats to check which day it is.
select to_char(sysdate,'DAY') from dual; /* TUESDAY */
select to_char(sysdate,'D') from dual; /* 3 */
select to_char(sysdate,'DY') from dual; /* TUE */
Add the if statement as shown below to remove days that equal SAT or SUN.
BEGIN
FOR i IN 1..365 LOOP
IF to_char(sysdate-1+i,'DY') NOT in ('SAT','SUN') THEN
INSERT INTO MY_TABLE (ID, MY_DATE) VALUES (i, (to_date(sysdate,'DD-MON-YY')-1)+i);
END IF;
END LOOP;
END;
If you let Monday = 0 and Sunday = 6 you could use (if mod(i,7) < 4 )) then Insert... should work.
Related
I need to insert only date into a table MONTH_YEAR from 01-01-2010 to 01-01-2040:
For example, I need to insert record on my table month wise
DATE:
01-01-2010
01-02-2010
01-03-2010
01-04-2010
01-05-2010
01-06-2010
01-07-2010
01-08-2010
01-09-2010
01-10-2010
01-11-2010
01-12-2010
01-01-2011
01-02-2011
01-03-2011
01-04-2011
01-05-2011
.....................................
01-06-2040
01-07-2040
01-08-2040
01-09-2040
01-10-2040
01-11-2040
01-12-2040
Like this I want to insert only date into my table for month wise from 01-01-2010 to 01-01-2040
You can use a hierarchical query:
INSERT INTO month_year (column_name)
SELECT ADD_MONTHS(DATE '2010-01-01', LEVEL - 1)
FROM DUAL
CONNECT BY LEVEL <= 31*12;
Or a recursive query:
INSERT INTO month_year (column_name)
WITH range (dt) AS (
SELECT DATE '2010-01-01' FROM DUAL
UNION ALL
SELECT ADD_MONTHS(dt, 1)
FROM range
WHERE dt < DATE '2040-12-01'
)
SELECT dt FROM range;
db<>fiddle here
Row generator it is:
SQL> insert into month_year (datum)
2 select date '2010-01-01' + level - 1
3 from dual
4 connect by level <= date '2040-01-01' - date '2010-01-01' + 1;
10958 rows created.
SQL> select min(datum) min_date,
2 max(datum) max_date
3 from month_year;
MIN_DATE MAX_DATE
---------- ----------
01.01.2010 01.01.2040
SQL>
If you only need 1st of every month, then
SQL> insert into month_year (datum)
2 select add_months(date '2010-01-01', level - 1)
3 from dual
4 connect by level <= months_between(date '2040-01-01', date '2010-01-01') + 1;
361 rows created.
SQL>
Do you really need 40 years of dates, it seems unlikely or can you make due with a virtual calendar, where you specify a start and end_date and all the dates are generated for you. This example does every day in the range but feel free to modify it to produce what you need. I use it in several places.
CREATE OR REPLACE TYPE nt_date IS TABLE OF DATE;
CREATE OR REPLACE FUNCTION generate_dates_pipelined(
p_from IN DATE,
p_to IN DATE
)
RETURN nt_date PIPELINED DETERMINISTIC
IS
v_start DATE := TRUNC(LEAST(p_from, p_to));
v_end DATE := TRUNC(GREATEST(p_from, p_to));
BEGIN
LOOP
PIPE ROW (v_start);
EXIT WHEN v_start >= v_end;
v_start := v_start + INTERVAL '1' DAY;
END LOOP;
RETURN;
END generate_dates_pipelined;
/
How to check if it's a weekend in SQL?
I know I can convert sysdate to a number using this
SQL> select to_char(sysdate, 'd') from dual;
TO_CHAR(SYSDATE,'D')
But I'm not really sure how to check if today is a 6 or 7.
Do not use TO_CHAR with the D format model for this as it is dependant on the NLS_TERRITORY session parameter.
For example, when SYSDATE = 2018-09-10 (a Monday):
ALTER SESSION SET NLS_TERRITORY = 'France';
SELECT TO_CHAR( SYSDATE, 'D' ) FROM DUAL;
Outputs 1 but the same query in a different territory:
ALTER SESSION SET NLS_TERRITORY = 'America';
SELECT TO_CHAR( SYSDATE, 'D' ) FROM DUAL;
Outputs 2.
Instead, you can use TRUNC and the IW format model:
SELECT TRUNC( SYSDATE ) - TRUNC( SYSDATE, 'IW' ) FROM DUAL
Outputs 0 for Monday (and 1 for Tuesday ... 6 for Sunday) and is independent of the NLS_TERRITORY setting.
So you could filter this to give weekends as:
SELECT *
FROM DUAL
WHERE TRUNC( SYSDATE ) - TRUNC( SYSDATE, 'IW' ) IN ( 5, 6 )
or
SELECT *
FROM DUAL
WHERE SYSDATE - TRUNC( SYSDATE, 'IW' ) >= 5
If you want the days 1-indexed for consistency with your expected output from TO_CHAR (rather then 0-indexed) then just add 1 to the value.
sysdate is a pseudo column. You don't need to query it, you can evaluate it directly:
IF TO_CHAR(SYSDATE, 'D') IN ('6', '7') THEN
-- Do something
END IF;
I would avoid the ambiguous 'D' format as this varies between territories (the week starts after the weekend where I live), and use
if to_char(sysdate,'fmDY','nls_date_language=English') like 'S%'
then
Regarding the 'D' format, unfortunately to_char doesn't let you specify nls_territory inline, so without an explicit alter session command, it will rely on the session settings at runtime. I've seen production bugs due to this, where the same code worked in London but failed in New York.
And here's the logic in a reusable function, but flipped to ask "Is it a weekday?" (not a weekend). You could add a parameter for the start day of the weekdays (2 is the default in Oracle; Sunday is day #1).
CREATE OR REPLACE FUNCTION is_weekday (date_in IN DATE)
RETURN BOOLEAN
IS
BEGIN
RETURN TO_CHAR (date_in, 'D') BETWEEN 2 AND 6;
END;
/
DECLARE
l_date DATE := DATE '2018-09-10';
BEGIN
DBMS_OUTPUT.put_line ('If your weekend is Saturday and Sunday....');
FOR indx IN 1 .. 7
LOOP
DBMS_OUTPUT.put_line (
TO_CHAR (l_date, 'FMDay, Month DD YYYY')
|| ' is '
|| CASE WHEN NOT is_weekday (l_date) THEN 'not ' END
|| 'a weekday');
l_date := l_date + 1;
END LOOP;
END;
/
Try it in LiveSQL:
https://livesql.oracle.com/apex/livesql/file/content_G8NQSY6NP48NPJX96RLQ51SUE.html
I have table
CREATE TABLE T_TEST
( KURS_NUMBER NUMBER PRIMARY KEY,
KURS_ID NUMBER NOT NULL,
DATEKURS DATE NOT NULL,
CONSTRAINT UNIQUE2 UNIQUE
(KURS_ID,DATEKURS)
);
TRIGGER for kurs_number
create or replace trigger TR_INSERT_TEST01
before insert on test01
FOR EACH ROW
declare
-- local variables here
begin
IF :NEW.KURS_NUMBER IS NULL
THEN SELECT SEQ_TEST.NEXTVAL INTO :NEW.KURS_NUMBER FROM DUAL;
END IF;
end TR_INSERT_T_TEST;
How can I insert data to kurs_id which will contain only one digit '1'
and datekurs will contain date in order period from 2017year 1 january to 31 december 2017year ( or random date )
connect by level 365
This code works very well but if I want to use my trigger for new column kurs_number it doesn't work due (not enough values). I guess that should be in different way.
insert into t_test
select 1
, date '2017-01-01' + (level-1)
from dual
connect by level <= 365
/
This trick generates 365 rows. We can do arithmetic with dates so adding (level-1) to a root date generates 365 dates.
insert into t_test
select SEQ_TEST.NEXTVAL
, 1
, date '2017-01-01' + (level-1)
from dual
connect by level <= 365
/
"not enough values"
You changed the structure of the target table so you needed to change the projection of the query to match. The revised version includes the additional primary key column.
"i want to use my trigger for new column kurs_number"
You can make this a procedure with parameters for kurs_id and the target year.
As a bonus this code handles leap years correctly.
create or replace procedure generate_kurs_year_recs
( p_kurs_id in number
, p_kurs_year in varchar2 )
is
last_dayno number;
begin
/* find number of days in year */
select to_number(to_char(to_date( p_kurs_year||'-12-31', 'yyyy-mm-dd')
, 'DDD'))
into last_dayno
from dual;
/* generate records for year */
insert into t_test
select SEQ_TEST.NEXTVAL
, p_kurs_id
, to_date( p_kurs_year||'-01-01', 'yyyy-mm-dd') + (level-1)
from dual
connect by level <= last_dayno;
end generate_kurs_year_recs;
/
Call the procedure like this:
begin
generate_kurs_year_recs(1,'2017');
end;
To call from a trigger you will need to pass parameters somehow, presumably using values from the trigger's table.
This will insert all dates in order
insert into t_test (kurs_id, datekurs)
with CTE (DD) as
(
select to_date('20170101','YYYYMMDD') as DD
from dual
union
select DD +1
from CTE
where DD < to_date('20171231','YYYYMMDD')
)
select row_number() over(order by DD) as kurs_id, DD as datekurs
from CTE
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.
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)