按星期一到星期日的星期几排åº

时间:2012-12-01 18:36:23

标签: sql oracle sql-order-by

如果我写

select ename, to_char(hiredate,'fmDay') as "Day" order by "Day";

然åŽæ ¹æ®Day对结果进行排åº;从星期五,星期一到上周三, 喜欢按字符排åºã€‚

但是我想在一周之å‰å¯¹å®ƒè¿›è¡ŒæŽ’åº;从星期一到星期日。

16 个答案:

答案 0 :(得分:11)

你按照你的顺åºå¾—到它,因为你是用字符串排åºçš„(这ä¸ä¼šèµ·ä½œç”¨ï¼Œå› ä¸ºä½ æ²¡æœ‰é€‰æ‹©ä»»ä½•ä¸œè¥¿ï¼‰ã€‚

您å¯ä»¥æŒ‰format model订购,以数字形å¼D创建星期几,但由于星期日为1,我建议您使用mod()进行此æ“作工作

å³ã€‚å‡è®¾è¡¨

create table a ( b date );

insert into a
 select sysdate - level
  from dual
connect by level <= 7;

这样å¯è¡Œï¼š

select mod(to_char(b, 'D') + 5, 7) as dd, to_char(b, 'DAY')
  from a
 order by mod(to_char(b, 'D') + 5, 7)

这是一个SQL Fiddleæ¥æ¼”示。

在您的情况下,您的查询将å˜ä¸ºï¼š

select ename, to_char(hiredate,'fmDay') as "Day" 
  from my_table
 order by mod(to_char(hiredate, 'D') + 5, 7)

答案 1 :(得分:9)

查看TO_CHAR的其他格å¼ã€‚而ä¸æ˜¯'fmDay&#39;使用&#39; D&#39;并且它将为您æ供从1到7的星期几。然åŽæ‚¨å¯ä»¥è½»æ¾åœ°å¯¹å…¶è¿›è¡ŒæŽ’åºã€‚

以下是日期格å¼åˆ—表:http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm

答案 2 :(得分:7)

我刚刚é‡åˆ°äº†åŒæ ·çš„è¦æ±‚ - 按星期几订购查询结果,但ä¸æ˜¯ä»¥æ˜ŸæœŸæ—¥å¼€å§‹ã€‚我在Oracle中使用以下查询æ¥å¯åŠ¨w / Monday。 (修改它以开始订购w /一周中的任何一天,例如,将'MONDAY'更改为'TUESDAY'。)

SELECT ename, to_char(hiredate, 'fmDAY') AS "Day" 
FROM emp
ORDER BY (next_day(hiredate, 'MONDAY') - hiredate) DESC

或者:

SELECT ename, to_char(hiredate, 'fmDAY') AS "Day"
FROM emp
ORDER BY (hiredate - next_day(hiredate, 'MONDAY'))

答案 3 :(得分:7)

Dçš„{​​{1}}æ ¼å¼æŽ©ç å°†æ˜ŸæœŸå‡ æ˜ å°„到值1-7。

但是ï¼

此输出å–决于客户端的NLS_TERRITORY设置。美国认为星期日是第一天。而世界上大多数其他国家都认为星期一是开始:

to_char

é—憾的是,与许多其他NLSå‚æ•°ä¸åŒï¼Œæ‚¨ä¸èƒ½å°†NLS_TERRITORY用作alter session set nls_territory = AMERICA; with dts as ( select date'2018-01-01' + level - 1 dt from dual connect by level <= 7 ) select to_char ( dt, 'Day' ) day_name, to_char ( dt, 'd' ) day_number from dts order by day_number; DAY_NAME DAY_NUMBER Sunday 1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday 6 Saturday 7 alter session set nls_territory = "UNITED KINGDOM"; with dts as ( select date'2018-01-01' + level - 1 dt from dual connect by level <= 7 ) select to_char ( dt, 'Day' ) day_name, to_char ( dt, 'd' ) day_number from dts order by day_number; DAY_NAME DAY_NUMBER Monday 1 Tuesday 2 Wednesday 3 Thursday 4 Friday 5 Saturday 6 Sunday 7 的第三个å‚数:

to_char

因此,任何ä¾èµ–with dts as ( select date'2018-01-01' dt from dual ) select to_char ( dt, 'Day', 'NLS_DATE_LANGUAGE = SPANISH' ) day_name from dts; DAY_NAME Lunes with dts as ( select date'2018-01-01' dt from dual ) select to_char ( dt, 'Day', 'NLS_TERRITORY = AMERICA' ) day_name from dts; ORA-12702: invalid NLS parameter string used in SQL function 进行排åºçš„解决方案都是一个错误ï¼

为é¿å…è¿™ç§æƒ…况,请从日期中å‡åŽ»æœ€è¿‘的星期一(如果今天是星期一,则最近的星期一=今天)。您å¯ä»¥ä½¿ç”¨Dæ ¼å¼çš„掩ç æ¥æ‰§è¡Œæ­¤æ“作。返回ISO周的开始。始终是星期一:

IW

对于周日至周六的排åºï¼Œè¯·åœ¨æ—¥æœŸä¹‹å‰æ·»åŠ ä¸€ä¸ªï¼Œç„¶åŽå†æ‰¾åˆ°ISO周的开始:

with dts as (
  select date'2018-01-01' + level - 1 dt 
  from   dual
  connect by level <= 7
)
select to_char ( dt, 'Day' ) day_name,
       ( dt - trunc ( dt, 'iw' ) ) day_number
from   dts
order  by day_number;

DAY_NAME    DAY_NUMBER   
Monday                   0 
Tuesday                  1 
Wednesday                2 
Thursday                 3 
Friday                   4 
Saturday                 5 
Sunday                   6 

答案 4 :(得分:6)

为什么在添加å¦ä¸€åˆ—æ•°å­—1-7对应于天数然åŽæŒ‰æ­¤åˆ—排åºæ—¶ä¼šå˜å¾—å¤æ‚...

答案 5 :(得分:6)

如果您希望星期一始终被视为一周的第一天,则å¯ä»¥ä½¿ç”¨ï¼š

-- Not affected by NLS_TERRITORY
-- ALTER SESSION SET NLS_TERRITORY="AMERICA";  -- Sunday is first day of week
-- ALTER SESSION SET NLS_TERRITORY="GERMANY";  -- Monday is first day of week

SELECT *
FROM tab
ORDER BY 1+TRUNC(dt)-TRUNC(dt,'IW');

db<>fiddle demo

答案 6 :(得分:5)

<welcome-file-list>
    <welcome-file>
        index2.html
    </welcome-file>        
</welcome-file-list>

å‡è®¾ç”¨æˆ·åœ¨è¯¥è¡¨ä¸­æœ‰ä¸€ä¸ªå为classes的表,则用户具有class_id(主键),类å,Day。

答案 7 :(得分:5)

很简å•ã€‚

SELECT last_name, hire_date,TO_CHAR(hire_date, 'DAY') DAY
FROM employees
ORDER BY TO_CHAR(hire_date - 1, 'd');

TO_CHAR(hire_date - 1, 'd')将“星期一â€æ”¾å…¥å为“星期日â€çš„框中。

答案 8 :(得分:4)

正如所说的,有一个功能:

SELECT *
FROM table
ORDER BY WEEKDAY(table.date);

答案 9 :(得分:1)

这应该å¯ä»¥è§£å†³sql中的问题:

ORDER BY 
     CASE DATENAME(dw,<<enter your date variable here>>)
          WHEN 'Monday' THEN 1
          WHEN 'Tuesday' THEN 2
          WHEN 'Wednesday' THEN 3
          WHEN 'Thursday' THEN 4 
          WHEN 'Friday' THEN 5 
          WHEN 'Saturday' THEN 6
          WHEN 'Sunday' Then 7
     END ASC;

当然,如果您希望使用其他顺åºï¼Œåˆ™åªéœ€åœ¨æ­¤ä¹‹åŽåˆ‡æ¢å€¼å³å¯ã€‚

答案 10 :(得分:1)

至少对于Postgres,è¦ä½¿æ˜ŸæœŸä¸€å§‹ç»ˆä¸ºç¬¬ä¸€å¤©ï¼Œè¯·ä½¿ç”¨TO_CHAR(b, 'ID')å°†ISO 8601的星期几从星期一(1)返回到星期日(7)。

这是国际标准组织(ISO)的星期几,因此“ IDâ€ä¸­Då‰é¢çš„I。

https://www.postgresqltutorial.com/postgresql-to_char/

答案 11 :(得分:1)

å‘布答案å¯èƒ½ä¼šè¿Ÿåˆ°ï¼Œä½†ä»Žæ˜ŸæœŸä¸€å¼€å§‹æŒ‰æ˜ŸæœŸå‡ æŽ’åºä¼¼ä¹Žå¾ˆç®€å•ã€‚

select hiredate,dayname(hiredate) from emp order by (dayofweek(hiredate)+5)%7; 

为什么添加 5 的答案?

因为,在数字中,星期天的值为 1,星期一为 2 ...,星期六为 7

因此,2 + 5 = 7 å’Œ mod(%) 与 7 将从星期一开始为我们æ供新值。

您å¯ä»¥æ›´æ”¹æ­¤æ•°å­—以从一周中的任何一天开始。

答案 12 :(得分:0)

我通过给您一个从1而ä¸æ˜¯0开始的结果改进了Ben的答案。查询如下:

select 
    mod(to_char(b, 'D')+ 5, 7) +1 as dd, 
    to_char(b, 'DAY')
from a
order by mod(to_char(b, 'D')+ 5, 7);

å¦ä¸€æ–¹é¢ï¼Œå¦‚果希望一周从星期日开始,则应使用以下查询:

select 
    mod(to_char(b, 'D')+ 6, 7) +1 as dd, 
    to_char(b, 'DAY')
from a
order by mod(to_char(b, 'D')+ 6, 7)

希望这会有所帮助:)

