0

For example:

year
-----
1999
2001
2004
2006

How do i return years that are greater than 2000?

Year is stored as date format.

1

1 Answer 1

3

Using PostgreSQL specific DATE_PART:

SELECT DATE_PART('year', t.date_column)
  FROM YOUR_TABLE t
 WHERE t.date_column > DATE '2000-01-01'

Using EXTRACT (ANSI):

SELECT EXTRACT(YEAR FROM t.date_column)
  FROM YOUR_TABLE t
 WHERE t.date_column > DATE '2000-01-01'

You could use the function in the WHERE clause to filter out the years/etc, but doing so renders an index on the column useless.

Sign up to request clarification or add additional context in comments.

3 Comments

When using the EXTRACT in the WHERE clause, a function based index on extract(year from date_column) would be usable.
@a_horse_with_no_name: True, didn't know PostgreSQL supported them -- do you happen to know what the first version was to support them?
The manual of 6.5 already mentions the usage of functions in the CREATE INDEX syntax. So I'd say: "always"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.