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

Extract The Year From A Date

Return the year component from a date or timestamp value.

Docker-validated Not currently validation-green

Read the year from a launch date

The output is the same year number even though the extraction function differs.

MySQL MariaDB PostgreSQL
Engine-specific syntax
Setup
CREATE TABLE products (launch_date DATE);

INSERT INTO
  products (launch_date)
VALUES
  ('2022-07-15');
SQL
SELECT
  EXTRACT(
    YEAR
    FROM
      launch_date
  ) AS launch_year
FROM
  products;
launch_year
2022
SQL Server
Engine-specific syntax
Setup
CREATE TABLE products (launch_date DATE);

INSERT INTO
  products (launch_date)
VALUES
  ('2022-07-15');
SQL
SELECT
  YEAR (launch_date) AS launch_year
FROM
  products;
launch_year
2022
SQLite
Engine-specific syntax
Setup
CREATE TABLE products (launch_date DATE);

INSERT INTO
  products (launch_date)
VALUES
  ('2022-07-15');
SQL
SELECT
  CAST(strftime ('%Y', launch_date) AS INTEGER) AS launch_year
FROM
  products;
launch_year
2022

SQL Server uses `YEAR()` while the others can use `EXTRACT(...)` in this example.

Where this command helps.

  • grouping data by reporting year
  • filtering or labeling rows by the year portion of a date

What the command is doing.

Date-part extraction is common in reporting and grouping queries. The exact function differs by engine, so this command uses per-engine SQL while keeping the result aligned across all engines.