SlideShare una empresa de Scribd logo
1 de 4
Descargar para leer sin conexión
Date/Time Arithmetic with Oracle 9/10
   Date and Time information can be stored in Oracle using two different column’s datatype – DATE or
   TIMESTAMP.


Subtraction using DATE:

   DATE datatype stores Month, Date, Year, Century, Hours, Minutes and Seconds. This datatype cannot be
   used to get granular time intervals between two time events.

   DEFINE Time1=TO_DATE('03.12.2004:10:34:24','DD.MM.YYYY:HH24:MI:SS')

   DEFINE Time2=TO_DATE('17.12.2010:10:34:12','DD.MM.YYYY:HH24:MI:SS')

   SELECT TO_CHAR(&Time1,'DD.MM.YYYY:HH24:MI:SS') "Time1",
          TO_CHAR(&Time2,'DD.MM.YYYY:HH24:MI:SS') "Time2",
            &Time2 - &Time1 "Time2-Time1",
            TRUNC(86400*(&Time2-&Time1))-60*(TRUNC((86400*(&Time2-&Time1))/60)) "Sec",
            TRUNC((86400*(&Time2-&Time1))/60)-60*(TRUNC(((86400*(&Time2-&Time1))/60)/60)) "Min",
            TRUNC(((86400*(&Time2-&Time1))/60)/60)-24*(TRUNC((((86400*(&Time2-&Time1))/60)/60)/24))
   "Hrs",
          TRUNC((((86400*(&Time2-&Time1))/60)/60)/24) "Days"
   FROM dual;

   Time1               Time2               Time2-Time1        Sec        Min        Hrs       Days
   ------------------- ------------------- ----------- ---------- ---------- ---------- ----------
   03.12.2004:10:34:24 17.12.2010:10:34:12 2204.99986          48         59         23       2204


Subtraction using TIMESTAMP:

   TIMESTAMP datatype is an expansion of the DATE datatype to include fractional seconds.

   DEFINE Time1=TO_TIMESTAMP('03.12.2004:10:34:24:05','DD.MM.YYYY:HH24:MI:SS:FF')

   DEFINE Time2=TO_TIMESTAMP('17.12.2010:10:34:12:03','DD.MM.YYYY:HH24:MI:SS:FF')

   SELECT &Time2 - &Time1 “Time2–Time1” FROM dual;

   Time2-Time1
   -------------------------------
   +000002204 23:59:47.980000000


