5

I have a date and I want to get just the year from it as an integer.

4
  • 2
    a google search would've helped you. Commented Apr 13, 2017 at 16:57
  • extract(year from datecol)::int Commented Apr 13, 2017 at 17:05
  • @vkp i apologise im really new on pgsql and sql in general and i was confused with the google results as they ended up as dates or characters from extracting. all i needed was a simple integer format on top of that which i thought after seeing the suggestions Commented Apr 13, 2017 at 17:19
  • @OtoShavadze thanks a lot Commented Apr 13, 2017 at 17:19

2 Answers 2

18

Use extract

SELECT extract( year FROM CURRENT_DATE )::int;
Sign up to request clarification or add additional context in comments.

Comments

-2

It's pretty simple, really...

SELECT year([datecolumn])      
  FROM [yourTable]

Since that will be nothing more than a 4-digit number, you can use it as an integer wherever you go.

3 Comments

There is no year function in pg.
Ah. I saw SQL tag and went with that.
year() has nothing to do with SQL. It's a SQL Server flub, one of the very many ways it breaks the spec.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.