PostgreSQL : cast string to date DD/MM/YYYY

前端 未结 3 1718
小鲜肉
小鲜肉 2020-12-28 12:19

I\'m trying to cast a CHARACTER VARYING column to a DATE but I need a date format like this : DD/MM/YYYY. I use the following SQ

3条回答
  •  旧时难觅i
    2020-12-28 12:53

    A DATE column does not have a format. You cannot specify a format for it.

    You can use DateStyle to control how PostgreSQL emits dates, but it's global and a bit limited.

    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.

    regress=> SELECT to_char(DATE '2014-04-01', 'DD/MM/YYYY');
      to_char   
    ------------
     01/04/2014
    (1 row)
    

提交回复
热议问题