0% found this document useful (0 votes)
44 views5 pages

SQL Date Functions Implementation Guide

The document outlines practical SQL queries for various date functions, including adding months, calculating the difference between dates, rounding dates, finding the next occurrence of a specific day, truncating dates, identifying the greatest date, and converting timezones. Each query is accompanied by an example demonstrating its application using a sample_dates table. The aim is to implement these functions effectively in SQL.

Uploaded by

kunal bandale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

SQL Date Functions Implementation Guide

The document outlines practical SQL queries for various date functions, including adding months, calculating the difference between dates, rounding dates, finding the next occurrence of a specific day, truncating dates, identifying the greatest date, and converting timezones. Each query is accompanied by an example demonstrating its application using a sample_dates table. The aim is to implement these functions effectively in SQL.

Uploaded by

kunal bandale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Shri Guru Gobind Singhji Institute of Engineering and Technology

Department of Information Technology


Name: Kunal Bandale
Reg. No.: 2022BIT501

Practical No. 3

AIM: Implement SQL queries using data functions like add months, calculating months
between two dates, round, next day, truncate, greatest, new time etc.

Creating Table:
1. Adding months to date:
Query: SELECT id,
start_date,
DATE_ADD(start_date, INTERVAL 3 MONTH) as new_start_date
FROM sample_dates;

2. Calculate months between two dates


Query: SELECT id,
end_date,
TIMESTAMPDIFF(MONTH, start_date, end_date) as months_between
FROM sample_dates;
3. Rounding a date
Query: SELECT id,
start_date,
DATE_FORMAT(start_date, '%Y-%m-%d') as rounded_start_date
FROM sample_dates;

4. Finding next week of a date


Query: SELECT id,
start_date,
DATE_ADD(start_date, INTERVAL (7 - WEEKDAY(start_date) + IF
(WEEKDAY(start_date) >= 5, 7, 0)) DAY) as next_friday
FROM sample_dates;

5. Truncating a date
Query: SELECT id,
start_date,
DATE_FORMAT(start_date, '%Y-%m-%d') as truncated_start_date
FROM sample_dates;

6. Finding greatest date


Query: SELECT id,
start_date,
GREATEST(start_date, end_date) as greatest_date
FROM sample_dates;

7. Converting timezones
Query: SELECT id,
start_date,
CONVERT_TZ(start_date, '+00:00', '-07:00') as local_time
FROM sample_dates;

You might also like