1. Home
  2. Blog
  3. Databases & SQL

Databases & SQL

SQL Date Functions Explained: Adding, Comparing and Grouping Dates Without Bugs

Dates cause more quiet reporting bugs than almost anything else in SQL. Learn the core date operations, the BETWEEN trap that drops most of a day’s orders, month-end surprises and how each database spells the same thing differently.

10 min read
SQL date functions explained: adding, comparing and grouping dates without bugs

Key takeaways

  • Date syntax differs a lot between databases. The ideas are the same; the function names are not.
  • For date ranges, use half-open ranges: >= start AND < next_day, not BETWEEN.
  • Adding a month to 31 January does not always give the end of February. In SQLite it gave 3 March.
  • Wrapping a column in a function, like date(created_at), can stop an index from being used.

A daily sales report shows almost nothing for 10 November, even though the shop was busy all day. The query looked correct: WHERE created_at BETWEEN '2026-11-10' AND '2026-11-10'. It only matched orders placed at exactly midnight. Date logic is full of traps like this, and they rarely cause errors; they just produce wrong numbers.

We ran the examples in SQLite 3.50, which stores dates as text and uses functions like date() and strftime(). The outputs are from that run. Equivalents for PostgreSQL, MySQL and SQL Server are listed from their documentation; we did not run those.

Date and time types

Type Holds Example
DATE Calendar date only 2026-11-10
TIME Time of day only 14:35:00
TIMESTAMP / DATETIME Date and time 2026-11-10 14:35:00
TIMESTAMPTZ (PostgreSQL) An absolute moment, time-zone aware 2026-11-10 09:05:00+00
INTERVAL A duration 30 days

Use the database’s real date types where available, not plain text. SQLite has no dedicated date type, so it relies on ISO 8601 text, YYYY-MM-DD HH:MM:SS, which sorts correctly as text. In SQLite, date('2026-11-10 14:35:00'), time(...) and datetime(...) returned 2026-11-10, 14:35:00 and the full timestamp.

Current date and time

Every database has a way to get “now”, but they differ: CURRENT_DATE and CURRENT_TIMESTAMP are standard and widely supported; MySQL adds NOW() and CURDATE(); SQL Server uses GETDATE() or SYSDATETIME(). Know whether “now” is in UTC or the server’s local time zone, because that affects every “today” query.

Adding and subtracting time

SELECT date('2026-11-10', '+30 days');   -- 2026-12-10
SELECT date('2026-11-10', 'start of month');                         -- 2026-11-01
SELECT date('2026-11-10', 'start of month', '+1 month', '-1 day');   -- 2026-11-30

SQLite uses modifiers; the last example finds the last day of the month by going to the first of the next month and stepping back a day, a trick that works in every database. The same “add 30 days” looks like this elsewhere:

-- PostgreSQL:  d + INTERVAL '30 days'
-- MySQL:       DATE_ADD(d, INTERVAL 30 DAY)
-- SQL Server:  DATEADD(DAY, 30, d)

Month-end and leap-year surprises

SELECT date('2026-01-31', '+1 month');   -- 2026-03-03
SELECT date('2028-02-29', '+1 year');    -- 2029-03-01

“31 February” does not exist, so SQLite rolled the extra days into March. Other databases clamp to the last valid day instead: PostgreSQL, MySQL and SQL Server document that adding a month to 31 January gives 28 or 29 February. Neither is wrong, but they disagree, so a billing job that “adds one month” can charge on different days depending on the database. For subscriptions, store the intended day of the month and calculate each date from it, rather than adding a month to the previous date repeatedly.

Tip: Always test date logic on the boundaries: the 29th, 30th and 31st of a month, 28 and 29 February in leap and non-leap years, 31 December and the days clocks change. These are exactly the cases boundary value analysis tells you to pick.

Differences between dates

SELECT julianday('2026-12-25') - julianday('2026-11-10');   -- 45.0

