what is mysql date_format() equivalent in Presto or Postgres?

Viewed 291

From datetime 2018-12-12 10:10:10 to get only year-month I would usually do this in MySQL8.0

date_format('2018-12-12 10:10:10', '%Y-%m')

which outputs 2018-12

How can I do this in Presto or Postgres?

I can do it with

concat(cast(year('2018-12-12 10:10:10') as varchar),'-',cast(month('2018-12-12 10:10:10') as varchar)) as year_month

but seems very cumbersome.

2 Answers

On Postgres, we can use TO_CHAR here:

SELECT TO_CHAR('2018-12-12 10:10:10'::timestamp, 'YYYY-MM') AS dt;
-- outputs 2018-12

In presto

date_format(date, '%Y-%m') as year_month

seems to work.

Related