sqlpostgresqldate-arithmetic

How to get day name from weekday number in postgresql?


I have a table with the weekday stored as an integer, 0 to 6.

What function do I need to create the day names relative to these day numbers?

Eg: 0 -> Sun, 1->Mon, etc

Like:

SELECT magic(my_day_number) FROM my_table;
Mon

Solution

  • A simple and efficient method uses case instead of date manipulation routines:

    select case my_day_number
        when 0 then 'Sun'
        when 1 then 'Mon'
        when 2 then 'Tue'
        ...
        when 6 then 'Sat'
    end my_day_name from mytable