Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date in mmm yyyy format in postgresql

Tags:

sql

postgresql

I have a table with a column of type timestamp without time zone.

I want to select that column with the mmm yyyy format – for example, “Mar 2011”. How to format it that way? I tried:

select cast(now() as date) 

but it is giving me the incorrect format.

like image 347
Deepak Kumar Avatar asked Sep 14 '11 10:09

Deepak Kumar


People also ask

How do I format date in PostgreSQL?

Instead, you should use to_char to format the date when you query it, or format it in the client application. Like: SELECT to_char("date", 'DD/MM/YYYY') FROM mytable; e.g.

How do I change the date format from YYYY-MM-DD in PostgreSQL?

1) Get the current date To get the current date and time, you use the built-in NOW() function. However, to get the date part only (without the time part), you use the double colons (::) to cast a DATETIME value to a DATE value. The result is in the format: yyyy-mm-dd .

How do I change the default date format in PostgreSQL?

PostgreSQL uses the yyyy-mm-dd format for storing and inserting date values. If you create a table that has a DATE column and you want to use the current date as the default value for the column, you can use the CURRENT_DATE after the DEFAULT keyword.


2 Answers

SELECT TO_CHAR(NOW(), 'Mon YYYY');

like image 100
Mohamed Saligh Avatar answered Sep 19 '22 17:09

Mohamed Saligh


DateAndTime Reformat:

SELECT *, to_char( last_update, 'DD-MON-YYYY') as re_format from actor; 

DEMO:

enter image description here

like image 42
Ram Pukar Avatar answered Sep 19 '22 17:09

Ram Pukar