Converting DATE to TIMESTAMP:

   To convert the existing DATE datatype to TIMESTAMP datatype, we could use the CAST function. Since
   the DATE does not include fractional seconds, the CAST function would default it to zeros and the display
   is defaulted to the default timestamp format (NLS_TIMESTAMP_FORMAT).

   DEFINE Time1=TO_DATE('03.12.2004:10:34:24','DD.MM.YYYY:HH24:MI:SS')

   DEFINE Time2=TO_DATE('17.12.2010:10:34:12','DD.MM.YYYY:HH24:MI:SS')

   SELECT CAST(&Time2 AS TIMESTAMP) – CAST(&Time1 AS TIMESTAMP “Time2–Time1” FROM dual;

   Time2-Time1
   ----------------------------
   +000002204 23:59:47.000000
NEXT_DAY (date, char)

  NEXT_DAY returns the date of the first weekday named by char that is later than date. The return type is
  always DATE, regardless of the datatype of date. The argument char must be a day of the week in the
  date language of your session, either the full name or the abbreviation. The minimum number of letters
  required is the number of letters in the abbreviated version. Any characters immediately following the
  valid abbreviation are ignored. The return value has the same hours, minutes, and seconds component as
  the argument date.

  SELECT TO_CHAR(NEXT_DAY(SYSDATE,'MON'),'DD.MM.YYYY') "Next Monday" FROM dual;

  Next Monday
  -----------
   08.03.2010


LAST_DAY(date)

  LAST_DAY returns the date of the last day of the month that contains date. The return type is always
  DATE, regardless of the datatype of date.

  SELECT SYSDATE, LAST_DAY(SYSDATE) "Last", LAST_DAY(SYSDATE) - SYSDATE "Days Left" FROM
  dual;

  SYSDATE     Last     Days Left
  --------- --------- ----------
  04-MAR-10 31-MAR-10         27

  Get the last date of a month:

  SELECT SYSDATE, LAST_DAY(TO_DATE('02','MM')) "Last Day" FROM dual;

  SYSDATE              Last Day
  -------------------- ---------
  3/4/2010 11:51:14 PM 28-FEB-10



GREATEST(date1, date2, date3, ….)

  GREATEST returns the latest date within the list of dates provided.

  SELECT GREATEST(SYSDATE, SYSDATE+1, SYSDATE-1) “GREATEST” FROM dual;

  GREATEST
  --------------------
  3/9/2010 11:59:13 AM

  CREATE TABLE example_tb
  (datecol1 DATE
  ,datecol2 DATE
  ,datecol3 DATE)


  INSERT INTO example_tb VALUES (SYSDATE+14, SYSDATE-10, SYSDATE-24);
  INSERT INTO example_tb VALUES (SYSDATE-25, SYSDATE, SYSDATE+11);
  INSERT INTO example_tb VALUES (SYSDATE-6, SYSDATE-7, SYSDATE-9);

  SELECT X.*, GREATEST(X.datecol1, X.datecol2, X.datecol3) "Greatest" FROM example_tb X;
DATECOL1                  DATECOL2                     DATECOL3                Greatest
   ---------------------     ---------------------        ---------------------   ---------------------
   3/22/2010 12:08:09 PM     2/26/2010 12:08:09 PM        2/12/2010 12:08:09 PM   3/22/2010 12:08:09 PM
   2/11/2010 12:08:10 PM     3/8/2010 12:08:10 PM         3/19/2010 12:08:10 PM   3/19/2010 12:08:10 PM
   3/2/2010 12:08:11 PM      3/1/2010 12:08:11 PM         2/27/2010 12:08:11 PM   3/2/2010 12:08:11 PM


LEAST(date1, date2, date3, ….)

   LEAST returns the earliest date within the list of dates provided.

   SELECT SYSDATE, LEAST(SYSDATE, SYSDATE+1, SYSDATE-1) “LEAST” FROM dual;

   SYSDATE              LEAST
   -------------------- -------------------
   3/8/2010 1:59:27 PM 3/7/2010 1:59:27 PM

   SELECT X.*, LEAST(X.datecol1, X.datecol2, X.datecol3) "Least" FROM example_tb X;

   DATECOL1                  DATECOL2                     DATECOL3                Least
   ---------------------     ---------------------        ---------------------   ---------------------
   3/22/2010 12:08:09 PM     2/26/2010 12:08:09 PM        2/12/2010 12:08:09 PM   2/12/2010 12:08:09 PM
   2/11/2010 12:08:10 PM     3/8/2010 12:08:10 PM         3/19/2010 12:08:10 PM   2/11/2010 12:08:10 PM
   3/2/2010 12:08:11 PM      3/1/2010 12:08:11 PM         2/27/2010 12:08:11 PM   2/27/2010 12:08:11 PM


MAX(date)

   MAX is a GROUP function returning the latest date.

   SELECT   MAX(X.datecol1) "Max of Datecol1" FROM example_tb X;

   Max of Datecol1
   ---------------------
   3/22/2010 12:08:09 PM


MIN(date)

   MIN is a GROUP function returning the earliest date.

   SELECT   MIN(X.datecol1) "Min of Datecol1" FROM example_tb X;

   Min of Datecol1
   ---------------------
   2/11/2010 12:08:10 PM


ADD_MONTHS(date, integer)

   ADD_MONTHS adds the number of integer months to the date.

   SELECT SYSDATE, ADD_MONTHS(SYSDATE,2) "X", ADD_MONTHS(SYSDATE,-2) "Y" FROM dual;

   SYSDATE             X                   Y
   ------------------- ------------------- -------------------
   3/8/2010 2:00:43 PM 5/8/2010 2:00:43 PM 1/8/2010 2:00:43 PM
MONTHS_BETWEEN(date, date)

  MONTHS_BETWEEN returns the months between the two dates.

  SELECT   MONTHS_BETWEEN(SYSDATE+365, SYSDATE-365) "Months Between" FROM dual;

  Months Between
  --------------
  24


ROUND(date, <format>)

  ROUND returns the date rounded to the unit specified by the format. If format is not provided, the date is
  rounded to the nearest date.

  SELECT SYSDATE, ROUND(SYSDATE) "No Unit Round", ROUND(SYSDATE,'MM') "Unit Round" FROM
  dual; -- (MM/Month - rounds up on the 16th day)


  SYSDATE             No Unit Round Unit Round
  ------------------- -------------- ----------
  3/8/2010 1:54:26 PM 3/9/2010       3/1/2010


TRUNC(date, <format>)

  TRUNC returns the date truncated to the unit specified by the format.

  SELECT SYSDATE, TRUNC(SYSDATE) "No Unit Trunc", TRUNC(SYSDATE,'HH24') "Unit Trunc" FROM
  dual;

  SYSDATE             No Unit Trunc Unit Trunc
  ------------------- -------------- -------------------
  3/8/2010 1:56:04 PM 3/8/2010       3/8/2010 1:00:00 PM

Más contenido relacionado

Similar a Oracle Date Functions

Date data type and Globalization in Oracle
Date data type and Globalization in OracleDate data type and Globalization in Oracle
Date data type and Globalization in OracleMasoud Haji Hassan Pour
 
MySQL Built-In Functions
MySQL Built-In FunctionsMySQL Built-In Functions
MySQL Built-In FunctionsSHC
 
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)Faysal Shaarani (MBA)
 
