Discover how your database can become your personal timekeeper, saving you from messy date mistakes!
Why CURRENT_DATE, CURRENT_TIMESTAMP, NOW() in PostgreSQL? - Purpose & Use Cases
Imagine you need to write down the exact date and time every time a customer places an order, but you do it by hand on paper or in a simple text file.
Later, you want to find all orders from today or see when the last order was made.
Manually writing dates and times is slow and easy to mess up.
You might forget to note the time, write the wrong date, or mix formats.
Searching or sorting these handwritten dates is almost impossible without errors.
Using CURRENT_DATE, CURRENT_TIMESTAMP, and NOW() in your database automatically captures the exact date and time when data is added or queried.
This means no mistakes, no extra work, and easy filtering or sorting by date and time.
INSERT INTO orders (order_id, order_date) VALUES (1, '2023-04-01'); -- You type the date yourself
INSERT INTO orders (order_id, order_date) VALUES (1, CURRENT_TIMESTAMP); -- Database adds current date and time automatically
You can track and analyze events by exact date and time effortlessly, making your data reliable and timely.
An online store records the exact moment each purchase happens to manage stock and send timely delivery updates.
Manual date/time entry is slow and error-prone.
Database functions like CURRENT_DATE and NOW() automate accurate timestamps.
This makes data tracking and analysis simple and reliable.
Practice
Solution
Step 1: Understand the function outputs
CURRENT_DATEreturns only the date part (year-month-day) without time.Step 2: Compare with other functions
CURRENT_TIMESTAMPandNOW()return both date and time, whileDATE_PART()extracts parts but is not a current date function.Final Answer:
CURRENT_DATE -> Option AQuick Check:
Only date, no time = CURRENT_DATE [OK]
- Confusing CURRENT_TIMESTAMP with CURRENT_DATE
- Using NOW() when only date is needed
- Thinking DATE_PART() returns current date
Solution
Step 1: Check function syntax
CURRENT_TIMESTAMPis a special function and does not require parentheses.Step 2: Validate other options
SELECT CURRENT_DATE();uses parentheses incorrectly with CURRENT_DATE.SELECT CURRENT_TIME();returns only the time part (time type), not full timestamp.SELECT NOW;misses required parentheses for NOW().SELECT CURRENT_TIMESTAMP;is the correct and preferred standard syntax.Final Answer:
SELECT CURRENT_TIMESTAMP; -> Option BQuick Check:
Standard timestamp function = CURRENT_TIMESTAMP [OK]
- Adding parentheses to CURRENT_DATE
- Omitting parentheses for NOW()
- Confusing function syntax
SELECT CURRENT_DATE, CURRENT_TIMESTAMP, NOW();Solution
Step 1: Identify each function's return type
CURRENT_DATEreturns a date type (no time).CURRENT_TIMESTAMPandNOW()return timestamp with time zone.Step 2: Confirm output columns
The query returns three columns: one date and two timestamps with time zone.Final Answer:
Three columns: date, timestamp with time zone, timestamp with time zone -> Option DQuick Check:
Types: date, timestamptz, timestamptz = Three columns: date, timestamp with time zone, timestamp with time zone [OK]
- Assuming all return date only
- Thinking NOW() returns date
- Expecting syntax error
SELECT CURRENT_DATE();What is the problem?
Solution
Step 1: Check function usage
CURRENT_DATEis a special keyword, not a function, so it does not accept parentheses.Step 2: Understand error cause
Using parentheses causes syntax error because PostgreSQL expects no parentheses for CURRENT_DATE.Final Answer:
CURRENT_DATE is not a function and should not have parentheses -> Option CQuick Check:
No parentheses with CURRENT_DATE [OK]
- Adding () to CURRENT_DATE
- Thinking CURRENT_DATE needs parameters
- Confusing with NOW() syntax
created_date DATE and created_time TIMESTAMP. Which INSERT statement is correct?Solution
Step 1: Match data types with values
created_dateis DATE type, soCURRENT_DATEfits.created_timeis TIMESTAMP, soNOW()fits.Step 2: Check syntax correctness
INSERT INTO table_name (created_date, created_time) VALUES (CURRENT_DATE, NOW()); uses correct functions and order. INSERT INTO table_name (created_date, created_time) VALUES (NOW(), CURRENT_DATE); swaps types incorrectly. INSERT INTO table_name (created_date, created_time) VALUES (CURRENT_TIMESTAMP, CURRENT_DATE); uses timestamp for date column (wrong type). INSERT INTO table_name (created_date, created_time) VALUES (CURRENT_DATE(), CURRENT_TIMESTAMP()); uses parentheses incorrectly with CURRENT_DATE and CURRENT_TIMESTAMP.Final Answer:
INSERT INTO table_name (created_date, created_time) VALUES (CURRENT_DATE, NOW()); -> Option AQuick Check:
Date = CURRENT_DATE, Timestamp = NOW() [OK]
- Swapping date and timestamp values
- Using parentheses with CURRENT_DATE
- Using CURRENT_TIMESTAMP for DATE column
Ready to prove it?
Pass the test and this pattern moves unknown
