MONTH() equivalent

From PostgreSQL wiki

Jump to: navigation, search

Compatibility Snippets

MONTH() substitute

Works with PostgreSQL

Any version

Written in

SQL

Depends on

Nothing


These functions extract the month from timestamp (with/without TZ and date format). If you want to extract another piece of data (like year), you only have to replace the correct word in the extract function.

CREATE OR REPLACE FUNCTION MONTH(TIMESTAMP without TIME ZONE) RETURNS INTEGER
AS $$ 
      SELECT EXTRACT(MONTH FROM $1)::INTEGER; 
$$ LANGUAGE SQL IMMUTABLE;
CREATE OR REPLACE FUNCTION MONTH(TIMESTAMP WITH TIME ZONE) RETURNS INTEGER
AS $$
      SELECT EXTRACT(MONTH FROM $1)::INTEGER;
$$ LANGUAGE SQL STABLE;
CREATE OR REPLACE FUNCTION MONTH(DATE) RETURNS INTEGER
AS $$
      SELECT EXTRACT(MONTH FROM $1)::INTEGER;
$$ LANGUAGE SQL IMMUTABLE;

See also

EXTRACT() documentation

Personal tools