Table of contents
1.
Date functions in SQL Introduction
1.1.
SQL Date Data Types
1.2.
MYSQL
1.2.1.
Default function to find a date in MYSQL:
2.
Date Format 
3.
SQL Working with Dates
3.1.
Query 1: If we want to select the product with order date as 2022-11-11.
3.2.
Query 2: If we want to select the product with the order date as 2022-11-11 and want to see only the date, not the time.
3.3.
Query 3: If we want to add a specified time interval to an existing date.
3.4.
Query 4: If we want to select the product with the order date as 2022-11-11 and want to see the date number or month number instead of the entire date.
4.
 
5.
 
6.
 
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Date functions in SQL

Author Apoorv
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Date functions in SQL Introduction

For newcomers to SQL, dates are difficult since, while working with databases, the format of the date in the table must match the input date in order to insert. Datetime (time is also linked with date) is used in many contexts instead of date.

When working with dates, the most challenging element is ensuring that the format of the date you're trying to insert matches the format of the database's date column. we will be exploring a lot of Date functions in SQL further in the blog.

SQL Date Data Types

Before exploring Date functions in SQL first we need to understand the SQL date data types.

For storing a date or a date/time value in MySQL, the following data types are available:

  • DATE - Date format in MYSQL is - YYYY-MM-DD
  • DATETIME - Datetime format in MYSQL is -  YYYY-MM-DD HH:MI:SS
  • TIMESTAMP - Timestamp format in MYSQL is -  YYYY-MM-DD HH:MI:SS
  • YEAR - Year format in MYSQL is -  YYYY or YY

MYSQL

Default function to find a date in MYSQL:

NOW(): It returns the current date and time when you run this query
Code:
SELECT NOW();
Output
2022-03-20 13:42:24
CURDATE(): It returns the current date and time when you run this query
Code:
SELECT CURDATE();
Output
2022-03-20 13:42:24
CURTIME(): It returns the current time 
Code:
SELECT CURTIME();
Output
05:36:05

Date Format 

DATE_FORMAT(): This function Displays date/time data in different formats. This is one of the most used Date functions in SQL.

%a-This is Abbreviated as weekday name (Sun-Sat)

%b-This is Abbreviated as month name (Jan-Dec)

%c-This is Abbreviated as Month, numeric (0-12)

%D-This is Abbreviated as Day of month with English suffix (0th, 1st, 2nd, 3rd)

%d-This is Abbreviated as Day of month, numeric (00-31)

%e-This is Abbreviated as  Day of month, numeric (0-31)

%f-This is Abbreviated as Microseconds (000000-999999)

%H-This is Abbreviated as Hour (00-23)

%h-This is Abbreviated as Hour (01-12)

%I-This is Abbreviated as Hour (01-12)

%i-This is Abbreviated as Minutes, numeric (00-59)

%j-This is Abbreviated as Day of year (001-366)

%k-This is Abbreviated as Hour (0-23)

%l-This is Abbreviated as Hour (1-12)

%M-This is Abbreviated as Month name (January-December)

%m-This is Abbreviated as Month, numeric (00-12)

%p-This is Abbreviated as AM or PM

%r-This is Abbreviated as Time, 12-hour (hh:mm:ss followed by AM or PM)

%S-This is Abbreviated as Seconds (00-59)

%s-This is Abbreviated as Seconds (00-59)

%T-Time, 24-hour (hh:mm:ss)

%U-Week (00-53) where Sunday is the first day of week

%u-Week (00-53) where Monday is the first day of week

%V-Week (01-53) where Sunday is the first day of week, used with %X

%v-Week (01-53) where Monday is the first day of week, used with %x

%W-This is Abbreviated as Weekday name (Sunday-Saturday)

%w-This is Abbreviated as Day of the week (0=Sunday, 6=Saturday)

%X-Year for the week where Sunday is the first day of week, four digits, used with %V

%x-Year for the week where Monday is the first day of week, four digits, used with %v

%Y-This is Abbreviated as  Year, numeric, four digits

%y-This is Abbreviated as  Year, numeric, two digits

SQL Working with Dates

Query 1: If we want to select the product with order date as 2022-11-11.

Table Name:  Orders

OrderId ProductName ProductId OrderDate

1

Tshirt

7748

2022-11-11

2

Trouser

4783

2022-11-09

3

Belt

2022

2022-11-11

4

Jeans

1946

2022-10-29

Code:

SELECT * FROM Orders WHERE OrderDate='2022-11-11'

Output

OrderId ProductName ProductId OrderDate

1

Tshirt

7748

2022-11-11

3

Belt

2022

2022-11-11

 

Query 2: If we want to select the product with the order date as 2022-11-11 and want to see only the date, not the time.

Table Name:  Orders

OrderId ProductName ProductId OrderDate

1

Tshirt

7748

2022-11-11 16:44:15.581

Code: 

SELECT ProductName, ProductId, DATE(OrderDate) AS OrderDate FROM Orders;

Explain 

DATE(): Extracts the date part of a date or date/time expression.

Output

ProductName ProductId OrderDate

Tshirt

7748

2022-11-11 

Query 3: If we want to add a specified time interval to an existing date.

Table Name:  Orders
 

OrderId ProductName ProductId OrderDate

1

Tshirt

7748

2020-11-11 16:44:15.581

Code: 

SELECT ProductName, ProductId, DATE_ADD(OrderDate, INTERVAL 2 YEAR) AS OrderDate FROM Orders;

Explain 

DATE_ADD(): Adds a specified time interval to date since in this the year table row 1 has  2020 year we are adding 2 more years now the output will print 2022

Output

ProductName ProductId OrderDate

Tshirt

7748

2022-11-11 16:44:15.581

Query 4: If we want to select the product with the order date as 2022-11-11 and want to see the date number or month number instead of the entire date.

Table Name:  Orders

OrderId ProductName ProductId OrderDate

1

Tshirt

7748

2022-11-11 16:44:15.581

Code: 

SELECT ProductName, ProductId ,Extract(DAY FROM OrderDate) AS OrderDate FROM Orders;

Explain 

EXTRACT(): This Returns a single part of a date/time. 

Output

ProductName ProductId OrderDate

Tshirt

7748

11

 

 

 

Recommended topics, DCL Commands in SQL and Tcl Commands in SQL

FAQs

  1. What is the difference between SQL and MYSQL?
    SQL is a query programming language for managing relational databases. MySQL is a SQL-based relational database management system. SQL is a querying and operating language for database systems. MySQL allows you to manage, store, edit, and delete data in a structured manner.
     
  2.  What is the use of  Date functions in SQL?
    To convert various data types to a date or DateTime data type, use this function. Returns data of the specified data type. If the string is a valid date, it returns 1; otherwise, it returns 0.

Key Takeaways

In this article, we have extensively discussed the Date functions in SQL along with their implementations.

Until then, All the best for your future endeavors, and Keep Coding. "We hope that this blog has helped you enhance your knowledge regarding this topic, and if you would like to learn more, check out our articles on  Coding Ninjas Studio. To master in SQL, solve most asked SQL Problems here. Do upvote our blog to help other ninjas grow. Happy Coding!”

Live masterclass