Forty-five days to 25 December. SQLite converts both dates to Julian day numbers and subtracts. Other dialects:

-- PostgreSQL:  DATE '2026-12-25' - DATE '2026-11-10'   (an integer)
-- MySQL:       DATEDIFF('2026-12-25', '2026-11-10')    (later date first)
-- SQL Server:  DATEDIFF(DAY, '2026-11-10', '2026-12-25') (unit first, earlier date first)

Watch the argument order: MySQL and SQL Server both have DATEDIFF but put the dates in opposite orders, so a query copied between them returns a negative number. SQL Server’s DATEDIFF also counts boundaries crossed, not full periods: the difference in years between 31 December and 1 January is 1.

Extracting parts of a date

SELECT strftime('%w', '2026-11-10'),   -- 2   (day of week, 0 = Sunday)
       strftime('%Y-%m', '2026-11-10'), -- 2026-11
       strftime('%j', '2026-11-10');    -- 314 (day of year)

10 November 2026 is a Tuesday, day 2 when Sunday is 0. Be careful here too: day-of-week numbering differs by database and settings, with some starting the week on Monday. The standard EXTRACT(YEAR FROM d) works in PostgreSQL and MySQL; MySQL and SQL Server also offer YEAR(d), MONTH(d) and DAY(d).

The BETWEEN trap

We stored six orders, including some just before and after 10 November:

id 1  2026-11-09 23:30:00
id 2  2026-11-10 00:15:00
id 3  2026-11-10 18:00:00
id 4  2026-11-10            (date only)
id 5  2026-11-11 00:00:00
id 6  10/11/2026            (wrong format)

WHERE created_at BETWEEN '2026-11-10' AND '2026-11-10'          -- [4]
WHERE created_at >= '2026-11-10' AND created_at < '2026-11-11'  -- [2, 3, 4]

BETWEEN matched only order 4, stored without a time. Orders 2 and 3 have times after midnight, so they are greater than '2026-11-10' and fall outside the range. The half-open range, from the start of the day up to but not including the next day, matched all three orders on 10 November and correctly excluded order 5 at midnight on the 11th. This pattern works for any period, such as a month (>= '2026-11-01' AND < '2026-12-01'), and never needs to know how many days a month has.

Grouping by day and month

SELECT date(created_at) AS d, SUM(total)
FROM orders
WHERE date(created_at) IS NOT NULL
GROUP BY d ORDER BY d;

-- 2026-11-09  100
-- 2026-11-10  550
-- 2026-11-11  400

Truncating a timestamp to its day or month is the basis of most reports. PostgreSQL uses DATE_TRUNC('month', d); SQL Server 2022 added DATETRUNC; MySQL has no direct equivalent, so people use DATE_FORMAT(d, '%Y-%m-01'). Remember that days with no orders do not appear at all. For charts, join to a calendar table or generated date series so empty days show as zero. See our GROUP BY guide for more on aggregation.

Dates and indexes

EXPLAIN QUERY PLAN ... WHERE date(created_at) = '2026-11-10'
-- SCAN orders USING COVERING INDEX ix

EXPLAIN QUERY PLAN ... WHERE created_at >= '2026-11-10' AND created_at < '2026-11-11'
-- SEARCH orders USING COVERING INDEX ix (created_at>? AND created_at<?)

Both return the same rows, but wrapping the column in date() forced a scan of the whole index, while the range let the database jump straight to the right section. On a large table, that is the difference between milliseconds and seconds. Keep the column bare on one side of the comparison, and apply functions to the constants instead. Our SQL indexes guide explains why.

Badly formatted dates

Order 6 was stored as 10/11/2026. SQLite’s date() returned NULL for it, so it silently disappeared from both the day filter and the report. Is it 10 November or 11 October? Nobody can tell. Store dates in a real date column or in ISO format, validate input at the boundary, and add a CHECK constraint where the database allows, so bad values are rejected instead of quietly ignored.