State of the Trino Project
State of the Trino ProjectState of the Trino Project
State of the Trino ProjectMartin Traverso
 
Metadata Matters
Metadata MattersMetadata Matters
Metadata Mattersafa reg
 
Oracle 12c SQL: Date Ranges
Oracle 12c SQL: Date RangesOracle 12c SQL: Date Ranges
Oracle 12c SQL: Date RangesStew Ashton
 
Connor McDonald Partitioning
Connor McDonald PartitioningConnor McDonald Partitioning
Connor McDonald PartitioningInSync Conference
 
Chasing the optimizer
Chasing the optimizerChasing the optimizer
Chasing the optimizerMauro Pagano
 
Common Performance Pitfalls in Odoo apps
Common Performance Pitfalls in Odoo appsCommon Performance Pitfalls in Odoo apps
Common Performance Pitfalls in Odoo appsOdoo
 
JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8Serhii Kartashov
 
Sql server ___________session 2(sql 2008)
Sql server  ___________session 2(sql 2008)Sql server  ___________session 2(sql 2008)
Sql server ___________session 2(sql 2008)Ehtisham Ali
 
Date rangestech15
Date rangestech15Date rangestech15
Date rangestech15stewashton
 
ACL London User Group - Question Box Session
ACL London User Group - Question Box SessionACL London User Group - Question Box Session
ACL London User Group - Question Box SessionAlex Psarras
 

Similar a Oracle Date Functions (20)

Date data type and Globalization in Oracle
Date data type and Globalization in OracleDate data type and Globalization in Oracle
Date data type and Globalization in Oracle
 
MySQL Built-In Functions
MySQL Built-In FunctionsMySQL Built-In Functions
MySQL Built-In Functions
 
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
 
State of the Trino Project
State of the Trino ProjectState of the Trino Project
State of the Trino Project
 
Metadata Matters
Metadata MattersMetadata Matters
Metadata Matters
 
Les05
Les05Les05
Les05
 
Oracle 12c SQL: Date Ranges
Oracle 12c SQL: Date RangesOracle 12c SQL: Date Ranges
Oracle 12c SQL: Date Ranges
 
Connor McDonald Partitioning
Connor McDonald PartitioningConnor McDonald Partitioning
Connor McDonald Partitioning
 
SAV
SAVSAV
SAV
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
 
Sql queries
Sql queriesSql queries
Sql queries
 
Chasing the optimizer
Chasing the optimizerChasing the optimizer
Chasing the optimizer
 
Common Performance Pitfalls in Odoo apps
Common Performance Pitfalls in Odoo appsCommon Performance Pitfalls in Odoo apps
Common Performance Pitfalls in Odoo apps
 
Sqlserver 2008 r2
Sqlserver 2008 r2Sqlserver 2008 r2
Sqlserver 2008 r2
 
JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8
 
Sql server ___________session 2(sql 2008)
Sql server  ___________session 2(sql 2008)Sql server  ___________session 2(sql 2008)
Sql server ___________session 2(sql 2008)
 
Date rangestech15
Date rangestech15Date rangestech15
Date rangestech15
 
5 Cool Things About SQL
5 Cool Things About SQL5 Cool Things About SQL
5 Cool Things About SQL
 
Flashback ITOUG
Flashback ITOUGFlashback ITOUG
Flashback ITOUG
 
ACL London User Group - Question Box Session
ACL London User Group - Question Box SessionACL London User Group - Question Box Session
ACL London User Group - Question Box Session
 

