SQL Date Functions Tutorial 📝

beginner
15 min

SQL Date Functions Tutorial 📝

Welcome to our SQL Date Functions tutorial! In this comprehensive guide, we'll dive into various date and time functions in SQL that will help you work with data related to dates and times. By the end of this tutorial, you'll be able to manipulate and analyze date data with ease. 🎯

Table of Contents 📝

  1. Understanding SQL Date Functions

    • What are SQL Date Functions?
    • Importance of Date Functions in SQL
  2. Basic Date Functions

    • CURRENT_DATE
    • SYSDATE
    • CURDATE
  3. Date Arithmetic Functions

    • DATE_ADD
    • DATE_SUB
    • DATEDIFF (MySQL only)
  4. Extracting Date Parts

    • DAY, MONTH, YEAR, HOUR, MINUTE, SECOND
  5. Conversion Functions

    • STR_TO_DATE
    • DATE_FORMAT
  6. Advanced Date Functions

    • NOW
    • EXTRACT
    • LAST_DAY
  7. Quiz

Understanding SQL Date Functions 📝

SQL Date Functions are a set of built-in functions that allow you to work with dates and times in your database. These functions are essential when dealing with data that involves dates and times, such as transaction dates, appointment schedules, and user registrations. 💡

Basic Date Functions 📝

SQL provides several basic date functions to get the current date, including CURRENT_DATE, SYSDATE, and CURDATE. These functions are different across databases, but all provide the current date and time in your database.

Example 📝

sql
SELECT CURRENT_DATE;

Date Arithmetic Functions 📝

You can perform arithmetic operations on dates using SQL's DATE_ADD and DATE_SUB functions. These functions add or subtract the specified interval from a date.

Example 📝

sql
SELECT DATE_ADD(CURDATE(), INTERVAL 7 DAY);

In this example, we add 7 days to the current date.

Extracting Date Parts 📝

You can extract specific parts of a date using SQL's DAY, MONTH, YEAR, HOUR, MINUTE, and SECOND functions.

Example 📝

sql
SELECT EXTRACT(DAY FROM CURDATE());

In this example, we extract the day from the current date.

Conversion Functions 📝

SQL's STR_TO_DATE and DATE_FORMAT functions allow you to convert dates between strings and date formats.

Example 📝

sql
SELECT STR_TO_DATE('2022-03-01', '%Y-%m-%d');

In this example, we convert the string '2022-03-01' into a date.

Advanced Date Functions 📝

SQL offers more advanced date functions such as NOW, EXTRACT, and LAST_DAY. These functions are useful for complex date manipulations and extractions.

Example 📝

sql
SELECT EXTRACT(MONTH FROM LAST_DAY(CURDATE()));

In this example, we extract the month of the last day of the current month.

Quiz 📝

Quick Quiz
Question 1 of 1

Which SQL function provides the current date and time in your database?

Keep learning and practicing, and soon you'll be a SQL date functions expert! 🚀