PostgreSQL : cast string to date DD/MM/YYYY

前端 未结 3 1729
小鲜肉
小鲜肉 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条回答
  •  情深已故
    2020-12-28 12:58

    The documentation says

    The output format of the date/time types can be set to one of the four styles ISO 8601, SQL (Ingres), traditional POSTGRES (Unix date format), or German. The default is the ISO format.

    So this particular format can be controlled with postgres date time output, eg:

    t=# select now();
                  now
    -------------------------------
     2017-11-29 09:15:25.348342+00
    (1 row)
    
    t=# set datestyle to DMY, SQL;
    SET
    t=# select now();
                  now
    -------------------------------
     29/11/2017 09:15:31.28477 UTC
    (1 row)
    
    t=# select now()::date;
        now
    ------------
     29/11/2017
    (1 row)
    

    Mind that as @Craig mentioned in his answer, changing datestyle will also (and in first turn) change the way postgres parses date.

提交回复
热议问题