答案 13 :(得分:0)

with s as (select trunc(sysdate) + level dt from dual connect by level <= 7)
select to_char(dt, 'fmDay', 'nls_date_language=English') d
from s
order by dt - trunc(dt, 'iw');

D                                   
------------------------------------
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

7 rows selected. 

答案 14 :(得分:0)

我有一个简å•çš„主æ„,希望您喜欢。我ä¸ä½¿ç”¨å“ªä¸ªsql,请更正语法错误。

select ename, to_char(hiredate,'fmDay') as "Day" from ABC_TABLE
JOIN (VALUES (1,'Monday'),(2,'Tuesday'),(3,'Wednesday'),(4,'Thursday'),(5,'Friday'),(6,'Saturday'),(7,'Sunday')) weekdays(seq,[Days]) on
ABC_TABLE.to_char(hiredate,'fmDay') = weekdays.[Days]
order by weekdays.seq;

我想在一周结æŸåŽä¸‹å‘¨å¼€å§‹ï¼Œç„¶åŽæ‰¾å‡ºä¸€ä¸ªæœˆçš„季度并按clouse顺åºæ·»åŠ ã€‚

仅用于查找四分之一英寸(MSSQL):select DatePart(QUARTER, cast(cast(mydate as char(8)) as date))

答案 15 :(得分:0)

