Date and Time SQL Commands

Excellent ๐Ÿ‘
Now Date & Time SQL commands in the same exam-perfect pattern you liked:

  • Question

  • SQL

  • Output shown

  • ✔ clear takeaway

(For CURRENT_*, output is example / sample, because it depends on system date & time.)


๐Ÿงพ Summary Table (Date & Time Questions)

No.CommandQuestion
1CURRENT_DATE()Get today’s date
2CURRENT_TIME()Get current time
3CURRENT_TIMESTAMP()Get current date & time
4DATE_PART()Extract year from a date
5DATE_ADD()Add days to a date
6DATE_SUB()Subtract days from a date
7EXTRACT()Extract year from date
8TO_CHAR()Format a date
9TIMESTAMPDIFF()Difference between two timestamps
10DATEDIFF()Difference between two dates

1️⃣ CURRENT_DATE()

Question: Show today’s date.

SELECT CURRENT_DATE() AS current_date;

✅ Output (Example)

current_date
2025-12-17

✔ Returns only the date
✔ No time included


2️⃣ CURRENT_TIME()

Question: Show current system time.

SELECT CURRENT_TIME() AS current_time;

✅ Output (Example)

current_time
00:05:30

✔ Returns only time
✔ Format depends on database


3️⃣ CURRENT_TIMESTAMP()

Question: Show current date and time.

SELECT CURRENT_TIMESTAMP() AS current_timestamp;

✅ Output (Example)

current_timestamp
2025-12-17 00:05:30

✔ Returns date + time together


4️⃣ DATE_PART()

Question: Extract year from a given date.

SELECT DATE_PART('year', '2024-04-11') AS extracted_part;

✅ Output

extracted_part
2024

✔ Extracts a specific part of date
✔ Common parts: year, month, day


5️⃣ DATE_ADD()

Question: Add 1 day to a date.

SELECT DATE_ADD('2024-04-11', INTERVAL 1 DAY) AS new_date;

✅ Output

new_date
2024-04-12

✔ Used to add days / months / years


6️⃣ DATE_SUB()

Question: Subtract 1 day from a date.

SELECT DATE_SUB('2024-04-11', INTERVAL 1 DAY) AS new_date;

✅ Output

new_date
2024-04-10

✔ Used to subtract time from a date


7️⃣ EXTRACT()

Question: Extract year from a date.

SELECT EXTRACT(YEAR FROM '2024-04-11') AS extracted_part;

✅ Output

extracted_part
2024

✔ Similar to DATE_PART
✔ ANSI-SQL standard


8️⃣ TO_CHAR()

Question: Convert date into a specific format.

SELECT TO_CHAR('2024-04-11', 'YYYY-MM-DD') AS formatted_date;

✅ Output

formatted_date
2024-04-11

✔ Converts date/time to text
✔ Used heavily in reports


9️⃣ TIMESTAMPDIFF()

Question: Find difference in days between two timestamps.

SELECT TIMESTAMPDIFF(
  DAY,
  '2024-04-10',
  '2024-04-11'
) AS difference;

✅ Output

difference
1

✔ Can calculate in DAY, HOUR, MINUTE, etc.


๐Ÿ”Ÿ DATEDIFF()

Question: Find difference in days between two dates.

SELECT DATEDIFF('2024-04-11', '2024-04-10') AS difference_in_days;

✅ Output

difference_in_days
1

✔ Returns number of days only
✔ Very common in exams


๐Ÿ”ฅ DATE_PART vs EXTRACT (Exam Trap)

FeatureDATE_PARTEXTRACT
StandardPostgreSQLANSI SQL
Syntax'year', dateYEAR FROM date
PurposeSameSame

๐Ÿง  One-Line Memory Trick

FunctionUse
CURRENT_DATEToday’s date
CURRENT_TIMECurrent time
CURRENT_TIMESTAMPDate + time
DATE_ADDAdd time
DATE_SUBSubtract time
DATEDIFFDate difference
TIMESTAMPDIFFTimestamp difference

If you want next:

  • Date-based interview MCQs

  • Real salary / job portal use-cases

  • All SQL functions in one mega revision table

  • Hindi explanation for exams

Just say ๐Ÿ‘

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.