Difference between two dates - sql

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)

Related

Insert records for 40years in table only date

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 get hour and min from timestamp variable in Oracle SQL

I got a timestamp variable like this:
INSERT INTO Table VALUES(TO_TIMESTAMP('2021-02-17 17:00','YYYY-MM-DD HH24:MI'));
I've created a trigger and my aim is to get the hour and min from that attribute so that I can compare them in two cursors like time.cur1 = time.cur2 (supposing that time is hour:min or hour+min), is there any cast like for the date,
CAST((timestamp) AS DATE)
should I create a new type after extracting the hour and min with
SELECT EXTRACT(HOUR FROM timestamp) FROM Table;
SELECT EXTRACT(MIN FROM timestamp) FROM Table;
or there is another way? (I'm using Oracle Database 11g). Thanks.
New type? No, put those values into a NUMBER datatype variables.
SQL> declare
2 l_hour number;
3 l_min number;
4 begin
5 select extract (hour from systimestamp),
6 extract (minute from systimestamp)
7 into l_hour,
8 l_min
9 from dual;
10
11 dbms_output.put_line('Hour: ' || l_hour ||'; minute: ' || l_min);
12 end;
13 /
Hour: 11; minute: 47
PL/SQL procedure successfully completed.
SQL>
SELECT TO_CHAR(SYSDATE, 'HH24') AS HOUR, TO_CHAR(SYSDATE, 'MI') AS MINUTS FROM DUAL;
TO YOUR EXAMPLE:
INSERT INTO Table VALUES(HOUR, MINUTS)
SELECT TO_CHAR(SYSDATE, 'HH24') AS HOUR, TO_CHAR(SYSDATE, 'MI') AS MINUTS;
FROM <EVERY_TABLE_OR_DUAL>;

null values not processed by to_timestamp in oracle sql

I'm trying to load data from a csv with sql-loader. There's one column with date in this format:
2011-12-31 00:00:00.000
I tried it using to_date() but it couldn't handle fractions of second. Therefore I used this:
cast(TO_TIMESTAMP(:DATUM_ONTVANGST, 'YYYY-MM-DD HH24:MI:SS.FF3')as date)
Now I get the error:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
for null values in the column
Can to_timestamp not handle null values or am I doing something wrong?
This works:
select
cast(TO_TIMESTAMP('2011-12-31 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FF3')as date)
from dual
and this also works
select
cast(TO_TIMESTAMP(null, 'YYYY-MM-DD HH24:MI:SS.FF3')as date)
from dual
So there must be some value that is not right format
I think there is space(<field>, ,<field>) in your data which is causing the issue.
Better to use TRIM before using them.
Following are some examples to demonstrate with different values:
-- working fine with correct timestamp values
SQL> SELECT
2 CAST(TO_TIMESTAMP('2019-11-05 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FF3') AS DATE) as dt
3 FROM
4 DUAL;
DT
---------
05-NOV-19
-- working fine with a null value
SQL>
SQL> SELECT
2 CAST(TO_TIMESTAMP(NULL, 'YYYY-MM-DD HH24:MI:SS.FF3') AS DATE) as dt
3 FROM
4 DUAL;
DT
---------
-- working fine with an empty value
SQL>
SQL> SELECT
2 CAST(TO_TIMESTAMP('', 'YYYY-MM-DD HH24:MI:SS.FF3') AS DATE) as dt
3 FROM
4 DUAL;
DT
---------
Now showing the error and solution
-- giving an error with space in the value -- Your case
-- might be you are considering it as the null
-- but it is actually a value that is a space character
SQL>
SQL> SELECT
2 CAST(TO_TIMESTAMP(' ', 'YYYY-MM-DD HH24:MI:SS.FF3') AS DATE) as dt
3 FROM
4 DUAL;
CAST(TO_TIMESTAMP(' ', 'YYYY-MM-DD HH24:MI:SS.FF3') AS DATE) as dt
*
ERROR at line 2:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
-- Solution to your issue -- using TRIM
SQL>
SQL> SELECT
2 CAST(TO_TIMESTAMP(TRIM(' '), 'YYYY-MM-DD HH24:MI:SS.FF3') AS DATE) as dt
3 FROM
4 DUAL;
DT
---------
SQL>
Cheers!!
Building off Ersin's answer, this is the format that worked for me
select
cast(NULL AS TIMESTAMP WITH LOCAL TIME ZONE) as date
from dual

Insert a range of dates and should be unique with another column

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

Extract date (yyyy/mm/dd) from a timestamp in PostgreSQL

I want to extract just the date part from a timestamp in PostgreSQL.
I need it to be a postgresql DATE type so I can insert it into another table that expects a DATE value.
For example, if I have 2011/05/26 09:00:00, I want 2011/05/26
I tried casting, but I only get 2011:
timestamp:date
cast(timestamp as date)
I tried to_char() with to_date():
SELECT to_date(to_char(timestamp, 'YYYY/MM/DD'), 'YYYY/MM/DD')
FROM val3 WHERE id=1;
I tried to make it a function:
CREATE OR REPLACE FUNCTION testing() RETURNS void AS '
DECLARE i_date DATE;
BEGIN
SELECT to_date(to_char(val1, "YYYY/MM/DD"),"YYYY/MM/DD")
INTO i_date FROM exampTable WHERE id=1;
INSERT INTO foo(testd) VALUES (i);
END
What is the best way to extract date (yyyy/mm/dd) from a timestamp in PostgreSQL?
You can cast your timestamp to a date by suffixing it with ::date. Here, in psql, is a timestamp:
# select '2010-01-01 12:00:00'::timestamp;
timestamp
---------------------
2010-01-01 12:00:00
Now we'll cast it to a date:
wconrad=# select '2010-01-01 12:00:00'::timestamp::date;
date
------------
2010-01-01
On the other hand you can use date_trunc function. The difference between them is that the latter returns the same data type like timestamptz keeping your time zone intact (if you need it).
=> select date_trunc('day', now());
date_trunc
------------------------
2015-12-15 00:00:00+02
(1 row)
Use the date function:
select date(timestamp_field) from table
From a character field representation to a date you can use:
select date(substring('2011/05/26 09:00:00' from 1 for 10));
Test code:
create table test_table (timestamp_field timestamp);
insert into test_table (timestamp_field) values(current_timestamp);
select timestamp_field, date(timestamp_field) from test_table;
Test result:
Have you tried to cast it to a date, with <mydatetime>::date ?
In postgres simply :
TO_CHAR(timestamp_column, 'DD/MM/YYYY') as submission_date
This works for me in python 2.7
select some_date::DATE from some_table;
Just do select date(timestamp_column) and you would get the only the date part.
Sometimes doing select timestamp_column::date may return date 00:00:00 where it doesn't remove the 00:00:00 part. But I have seen date(timestamp_column) to work perfectly in all the cases. Hope this helps.
CREATE TABLE sometable (t TIMESTAMP, d DATE);
INSERT INTO sometable SELECT '2011/05/26 09:00:00';
UPDATE sometable SET d = t; -- OK
-- UPDATE sometable SET d = t::date; OK
-- UPDATE sometable SET d = CAST (t AS date); OK
-- UPDATE sometable SET d = date(t); OK
SELECT * FROM sometable ;
t | d
---------------------+------------
2011-05-26 09:00:00 | 2011-05-26
(1 row)
Another test kit:
SELECT pg_catalog.date(t) FROM sometable;
date
------------
2011-05-26
(1 row)
SHOW datestyle ;
DateStyle
-----------
ISO, MDY
(1 row)
You can use date_trunc('day', field).
select date_trunc('day', data_gps) as date_description from some_table;