我觉得这太晚了,但如果其他人需è¦å®ƒï¼Œæˆ‘会把它留在这里。

您å¯ä»¥åªæŒ‰æ˜ŸæœŸå‡ è®¢è´­ï¼Œä½†è¿™å–决于您的会è¯èŒƒå›´ã€‚

为了显示领土,你å¯ä»¥ç®€å•åœ°æ‰§è¡Œè¿™ä¸ªæŸ¥è¯¢ï¼š

SELECT * FROM V$NLS_PARAMETERS WHERE parameter = 'NLS_TERRITORY';

NLS_TERRITORY 指定è¦éµå¾ªå…¶æ—¥å’Œå‘¨ç¼–å·çº¦å®šçš„区域的å称。

回到上一个查询,如果你有 AMERICA 作为值,那么 Monday 的值为 2。

è¦ä»Žå‘¨ä¸€åˆ°å‘¨æ—¥å¯¹ç»“果进行排åºï¼Œæ‚¨è‡³å°‘有 2 æ¡é“路:

1- 从 hire_date å‡å°‘一天:

SELECT * FROM employees ORDER BY TO_CHAR(hire_date - 1, 'd');

2- 将领土更改为星期一的值为 1 的值,例如 FRANCE:

ALTER SESSION SET NLS_TERRITORY = 'FRANCE';

SELECT * FROM employees ORDER BY TO_CHAR(hire_date, 'd');