Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

date - How to convert 'dd-mm-yyyy' to 'dd-mmm-yyyy' in PostgreSQL?

I want to convert data format 'DD-MM-YYYY' (e.g., '31-03-2020' ) to 'DD-MMM-YYYY' (e.g., '31-MAR-2020'). Can someone please help me out here?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You'll need to first convert the non-standard date (31-03-2020, which is actually a text or varchar type, in Postgres terms), to a DATE type with to_date(), then convert it to the format you're looking for (which again, is actually a text or varchar type):


postgres=# select to_char(to_date('31-03-2020','DD-MM-YYYY'), 'DD-Mon-YYYY');
   to_char   
-------------
 31-Mar-2020
(1 row)

Or to make it a little more human-readable:

postgres=# WITH dt AS (SELECT to_date('31-03-2020','DD-MM-YYYY') mydate)
SELECT to_char(dt.mydate, 'DD-Mon-YYYY') FROM dt;
   to_char   
-------------
 31-Mar-2020
(1 row)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...