pookie
pookie

Reputation: 4142

Convert year and month variables to timestamp in Postgresql

I have to variables: year and month, both of type integer.

E.g:

year = 2016 month = 1

I want to, in my select statement, return a timestamp given those two variables.

I've had a look at the documentation, specifically to_timestamp and to_date, but all the examples I've come across show a string being converted into a timestamp.

I do not really want to convert my year and month into a string, such as:

to_timestamp(to_char(int,year) + ' ' + to_char(int, month),YYYY/MM/DD HH24:MI:SS)

So, how can I (if it is possible) convert my year and month into a timestamp?

Upvotes: 0

Views: 377

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521053

If you are using Postgres 9.4 or later, you could try using make_timestamp():

make_timestamp(2016, 1, 1, 0, 0, 0.0)

This would create a timestamp for January 1, 2016 at midnight. We need to specify values for the other components, even if they end up not being relevant to your query/calculation (e.g. you only need the date).

Upvotes: 2

Related Questions