SQL 在 Postgresql 中获取星期几
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9441734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get days of week in Postgresql
提问by Reena
Had been trying to figure out a select query to display days of week such as Monday, Tuesday, Wednesday, etc. I ended up with a select query displaying dates of a week,
一直试图找出一个选择查询来显示星期几,例如星期一、星期二、星期三等。我最终得到了一个显示一周日期的选择查询,
select ((date_trunc('week',current_date)::date) + (i+6)) as week_date
from generate_Series(0,6) i;
Is there a way to get the result as Monday, Tuesday and so on.
有没有办法得到周一、周二等的结果。
采纳答案by Eelke
You could do just:
你可以这样做:
VALUES ('Sunday'), ('Monday'), ('Tuesday'), ('Wednesday'), ('Thursday'), ('Friday'), ('Saturday');
回答by
Just use extract:
只需使用提取物:
extract(dow from date_column::timestamp)
from whatever_table;
This returns 0 for Sunday, 1 for Monday,...,6 for Saturday.
这将在星期日返回 0,星期一返回 1,...,星期六返回 6。
Edit:Or, since you apparently don't need anything that actually requires grabbing the day of week from a given date and you just want a single column with the fixed string values representing the names of the days of the week, just create a table...
编辑:或者,由于您显然不需要任何实际上需要从给定日期获取星期几的东西,而您只需要一个带有代表星期几名称的固定字符串值的单列,只需创建一个表...
回答by dannio
In Postgres 9 onwards there's:
在 Postgres 9 以后有:
to_char(date, 'dy');
to_char(date, 'dy');
which would return your date into the day of the week as a text value
这会将您的日期作为文本值返回到星期几
回答by Reena
The following query also works
以下查询也有效
select to_char((date_trunc('week',current_date)::date) + i,'Day') as wkday from generate_series(0,6) i
回答by Kevin Nguyen
creates a single field array that you unnest
创建一个您取消嵌套的单个字段数组
select day_of_week
from unnest(
array
['Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday']
) as day_of_week