sqlcmd.net validated sql reference
intermediate date-time MySQL MariaDB SQL Server PostgreSQL SQLite

Calculate The Number Of Days Between Two Dates

Find how many days separate two date values using each engine's date difference function.

Docker-validated Not currently validation-green

Calculate the number of days each project ran

Alpha ran from January 5 to January 20, a span of 15 days. Beta ran from February 1 to February 28, a span of 27 days. Note that MySQL and SQL Server share the function name but differ in argument order: MySQL puts the end date first, SQL Server puts the unit first and the start date second.

MySQL MariaDB
Engine-specific syntax
Setup
CREATE TABLE projects (
  id INT,
  name VARCHAR(50),
  start_date VARCHAR(10),
  end_date VARCHAR(10)
);

INSERT INTO
  projects (id, name, start_date, end_date)
VALUES
  (1, 'Alpha', '2024-01-05', '2024-01-20'),
  (2, 'Beta', '2024-02-01', '2024-02-28');
SQL
SELECT
  name,
  DATEDIFF (end_date, start_date) AS days
FROM
  projects
ORDER BY
  id;
namedays
Alpha15
Beta27
SQL Server
Engine-specific syntax
Setup
CREATE TABLE projects (
  id INT,
  name VARCHAR(50),
  start_date VARCHAR(10),
  end_date VARCHAR(10)
);

INSERT INTO
  projects (id, name, start_date, end_date)
VALUES
  (1, 'Alpha', '2024-01-05', '2024-01-20'),
  (2, 'Beta', '2024-02-01', '2024-02-28');
SQL
SELECT
  name,
  DATEDIFF (DAY, start_date, end_date) AS days
FROM
  projects
ORDER BY
  id;
namedays
Alpha15
Beta27
PostgreSQL
Engine-specific syntax
Setup
CREATE TABLE projects (
  id INT,
  name VARCHAR(50),
  start_date VARCHAR(10),
  end_date VARCHAR(10)
);

INSERT INTO
  projects (id, name, start_date, end_date)
VALUES
  (1, 'Alpha', '2024-01-05', '2024-01-20'),
  (2, 'Beta', '2024-02-01', '2024-02-28');
SQL
SELECT name, end_date::DATE - start_date::DATE AS days FROM projects ORDER BY id;
namedays
Alpha15
Beta27
SQLite
Engine-specific syntax
Setup
CREATE TABLE projects (
  id INT,
  name VARCHAR(50),
  start_date VARCHAR(10),
  end_date VARCHAR(10)
);

INSERT INTO
  projects (id, name, start_date, end_date)
VALUES
  (1, 'Alpha', '2024-01-05', '2024-01-20'),
  (2, 'Beta', '2024-02-01', '2024-02-28');
SQL
SELECT
  name,
  CAST(
    JULIANDAY (end_date) - JULIANDAY (start_date) AS INT
  ) AS days
FROM
  projects
ORDER BY
  id;
namedays
Alpha15
Beta27

MySQL/MariaDB use DATEDIFF(end, start). SQL Server uses DATEDIFF(day, start, end) — note reversed date argument order. PostgreSQL subtracts DATE values directly. SQLite uses JULIANDAY subtraction.

Where this command helps.

  • computing the duration of a project or task in days
  • calculating how many days have elapsed since a reference date

What the command is doing.

Each engine uses a different function to compute the number of days between two dates. MySQL and MariaDB use DATEDIFF(end, start) with the later date first. SQL Server also uses DATEDIFF but with the unit as the first argument: DATEDIFF(day, start, end). PostgreSQL subtracts two DATE values directly with the - operator. SQLite uses JULIANDAY(end) - JULIANDAY(start) cast to an integer.