TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2495 2.53K
๐Ÿ”ฅ Now, letโ€™s move to the next topic

SQL Date & Time Functions โœ…

๐Ÿง  1. Why Date Functions Matter?

Almost every real-world database contains dates ๐Ÿ‘‡

โœ” Orders

โœ” Employee joining dates

โœ” Transactions

โœ” Login activity

SQL date functions help analyze time-based data ๐Ÿ’ฏ

โšก 2. Common Date Functions

Function : Purpose

NOW() : Current date & time

CURDATE() : Current date

CURTIME() : Current time

YEAR() : Extract year

MONTH() : Extract month

DAY() : Extract day

DATEDIFF() : Difference between dates

DATE_FORMAT() : Format dates

๐Ÿ”ฅ 3. NOW(), CURDATE(), CURTIME()

SELECT NOW();

โœ” Current date + time

SELECT CURDATE();

โœ” Current date only

SELECT CURTIME();

โœ” Current time only

๐Ÿ”ฅ 4. YEAR(), MONTH(), DAY()

SELECT YEAR(joining_date)

FROM employees;

SELECT MONTH(joining_date)

FROM employees;

SELECT DAY(joining_date)

FROM employees;

๐Ÿ”ฅ 5. DATEDIFF()

๐Ÿ‘‰ Find difference between dates

SELECT DATEDIFF('2026-06-10', '2026-06-01');

โœ” Result โ†’ 9 days

๐Ÿ”ฅ 6. DATE_FORMAT()

๐Ÿ‘‰ Format dates professionally

SELECT DATE_FORMAT(NOW(), '%d-%m-%Y');

โœ” Example Output โ†’ 06-06-2026

๐ŸŽฏ 7. Real Example

๐Ÿ‘‰ Employees joined in 2025

SELECT *

FROM employees

WHERE YEAR(joining_date) = 2025;

๐ŸŽฏ 8. Practice Tasks

1. Show current date

2. Show current time

3. Extract joining year from employee table

4. Find employees joined in specific month

5. Calculate days between two dates

โšก Mini Challenge ๐Ÿ”ฅ

๐Ÿ‘‰ Find employees who joined in the last 30 days

โœ… Solution Using DATEDIFF()

SELECT *

FROM employees

WHERE DATEDIFF(CURDATE(), joining_date) <= 30;

โœ… Alternative Solution Using DATE_SUB()

SELECT *

FROM employees

WHERE joining_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY);

๐Ÿ”ฅ Pro Tip

Date functions are heavily used in:

๐Ÿ‘‰ Sales reports

๐Ÿ‘‰ Monthly dashboards

๐Ÿ‘‰ Retention analysis

๐Ÿ‘‰ Time-series analytics ๐Ÿ’ฏ

Double Tap โค๏ธ For More
  • โค 11
More from @sqlanalyst
  1. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  2. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  3. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  6. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’