How to Get Day Names in PostgreSQL
Database:
Operators:
Table of Contents
Problem
You want to extract the name of the day of the week from a date in PostgreSQL.
Solution 1
To extract the day name from the date, you can use the to_char() function. The first argument is the date and the second is the desired output format. To extract the full day name, the format should be 'Day':
SELECT to_char(date '2022-01-01', 'Day');
The result is Saturday.
Discussion
This is not the only format you can use to extract the day name. Here are some alternatives:
'DAY'would return an uppercase name (SATURDAY)'day'would return a lowercase name (saturday)'DY'would returnSAT'Dy'would returnSat'dy'would returnsat
You can also extract the day name in other languages. Two adjustments are required to do this:
- Setting new locale for times and dates using
SET lc_time; - Using the
'TMDay'format to enable translation based onlc_time.
Let’s look at an example.
Solution 2
Let’s extract the day name in German:
set lc_time = 'de_DE'; select to_char(date '2022-01-01', 'TMDay') ;
The result is Samstag.