如果我写
select ename, to_char(hiredate,'fmDay') as "Day" order by "Day";
然åŽæ ¹æ®Day对结果进行排åº;从星期五,星期一到上周三, 喜欢按å—符排åºã€‚
但是我想在一周之å‰å¯¹å®ƒè¿›è¡ŒæŽ’åº;从星期一到星期日。
ç”案 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');
ç”案 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。
ç”案 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 æ¡é“路:
hire_date
å‡å°‘一天:SELECT * FROM employees ORDER BY TO_CHAR(hire_date - 1, 'd');
FRANCE
:ALTER SESSION SET NLS_TERRITORY = 'FRANCE';
SELECT * FROM employees ORDER BY TO_CHAR(hire_date, 'd');