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;