Último

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 

Último (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

Oracle Date Functions

  • 1. Date/Time Arithmetic with Oracle 9/10 Date and Time information can be stored in Oracle using two different column’s datatype – DATE or TIMESTAMP. Subtraction using DATE: DATE datatype stores Month, Date, Year, Century, Hours, Minutes and Seconds. This datatype cannot be used to get granular time intervals between two time events. DEFINE Time1=TO_DATE('03.12.2004:10:34:24','DD.MM.YYYY:HH24:MI:SS') DEFINE Time2=TO_DATE('17.12.2010:10:34:12','DD.MM.YYYY:HH24:MI:SS') SELECT TO_CHAR(&Time1,'DD.MM.YYYY:HH24:MI:SS') "Time1", TO_CHAR(&Time2,'DD.MM.YYYY:HH24:MI:SS') "Time2", &Time2 - &Time1 "Time2-Time1", TRUNC(86400*(&Time2-&Time1))-60*(TRUNC((86400*(&Time2-&Time1))/60)) "Sec", TRUNC((86400*(&Time2-&Time1))/60)-60*(TRUNC(((86400*(&Time2-&Time1))/60)/60)) "Min", TRUNC(((86400*(&Time2-&Time1))/60)/60)-24*(TRUNC((((86400*(&Time2-&Time1))/60)/60)/24)) "Hrs", TRUNC((((86400*(&Time2-&Time1))/60)/60)/24) "Days" FROM dual; Time1 Time2 Time2-Time1 Sec Min Hrs Days ------------------- ------------------- ----------- ---------- ---------- ---------- ---------- 03.12.2004:10:34:24 17.12.2010:10:34:12 2204.99986 48 59 23 2204 Subtraction using TIMESTAMP: TIMESTAMP datatype is an expansion of the DATE datatype to include fractional seconds. DEFINE Time1=TO_TIMESTAMP('03.12.2004:10:34:24:05','DD.MM.YYYY:HH24:MI:SS:FF') DEFINE Time2=TO_TIMESTAMP('17.12.2010:10:34:12:03','DD.MM.YYYY:HH24:MI:SS:FF') SELECT &Time2 - &Time1 “Time2–Time1” FROM dual; Time2-Time1 ------------------------------- +000002204 23:59:47.980000000 Converting DATE to TIMESTAMP: To convert the existing DATE datatype to TIMESTAMP datatype, we could use the CAST function. Since the DATE does not include fractional seconds, the CAST function would default it to zeros and the display is defaulted to the default timestamp format (NLS_TIMESTAMP_FORMAT). DEFINE Time1=TO_DATE('03.12.2004:10:34:24','DD.MM.YYYY:HH24:MI:SS') DEFINE Time2=TO_DATE('17.12.2010:10:34:12','DD.MM.YYYY:HH24:MI:SS') SELECT CAST(&Time2 AS TIMESTAMP) – CAST(&Time1 AS TIMESTAMP “Time2–Time1” FROM dual; Time2-Time1 ---------------------------- +000002204 23:59:47.000000
  • 2. NEXT_DAY (date, char) NEXT_DAY returns the date of the first weekday named by char that is later than date. The return type is always DATE, regardless of the datatype of date. The argument char must be a day of the week in the date language of your session, either the full name or the abbreviation. The minimum number of letters required is the number of letters in the abbreviated version. Any characters immediately following the valid abbreviation are ignored. The return value has the same hours, minutes, and seconds component as the argument date. SELECT TO_CHAR(NEXT_DAY(SYSDATE,'MON'),'DD.MM.YYYY') "Next Monday" FROM dual; Next Monday ----------- 08.03.2010 LAST_DAY(date) LAST_DAY returns the date of the last day of the month that contains date. The return type is always DATE, regardless of the datatype of date. SELECT SYSDATE, LAST_DAY(SYSDATE) "Last", LAST_DAY(SYSDATE) - SYSDATE "Days Left" FROM dual; SYSDATE Last Days Left --------- --------- ---------- 04-MAR-10 31-MAR-10 27 Get the last date of a month: SELECT SYSDATE, LAST_DAY(TO_DATE('02','MM')) "Last Day" FROM dual; SYSDATE Last Day -------------------- --------- 3/4/2010 11:51:14 PM 28-FEB-10 GREATEST(date1, date2, date3, ….) GREATEST returns the latest date within the list of dates provided. SELECT GREATEST(SYSDATE, SYSDATE+1, SYSDATE-1) “GREATEST” FROM dual; GREATEST -------------------- 3/9/2010 11:59:13 AM CREATE TABLE example_tb (datecol1 DATE ,datecol2 DATE ,datecol3 DATE) INSERT INTO example_tb VALUES (SYSDATE+14, SYSDATE-10, SYSDATE-24); INSERT INTO example_tb VALUES (SYSDATE-25, SYSDATE, SYSDATE+11); INSERT INTO example_tb VALUES (SYSDATE-6, SYSDATE-7, SYSDATE-9); SELECT X.*, GREATEST(X.datecol1, X.datecol2, X.datecol3) "Greatest" FROM example_tb X;
  • 3. DATECOL1 DATECOL2 DATECOL3 Greatest --------------------- --------------------- --------------------- --------------------- 3/22/2010 12:08:09 PM 2/26/2010 12:08:09 PM 2/12/2010 12:08:09 PM 3/22/2010 12:08:09 PM 2/11/2010 12:08:10 PM 3/8/2010 12:08:10 PM 3/19/2010 12:08:10 PM 3/19/2010 12:08:10 PM 3/2/2010 12:08:11 PM 3/1/2010 12:08:11 PM 2/27/2010 12:08:11 PM 3/2/2010 12:08:11 PM LEAST(date1, date2, date3, ….) LEAST returns the earliest date within the list of dates provided. SELECT SYSDATE, LEAST(SYSDATE, SYSDATE+1, SYSDATE-1) “LEAST” FROM dual; SYSDATE LEAST -------------------- ------------------- 3/8/2010 1:59:27 PM 3/7/2010 1:59:27 PM SELECT X.*, LEAST(X.datecol1, X.datecol2, X.datecol3) "Least" FROM example_tb X; DATECOL1 DATECOL2 DATECOL3 Least --------------------- --------------------- --------------------- --------------------- 3/22/2010 12:08:09 PM 2/26/2010 12:08:09 PM 2/12/2010 12:08:09 PM 2/12/2010 12:08:09 PM 2/11/2010 12:08:10 PM 3/8/2010 12:08:10 PM 3/19/2010 12:08:10 PM 2/11/2010 12:08:10 PM 3/2/2010 12:08:11 PM 3/1/2010 12:08:11 PM 2/27/2010 12:08:11 PM 2/27/2010 12:08:11 PM MAX(date) MAX is a GROUP function returning the latest date. SELECT MAX(X.datecol1) "Max of Datecol1" FROM example_tb X; Max of Datecol1 --------------------- 3/22/2010 12:08:09 PM MIN(date) MIN is a GROUP function returning the earliest date. SELECT MIN(X.datecol1) "Min of Datecol1" FROM example_tb X; Min of Datecol1 --------------------- 2/11/2010 12:08:10 PM ADD_MONTHS(date, integer) ADD_MONTHS adds the number of integer months to the date. SELECT SYSDATE, ADD_MONTHS(SYSDATE,2) "X", ADD_MONTHS(SYSDATE,-2) "Y" FROM dual; SYSDATE X Y ------------------- ------------------- ------------------- 3/8/2010 2:00:43 PM 5/8/2010 2:00:43 PM 1/8/2010 2:00:43 PM
  • 4. MONTHS_BETWEEN(date, date) MONTHS_BETWEEN returns the months between the two dates. SELECT MONTHS_BETWEEN(SYSDATE+365, SYSDATE-365) "Months Between" FROM dual; Months Between -------------- 24 ROUND(date, <format>) ROUND returns the date rounded to the unit specified by the format. If format is not provided, the date is rounded to the nearest date. SELECT SYSDATE, ROUND(SYSDATE) "No Unit Round", ROUND(SYSDATE,'MM') "Unit Round" FROM dual; -- (MM/Month - rounds up on the 16th day) SYSDATE No Unit Round Unit Round ------------------- -------------- ---------- 3/8/2010 1:54:26 PM 3/9/2010 3/1/2010 TRUNC(date, <format>) TRUNC returns the date truncated to the unit specified by the format. SELECT SYSDATE, TRUNC(SYSDATE) "No Unit Trunc", TRUNC(SYSDATE,'HH24') "Unit Trunc" FROM dual; SYSDATE No Unit Trunc Unit Trunc ------------------- -------------- ------------------- 3/8/2010 1:56:04 PM 3/8/2010 3/8/2010 1:00:00 PM