使用Oracle SQL,你如何創建一個結果集爲:使用Oracle SQL,如何輸出星期幾和星期幾?
- 號(週一,週二,週三,等等)日的一週內(1-7)
- 一天的名稱
例如:
DAY NAME 1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday 6 Saturday 7 Sunday
使用Oracle SQL,你如何創建一個結果集爲:使用Oracle SQL,如何輸出星期幾和星期幾?
例如:
DAY NAME 1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday 6 Saturday 7 Sunday
弗洛林的答案是我該怎麼做,但你需要對NLS設置有點小心。一週的天是由NLS領土受到影響,所以如果我運行這個,如果我在美國是它的工作原理:
alter session set nls_territory = 'AMERICA';
select to_char(sysdate, 'D') as d, to_char(sysdate, 'Day') as day from dual;
D DAY
- ------------------------------------
6 Friday
select level as dow,
to_char(trunc(sysdate ,'D') + level, 'Day') as day
from dual
connect by level <= 7;
DOW DAY
--- ------------------------------------
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
但在英國運行的同一查詢是一個休息日:
alter session set nls_territory = 'UNITED KINGDOM';
select to_char(sysdate, 'D') as d, to_char(sysdate, 'Day') as day from dual;
D DAY
- ------------------------------------
5 Friday
select level as dow,
to_char(trunc(sysdate ,'D') + level, 'Day') as day
from dual
connect by level <= 7;
DOW DAY
--- ------------------------------------
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday
7 Monday
...我需要調整計算更正爲:
select level as dow,
to_char(trunc(sysdate ,'D') + level - 1, 'Day') as day
from dual
connect by level <= 7;
DOW DAY
--- ------------------------------------
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
你也可以分別指定使用的日期名稱的語言,如果你想:
select level as dow,
to_char(trunc(sysdate ,'day') + level - 1, 'Day',
'NLS_DATE_LANGUAGE=FRENCH') as day
from dual
connect by level <= 7;
DOW DAY
--- --------------------------------
1 Lundi
2 Mardi
3 Mercredi
4 Jeudi
5 Vendredi
6 Samedi
7 Dimanche
to_char()
with nls_date_language
和day of the week的文檔,以及更多的globalisation support guide。
Select 1, 'Monday' from dual union all select 2, 'Tuesday' from dual ...
感謝您的回覆。有沒有使用UNION的另一種方法。 –
select level as dow,
to_char(level+trunc(sysdate,'D'),'Day') as day
from dual
connect by level <= 7;
這原本是一個措辭很差的問題。然而,它已被編輯和澄清,現在是一個非常有用的問題。本頁上的答案對我來說簡化了(最初)困難的Oracle問題很有幫助。這些答案在其他地方不可用。這個問題值得重新評估,並可能重新開放。 –
現在這是一個真正的問題。 –