Time zones

An order at 23:30 in India on 9 November is 18:00 UTC the same day, but an order at 02:00 IST on 10 November is 20:30 UTC on 9 November. Which day it “belongs to” depends on whose day you mean. The common rule is to store timestamps in UTC, or in a time-zone-aware type, and convert to the user’s or business’s local time only when reporting. In SQLite we shifted a UTC timestamp by hand: datetime('2026-11-10 12:00:00', '+5 hours', '+30 minutes') gave 17:30. PostgreSQL’s AT TIME ZONE and MySQL’s CONVERT_TZ do this properly, including daylight saving rules.

Dialect cheat sheet

Task PostgreSQL MySQL SQL Server
Today CURRENT_DATE CURDATE() CAST(GETDATE() AS DATE)
Add 7 days d + INTERVAL '7 days' DATE_ADD(d, INTERVAL 7 DAY) DATEADD(DAY, 7, d)
Days between d2 - d1 DATEDIFF(d2, d1) DATEDIFF(DAY, d1, d2)
Year EXTRACT(YEAR FROM d) YEAR(d) YEAR(d)
Start of month DATE_TRUNC('month', d) DATE_FORMAT(d, '%Y-%m-01') DATETRUNC(month, d) (2022+)
Format TO_CHAR(d, 'YYYY-MM') DATE_FORMAT(d, '%Y-%m') FORMAT(d, 'yyyy-MM')

Final thoughts

Date bugs hide because the queries run without errors. Use real date types, store times in UTC, filter with half-open ranges instead of BETWEEN, keep indexed columns free of functions, and test the month-end, leap-year and midnight boundaries. When moving SQL between databases, rewrite date logic deliberately rather than assuming it carries over.

Frequently asked questions

Why does BETWEEN miss rows for a date?

BETWEEN ‘2026-11-10’ AND ‘2026-11-10’ only matches midnight when the column has a time. Use created_at >= ‘2026-11-10’ AND created_at < ‘2026-11-11’ instead.

How do I add days to a date in SQL?

It depends on the database: d + INTERVAL ‘7 days’ in PostgreSQL, DATE_ADD(d, INTERVAL 7 DAY) in MySQL, DATEADD(DAY, 7, d) in SQL Server and date(d, ‘+7 days’) in SQLite.

How do I get the number of days between two dates?

Subtract dates in PostgreSQL, use DATEDIFF in MySQL or SQL Server (with different argument orders), or subtract julianday values in SQLite.

What happens when I add a month to January 31?

It varies. SQLite returned March 3 in our test, while PostgreSQL, MySQL and SQL Server clamp to the end of February according to their documentation.

Why is my date query slow?

Applying a function to an indexed column, such as date(created_at), can force a full scan. Compare the bare column with a range of values instead.

Keep learning

Work with Stack Quora

Need help with testing, WordPress or SEO?

We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.

Keep reading

More guides on testing, AI & modern development

Enjoyed this article? Pick a topic and jump straight into our newest tutorials.

Artificial Intelligence

LLM Structured Output Explained: Getting Reliable JSON From AI Models

LLM structured output explained with validation we ran in Pydantic: JSON mode and schemas, constrained decoding, the ways model output…

Read article →
DevOps

SSH Keys Explained: How They Work, ssh-keygen, Ed25519 and Safe Habits

SSH keys explained with keys we generated: public vs private keys, ssh-keygen, Ed25519 vs RSA, fingerprints, passphrases, ssh-agent, authorized_keys, config…

Read article →
DevOps

HTTP Caching Explained: Cache-Control, ETag and 304 Responses, With a Live Test

HTTP caching explained with a small server we ran: Cache-Control directives, max-age, no-cache vs no-store, ETag revalidation and 304 responses,…

Read article →
Test Automation

API Contract Testing Explained: Catch Breaking Changes Before They Ship

