• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Sql » Queries » SQL DATE Function
Next →
← Prev

SQL DATE Function

By Dinesh Thakur

The most difficult part when working with dates is to be sure that the format of the date you are trying to insert, matches the format of the date column in the database. As long as your data contains only the date portion, your queries will work as expected. However, if a time portion is involved, it gets complicated.

Date functions operate on values of the DATE datatype. All date functions return a value of DATE datatype, except the MONTHS_BETWEEN function, which returns a number.

ADD_MONTHS (dt, i): Returns the date dt plus i months. If i is a decimal number. Oracle will automatically convert it to an integer by truncating the decimal portion (it may also be negative).

CURRENT _DATE: Returns the current date in the Gregorian calendar for the session’s time zone. It requires no arguments.

CURRENT_TIMESTAMP ([p]) Returns the current date and time in the session’s time zone to p digits of the precision, p should be an integer 0 through 9 and defaults to 6. The return datatype is TIMESTAMP WITH TIME ZONE.

LAST_DAY(dt) Returns the last day of the month for the date dt.

MONTHS_BETWEEN (dt1, dt2): Returns the number of months that d11 is later than dt2. A whole number is returned if dt1 and dt2 are the same day of the month or if both are the last day of the month.

NEXT_DAY(dt, s): Returns the date that corresponds to the next day of week specified by the string s following the date dt. The time portion of the date is the same as the time portion of dt.

ROUND(dt[, fint]) : Returns the date dt rounded to the granularity specified in the date-format string fmt.

 SYSDATE Returns the current date/time, takes no arguments.

TRUNC(dt[, fmt]) Returns the date dt truncated to the granularity specified by the format string fmt.

1. SYSDATE: the SYSDATE function returns the current system date and time on your local database. The default format that the Oracle database uses is: DD-Mon-YY This is how SQL*Plus will show you the data, when requested.

Syntax:
 
SQL> SELECT SYSDATE FROM dual;
SQL> SELECT TO_CHAR(SYSDATE, 'MM-DD-YYYY HH24: MI: SS')"NOW" FROM DUAL; 

2. CURRENT_DATE: This Function Returns the current date in the Gregorian calendar for the session’s time zone. It requires no arguments.

SQL> SELECT CURRENT_DATE FROM DUAL; 

3. CURRENT_TIMESTAMP ([p]): Returns the current date and time in the session’s time zone to p digits of the precision, p should be an integer 0 through 9 and defaults to 6. The return datatype is TIMESTAMP WITH TIME ZONE.

SQL> SELECT CURRENT_TIMESTAMP FROM DUAL; 

4. ADD_MONTHS: This Function Calculates difference between two dates.

Syntax:

ADD_MONTHS (d,n) 

This function Returns the date d plus n months. The argument n can be any integer. If d is the last day of the month or if the resulting month has fewer days than the day component of d, then the result is the last day of the resulting month. Otherwise, the result has the same day component as d.

SQL > SELECT SYSDATE,ADD_MONTHS(SYSDATE,6), ADD_MONTHS(SYSDATE, -6) FROM DUAL; 
SQL> SELECT TO_CHAR(ADD_MONTHS(hiredate,1),'DD-MON-YYYY') "Next Month" From emp Where ename = 'SMITH'; 

5. LAST_DAY: This Function Returns the date of the last day of the month. You might use this function to determine how many days are left in the current month.

SQL> SELECT SYSDATE, LAST_DAY(SYSDATE) "Last", LAST_DAY(SYSDATE)–SYSDATE "Days Left" FROM DUAL; 

6. MONTHS_BETWEEN: This function returns number of months between two dates d1 and d2. If d1 later than d2, result is positive; if earlier, negative. If d1 and d2 are either the same days of the month or both last days of months, the result is always an integer; otherwise oracle calculates the fractional portion of the result based on a 31-day month and considers the difference in components of d1 and d2.

SQL> SELECT MONTHS_BETWEEN (TO_DATE(’02-02-1995’,’MM-DD-YYYY’), TO_DATE(’01-01-1995’,’MM-DD-YYYY’)) "Months" FROM DUAL; 

7. NEXT_DAY: This Function returns the date of the first weekday named by char that is later than the d. The argument char must be a day of the week in your session’s date language-either the full name or the abbreviation.

SQL> SELECT NEXT_DAY('1-JUNE-09','TUESDAY') "NEXT DAY" FROM DUAL; 

8. ROUND: The SQL ROUND() function rounds a number to a specific length or precision.

Syntax ROUND (d [, fmt]) 

• It Returns the date dt rounded to the granularity specified in the date-format string fmt.

• If the format you specify is ‘MON’ and if date is Less than or to <= 15 then it will the day’s date a given month.

• If the day you specify is greater than 15 it display date of first day of next month.

Example1:
 
SQL> SELECT SYSDATE, HIREDATE, ROUND (HIREDATE,'MON') FROM EMP WHERE SAL > 3000;
 
 Example2:

SQL> SELECT ROUND (TO_DATE ('2-OCT-92'), 'MON') "NEW YEAR" FROM DUAL; 

 

You’ll also like:

  1. MySql Date Function in Java Servlet
  2. SQL ISNULL() Function
  3. SQL NVL Function
  4. SQL GETDATE() Function
  5. SQL CAST() Function
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.