SQL-Engineering-Handbook

01 — Current Date Functions

Introduction

Almost every recurring business report begins with the same implicit question: “as of right now, what does the data look like?” Before you can filter last month’s orders, calculate an employee’s tenure, or flag an overdue invoice, SQL needs a reliable way to answer one thing first — what is “now”?

This file covers the functions that answer that question, and — more importantly — the engineering judgment behind using them safely in production systems.


Concept Overview

SQL engines expose several functions that return the current date and/or time, evaluated by the database server, not the application, not the browser, and not the analyst’s laptop. Understanding which of these functions to use, and when they are evaluated during query execution, is the foundation for everything else in this module.

Function Returns Type
CURRENT_DATE / CURDATE() Today’s date DATE
CURRENT_TIME Current time of day TIME
CURRENT_TIMESTAMP / NOW() Current date and time DATETIME
SYSDATE() Current date and time at the moment the function executes, not the start of the statement DATETIME

Why This Exists

Applications constantly need a reference point for “now” to answer relative questions: Is this invoice overdue? Has this employee passed their probation period? Is this subscription still active? Hard-coding a date into a query makes it correct for exactly one day and wrong every day after. CURRENT_DATE and NOW() make queries self-updating — a report written today produces the correct answer next year without modification.


Business Context

Consider a payroll system that runs a nightly job to flag employees who have completed their 90-day probation. If the query hard-codes '2024-06-01' as “today,” the report is correct once and silently wrong forever after. Using CURDATE() makes the report correct on every single run, indefinitely, with zero maintenance.


Real Company Examples


Where It Is Used


Functions Covered


Syntax Explanation

SELECT CURRENT_DATE;        -- 2026-07-07
SELECT CURDATE();           -- 2026-07-07  (MySQL alias, identical result)
SELECT CURRENT_TIME;        -- 14:32:07
SELECT CURRENT_TIMESTAMP;   -- 2026-07-07 14:32:07
SELECT NOW();                -- 2026-07-07 14:32:07  (MySQL alias for CURRENT_TIMESTAMP)
SELECT SYSDATE();            -- 2026-07-07 14:32:09  (evaluated live, see below)

CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP are technically SQL keywords, not functions — they can be used with or without parentheses in MySQL (CURRENT_TIMESTAMP or CURRENT_TIMESTAMP()), but the parenthesis-free form is the ANSI SQL standard and is portable across PostgreSQL and SQL Server.


Visual Explanation

NOW() vs CURDATE() vs SYSDATE() evaluation timing

Statement starts execution
        │
        ▼
 NOW() / CURRENT_TIMESTAMP  ──►  frozen at statement start, same value
 CURDATE() / CURRENT_DATE   ──►  every row in the result set
        │
        ▼
 SYSDATE()  ──►  re-evaluated at the exact instant the function is called,
                  which can differ row-to-row in a long-running statement

Step-by-Step Walkthrough

  1. A query begins execution. MySQL fixes the value of NOW() / CURRENT_TIMESTAMP / CURDATE() at the start of the statement.
  2. Every reference to NOW() within that same statement — even across multiple rows or subqueries — returns that same frozen value.
  3. SYSDATE(), by contrast, is not fixed at statement start. If called multiple times within a long-running statement, it can return a different value on each call.
  4. This distinction rarely matters for a simple SELECT, but it matters enormously for row-level default values, replication consistency, and any statement that both reads and writes based on “the current time.”

Production Considerations


Performance Notes


Edge Cases


Common Mistakes


Interview Questions

  1. “What is the difference between NOW() and SYSDATE() in MySQL?” NOW() is fixed at the start of the statement; SYSDATE() is re-evaluated at the exact moment it’s called, which can vary within a single long-running statement or across replication.

  2. “Why is WHERE DATE(created_at) = CURDATE() considered an anti-pattern?” It wraps an indexed column in a function, forcing a full scan instead of an index seek. The sargable alternative uses a range comparison directly on the raw column.

  3. “How would you write a query that is correct every day without modification?” Use CURRENT_DATE / CURDATE() for relative filtering instead of hard-coded literals.


Summary

CURRENT_DATE, NOW(), and their relatives give SQL a self-updating reference point for “now,” evaluated safely and consistently by the database server. The key engineering distinctions are: statement-time evaluation (NOW()) versus call-time evaluation (SYSDATE()), DATE versus DATETIME return types, and writing filters that remain sargable so indexes stay usable. Master these distinctions here — every later file in this module assumes it.


Practice Challenges

  1. Write a query that returns today’s date, the current timestamp, and the current time of day, each in its own labeled column.
  2. Write a sargable filter that selects all orders placed today from a DATETIME column named order_timestamp, without wrapping the column in a function.
  3. Explain, in your own words, why a table’s created_at column should default to CURRENT_TIMESTAMP rather than being set by application code.

Further Reading


Next: 02 — Date Extraction →