API contract testing explained with a schema check we ran: what a contract is, consumer-driven contracts, Pact, OpenAPI, which changes…

Read article →
Software Testing

Risk-Based Testing Explained: Score Risks, Build a Matrix and Test What Matters

Risk-based testing explained step by step: identifying risks, scoring likelihood and impact, building a risk matrix, mapping risk to test…

Read article →
Databases & SQL

SQL Constraints Explained: NOT NULL, UNIQUE, CHECK and FOREIGN KEY, Tested

SQL constraints explained with tests we ran: NOT NULL, UNIQUE, CHECK, DEFAULT, PRIMARY and FOREIGN KEY, the NULL loopholes, SQLite's…

Read article →
Databases & SQL

SQL Date Functions Explained: Adding, Comparing and Grouping Dates Without Bugs

SQL date functions explained with queries we ran: adding intervals, month-end surprises, date differences, the BETWEEN trap, grouping by day,…

Read article →
Software Development

Python asyncio Explained: async, await, gather and TaskGroup With Real Timings

Python asyncio explained with code we timed: coroutines, await, gather, TaskGroup, error handling, timeouts, limiting concurrency and the blocking-call mistake…

Read article →
Software Development

Debounce vs Throttle in JavaScript: How They Work, When to Use Each, Tested

Debounce vs throttle in JavaScript explained with timings we measured in Node.js: how each works, simple implementations, leading and trailing…

Read article →
Software Development

Python Context Managers Explained: The with Statement, contextlib and Real Uses

Python context managers explained with code we ran: how with works, __enter__ and __exit__, contextlib.contextmanager, suppress, ExitStack, database transactions and…

Read article →
Artificial Intelligence

LLM Temperature and Top-p Explained: How AI Models Choose Their Next Word

LLM temperature and top-p explained with a simulation we ran: how probabilities are reshaped, what top-p keeps, recommended settings by…

Read article →
DevOps

YAML Explained: Syntax, the Norway Problem and Other Traps We Tested

YAML explained with a parser test: basic syntax, lists and maps, the Norway problem, versions turning into floats, octal zip…

Read article →
DevOps

Cron Jobs Explained: Syntax, Real Schedules and the Mistakes That Break Them

Cron jobs explained with schedules we verified: the five fields, operators, real examples, the day-of-month OR day-of-week trap, environment problems,…

Read article →
Software Testing

Bug Life Cycle in Software Testing: Every Status, Who Owns It and Common Mistakes

The bug life cycle explained: every defect status from New to Closed, who owns each step, rejected, deferred and reopened…

Read article →
Test Automation

Property-Based Testing in Python: Hypothesis Finds the Bugs Your Examples Miss

Property-based testing explained with Hypothesis tests we ran: properties vs examples, strategies, shrinking, the real bugs it found in our…

Read article →
Databases & SQL

SQL Pagination Explained: OFFSET vs Keyset, With a Million-Row Test

SQL pagination explained with a million-row test we ran: LIMIT and OFFSET, why deep pages get slow, keyset (cursor) pagination,…

Read article →
Databases & SQL

SQL EXISTS vs IN: Differences, the NOT IN NULL Trap and Performance

SQL EXISTS vs IN explained with queries we ran: how each works, why NOT IN returns no rows when a…

Read article →
Software Development

Python Logging Explained: Levels, Handlers, Formatters and Mistakes to Avoid

Python logging explained with code we ran: levels, basicConfig quirks, named loggers, handlers and formatters, logger.exception, lazy formatting, duplicate logs…

Read article →
Software Development

JavaScript Map vs Object: Key Differences, Pitfalls and a Benchmark

JavaScript Map vs Object explained with code we ran in Node.js: key types, ordering, the prototype trap, JSON, size, WeakMap…

Read article →
Software Development

Python Type Hints Explained: Annotations, mypy and the Errors It Catches

