28

I have a PostgreSQL timestamp as

2009-12-22 11:01:46

I need to change this to date as

2009-12-22

So that I can compare the dates in postgreSQL

How can I achieve this transformation?

3 Answers 3

65

Cast it to date.

SELECT yourtimestamp::date;

If you need to extract other kinds of stuff, you might want to use EXTRACT or date_trunc

Both links are to the same page, were you'll find more date/time-related functions.

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

Comments

12

You can either use one of the Postgres date functions, such as date_trunc, or you could just cast it, like this:

SELECT timestamp '2009-12-22 11:01:46'::date

>>> 2009-12-22

1 Comment

date_trunc ('day', yourtimestamp) will return a timesamp, but with hours, mins and secs to zero. It will not convert the value to a date. The cast is the correct anwser.
0
SELECT [timestamp column] ::date;

Along in select query you can search on the basis of date

WHERE [timestamp column] :: date = '2022-05-30'

Comments

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.