How To Increment Dates by 1?
Page 1 of 1
How To Increment Dates by 1?
If you have a date, and you want to increment it by 1. You can do this by adding the date with a
date interval. You can also do this by adding the number 1 directly on the date. The tutorial
example below shows you how to adding numbers to dates, and take date differences:
SELECT TO_DATE('30-APR-06') + 1 FROM DUAL;
-- Adding 1 day to a date
01-MAY-06
SELECT TO_DATE('01-MAY-06') - TO_DATE('30-APR-06') FROM DUAL;
-- Taking date differences
1
SELECT SYSTIMESTAMP + 1 FROM DUAL;
-- The number you add is always in days.
08-MAY-06
SELECT TO_CHAR(SYSTIMESTAMP+1,'DD-MON-YYYY HH24:MI:SS.FF3') FROM DUAL;
-- Error: Adding 1 to a timestamp makes it a date.
How To Calculate Date and Time Differences?
If you want to know how many years, months, days and seconds are there between two dates or
times, you can use the date and time interval expressions: YEAR ... TO MONTH and DAY ... TO
SECOND. The tutorial exercise below gives you some good examples:
SELECT
(TO_DATE('01-MAY-2006 16:52:57','DD-MON-YYYY HH24:MI:SS') -
TO_DATE('31-JAN-1897 09:26:50','DD-MON-YYYY HH24:MI:SS'))
YEAR(4) TO MONTH
FROM DUAL;
-- 109 years and 3 months
109-3
SELECT
(TO_DATE('01-MAY-2006 16:52:57','DD-MON-YYYY HH24:MI:SS') -
TO_DATE('31-JAN-1897 09:26:50','DD-MON-YYYY HH24:MI:SS'))
DAY(9) TO SECOND
FROM DUAL;
-- 39901 days and some seconds
39901 7:26:7.0
SELECT
(TO_TIMESTAMP('01-MAY-2006 16:52:57.847','DD-MON-YYYY HH24:MI:SS.FF3') -
TO_TIMESTAMP('31-JAN-1897 09:26:50.124','DD-MON-YYYY HH24:MI:SS.FF3'))
YEAR(4) TO MONTH
FROM DUAL;
-- 109 years and 3 months
109-3
SELECT
(TO_TIMESTAMP('01-MAY-2006 16:52:57.847','DD-MON-YYYY HH24:MI:SS.FF3') -
TO_TIMESTAMP('31-JAN-1897 09:26:50.124','DD-MON-YYYY HH24:MI:SS.FF3'))
DAY(9) TO SECOND
FROM DUAL;
-- 39901 days and some fractional seconds
39901 7:26:7.723000000
date interval. You can also do this by adding the number 1 directly on the date. The tutorial
example below shows you how to adding numbers to dates, and take date differences:
SELECT TO_DATE('30-APR-06') + 1 FROM DUAL;
-- Adding 1 day to a date
01-MAY-06
SELECT TO_DATE('01-MAY-06') - TO_DATE('30-APR-06') FROM DUAL;
-- Taking date differences
1
SELECT SYSTIMESTAMP + 1 FROM DUAL;
-- The number you add is always in days.
08-MAY-06
SELECT TO_CHAR(SYSTIMESTAMP+1,'DD-MON-YYYY HH24:MI:SS.FF3') FROM DUAL;
-- Error: Adding 1 to a timestamp makes it a date.
How To Calculate Date and Time Differences?
If you want to know how many years, months, days and seconds are there between two dates or
times, you can use the date and time interval expressions: YEAR ... TO MONTH and DAY ... TO
SECOND. The tutorial exercise below gives you some good examples:
SELECT
(TO_DATE('01-MAY-2006 16:52:57','DD-MON-YYYY HH24:MI:SS') -
TO_DATE('31-JAN-1897 09:26:50','DD-MON-YYYY HH24:MI:SS'))
YEAR(4) TO MONTH
FROM DUAL;
-- 109 years and 3 months
109-3
SELECT
(TO_DATE('01-MAY-2006 16:52:57','DD-MON-YYYY HH24:MI:SS') -
TO_DATE('31-JAN-1897 09:26:50','DD-MON-YYYY HH24:MI:SS'))
DAY(9) TO SECOND
FROM DUAL;
-- 39901 days and some seconds
39901 7:26:7.0
SELECT
(TO_TIMESTAMP('01-MAY-2006 16:52:57.847','DD-MON-YYYY HH24:MI:SS.FF3') -
TO_TIMESTAMP('31-JAN-1897 09:26:50.124','DD-MON-YYYY HH24:MI:SS.FF3'))
YEAR(4) TO MONTH
FROM DUAL;
-- 109 years and 3 months
109-3
SELECT
(TO_TIMESTAMP('01-MAY-2006 16:52:57.847','DD-MON-YYYY HH24:MI:SS.FF3') -
TO_TIMESTAMP('31-JAN-1897 09:26:50.124','DD-MON-YYYY HH24:MI:SS.FF3'))
DAY(9) TO SECOND
FROM DUAL;
-- 39901 days and some fractional seconds
39901 7:26:7.723000000
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Sun Jan 31, 2010 12:35 am by Micheal
» Accessing tables of other users in SQL
Sun Jan 31, 2010 12:34 am by Micheal
» Grouping commands in SQL
Sun Jan 31, 2010 12:32 am by Micheal
» Operations on Result Sets in SQL
Sun Jan 31, 2010 12:31 am by Micheal
» Subqueries in SQL
Sun Jan 31, 2010 12:30 am by Micheal
» Joining Relations in SQL
Sun Jan 31, 2010 12:28 am by Micheal
» Delete comand in SQL
Sun Jan 31, 2010 12:27 am by Micheal
» Update command in SQL
Sun Jan 31, 2010 12:26 am by Micheal
» Insert command in SQL
Sun Jan 31, 2010 12:23 am by Micheal