Python type hints explained with a real mypy run: basic annotations, Optional and unions, TypedDict, Literal, Protocol, generics, Callable, strict…

Read article →
Artificial Intelligence

LLM Tokens Explained: How AI Models Read Text, Count Costs and Fill Context Windows

LLM tokens explained with a tokenizer we ran: how text is split, why counts differ by language and tokenizer, context…

Read article →
DevOps

Semantic Versioning Explained: MAJOR.MINOR.PATCH, Ranges and Release Rules

Semantic versioning explained with code we ran: MAJOR.MINOR.PATCH rules, pre-releases, correct version sorting, caret and tilde ranges, 0.x versions, lockfiles…

Read article →
DevOps

Git Reset vs Revert: How to Undo Commits Safely, With a Tested Example

Git reset vs revert explained with a repository we built: soft, mixed and hard reset, revert commits, reverting merges, recovering…

Read article →
Software Testing

The Test Pyramid Explained: Layers, Ratios and When to Break the Shape

The test pyramid explained: unit, integration and end-to-end layers, sensible ratios, the ice cream cone anti-pattern, the testing trophy and…

Read article →
Test Automation

pytest Fixtures Explained: Scope, yield Teardown, conftest.py and Factories

pytest fixtures explained with a suite we ran: how fixtures work, scopes, yield teardown, conftest.py, factory fixtures, parametrized fixtures, built-ins…

Read article →
Databases & SQL

SQL Upsert Explained: INSERT ON CONFLICT, MERGE and the REPLACE Trap

SQL upsert explained with queries we ran: INSERT ON CONFLICT DO UPDATE and DO NOTHING, the excluded row, conditional updates,…

Read article →
Databases & SQL

SQL LIKE Operator Explained: Wildcards, Case Sensitivity and Performance

The SQL LIKE operator explained with queries we ran: % and _ wildcards, escaping, NOT LIKE and NULL, case sensitivity…

Read article →
Artificial Intelligence

AI Hallucinations Explained: Why Language Models Make Things Up and How to Reduce It

AI hallucinations explained: what they are, why language models produce them, common types, how to reduce them with grounding and…

Read article →
Test Automation

Mocking in Python: unittest.mock, patch and the Mistakes That Make Tests Lie

Mocking in Python with unittest.mock, tested: Mock and return_value, side_effect, patching where a name is used, spec and autospec, mocking…

Read article →
Software Testing

Code Coverage Explained: Line vs Branch Coverage and What 100% Really Means

Code coverage explained with a real coverage.py run: line, branch and condition coverage, reading a report, why 100% coverage can…

Read article →
Artificial Intelligence

What Are AI Agents? How They Work, With a Simple Agent Loop You Can Run

What are AI agents? A plain guide to the agent loop, tools, memory and guardrails, with a small Python loop…

Read article →
Test Automation

TDD vs BDD: Key Differences, One Feature Built Both Ways and When to Use Each

TDD vs BDD explained with one discount feature built both ways: red-green-refactor with code we ran, Given-When-Then scenarios, tools, trade-offs…

Read article →
Software Testing

User Acceptance Testing (UAT): Process, Checklist and Real Examples

User acceptance testing explained: who runs it, how it differs from QA, a seven-step process, a UAT test case template,…

Read article →
Artificial Intelligence

What Are Embeddings? Vectors and Cosine Similarity Explained Simply

What are embeddings? A plain-language guide to vectors, cosine similarity and semantic search, with a small example we ran, common…

Read article →
Test Automation

Flaky Tests: What Causes Them and How to Fix Them for Good

Flaky tests explained with demos we ran: the main causes, how to confirm and diagnose one, proper fixes for timing,…

Read article →
Software Testing

Load Testing vs Stress Testing: Differences, Metrics and k6 Examples

Load testing vs stress testing explained: what each proves, spike and soak tests, the metrics that matter, why averages mislead,…

Read article →