JEEVAN GEORGE ANTONY
JEEVAN GEORGE ANTONY

Reputation: 186

Query to get current year from Amazon Redshift

Trying to find the SQL query to get current year from Amazon redshift db.

Tried following queries but didn't worked :

select DATE_PART_YEAR(SYSDATE);
select DATE_PART_YEAR(getdate());

Upvotes: 1

Views: 13047

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269623

You can use:

extract(year from current_date)

You might as well use standard SQL for this. The above is standard. There are numerous other alternatives, such as:

extract(year from sysdate)
date_part_year(trunc(getdate()))
to_char(current_date, 'YYYY')

Upvotes: 4

JEEVAN GEORGE ANTONY
JEEVAN GEORGE ANTONY

Reputation: 186

On further reading through Amazon docs and with trial and error, found following queries to work returning current year value :

select DATE_PART_YEAR(TRUNC(SYSDATE));
select DATE_PART_YEAR(TRUNC(getdate()));
select extract(year from sysdate);
select extract(year from current_date);

Trunc method removes the timezone part and returns date value, which is the value being expected by DATE_PART_YEAR method.

Upvotes: 0

Related Questions