27

If I write

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

Then it sorts the result based on Day like; from Friday, then Monday and last Wednesday, like sorting by characters.

But I want to sort it by day of the week; from Monday to Sunday.

0

17 Answers 17

16
SELECT
     *
FROM
     classes
ORDER BY 
     CASE
          WHEN Day = 'Sunday' THEN 1
          WHEN Day = 'Monday' THEN 2
          WHEN Day = 'Tuesday' THEN 3
          WHEN Day = 'Wednesday' THEN 4
          WHEN Day = 'Thursday' THEN 5
          WHEN Day = 'Friday' THEN 6
          WHEN Day = 'Saturday' THEN 7
     END ASC

Assuming that user has a table called classes in that table user has class_id (primary key), class name, Day.

14

You're getting it in the order you are because you're ordering by a string (and this wouldn't work because you're not selecting from anything).

You could order by the format model used to create the day of the week in numeric form, D, but as Sunday is 1 in this I would recommend using mod() to make this work.

i.e. assuming the table

create table a ( b date );

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

This would work:

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)

Here's a SQL Fiddle to demonstrate.

In your case your query would become:

select ename, to_char(hiredate,'fmDay') as "Day" 
  from my_table
 order by mod(to_char(hiredate, 'D') + 5, 7)
2
  • 4
    The value returned by the D format mask is affected by the session's NLS_TERRITORY settings. Run this with a UK territory and you'll get Tuesday first May 10, 2019 at 8:50
  • You can use TO_CHAR(b, 'ID') instead so that it returns the ISO 8601 day of the week with Monday (1) and Sunday (7)
    – codeananda
    Jun 17, 2020 at 10:09
10

Take a look at other formats for TO_CHAR. Instead of 'fmDay' use 'D' and it will give you the day of the week from 1 to 7. Then you can easily sort on it.

Here's a list of date formats: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm

2
  • 1
    this is starting day with sunday. Jan 14, 2018 at 11:17
  • 2
    The NLS_TERRITORY settings affect which day is 1; for America this is Sunday, most other countries this is Monday. So you can't rely on this May 16, 2019 at 14:25
10

Why to complicate when you can add another column with numbers 1-7 corresponding to days and then sort by this column...

1
  • 3
    Depending on country 1 day of week could be Monday/Sunday May 10, 2019 at 19:52
9

I just encountered the same requirement -- to order a query result by day of the week, but not starting with Sunday. I used the following query in Oracle to start w/ Monday. (Modify it to start the ordering w/ any day of the week, e.g., change 'MONDAY' to 'TUESDAY'.)

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

Or:

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

The D format mask of to_char maps days of the week to the values 1-7.

But!

The output of this depends on the client's setting for NLS_TERRITORY. The US considers Sunday to be day 1. Whereas most of the rest of the world consider Monday to be the start:

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

Sadly, unlike many other NLS parameters, you can't pass NLS_TERRITORY as the third parameter of 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

So any solution relying on D for sorting is a bug!

To avoid this, subtract the most recent Monday from the date (if today is Monday, the most recent Monday = today). You can do this with the IW format mask. Which returns the start of the ISO week. Which is always a Monday:

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 

For Sunday-Saturday sorting, add one to the date before finding the start of the ISO week:

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 + 1, 'iw' ) ) day_number
from   dts
order  by day_number;

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

If you want Monday to be always treated as first day of week you could use:

-- 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

It's simple.

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') puts a 'Monday' into a box named 'Sunday'.

5

As it's said, there's a function for it:

SELECT *
FROM table
ORDER BY WEEKDAY(table.date);
2
  • 4
    no, there is not: SELECT weekday(sysdate) FROM dual; ORA-00904: "WEEKDAY": invalid identifier
    – Rusty
    May 14, 2019 at 13:51
  • 1
    the function name, of course, may differ for each database vendor but i suppose each should have date/time manipulation functions which can achieve the same thing. as for oracle, consider the TO_CHAR function, e.g. SELECT TO_CHAR(sysdate, 'DAY') FROM dual; May 14, 2019 at 17:22
3

Might be late to post the answer, But seems simple solution to sort by day of week starting from Monday.

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

Answer for why added 5 ?

Because , In numbers Sunday has value 1, Monday has 2 ... and Saturday has 7

So, 2 + 5 = 7 and mod(%) with 7 will give us new values start from Monday.

You can change this number to start with any other day of week.

2nd option 

simply use WEEKDAY() function return weekday number for a given date.
**Note:**
0 = monday
1 = tuesday
2 = wednesday
3 = thursday
4 = friday
5 = saturday
6 = sunday

select hiredate,dayname(hiredate) from emp order by WEEKDAY(hiredate);
1

I improved on Ben's answer by giving you a result which starts from 1 rather than 0. The query would be like this:

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);

On the other hand, if you want your week to start from Sunday, then you should use this query:

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)

Hope this helps :)

1
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. 
1

I have some simple idea hope you like it. i don't which sql you are using so please correct syntax error.

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;

I you want to start next week after end a week then just find quarter of month and add in order by clouse.

just for in of find Quarter in(MSSQL): select DatePart(QUARTER, cast(cast(mydate as char(8)) as date))

1

This should do the trick in 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;

Of course if you want different order you just switch the values after then.

1

For Postgres at least, to have Monday always be day 1, use TO_CHAR(b, 'ID') to return the ISO 8601 day of the week - Monday (1) to Sunday (7).

This is the International Standard's Organization (ISO) Day of the Week hence the I in front of the D in 'ID'.

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

0

I think this is too late, but I will leave this here if anyone else needs it.

You can just order by day of the week, but it depends on the territory of your session.

In order to show the territory, you can simply execute this query:

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

NLS_TERRITORY specifies the name of the territory whose conventions are to be followed for day and week numbering.

Back to the previous query, if you have AMERICA as the value, then Monday has the value 2.

To order your results from Monday to Sunday, you have at least 2 roads:

1- Decrease one day from hire_date:

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

2- Alter the territory to a value that has 1 as the value of Monday, for example, FRANCE:

ALTER SESSION SET NLS_TERRITORY = 'FRANCE';

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

This is my solution.

select ename, to_char(hiredate,'fmDay') "Day"
from employees
order by DECODE(to_number(to_char(hiredate ,'D'))-1,
                                                0, 7,
                to_number(to_char(hiredate ,'D'))-1);

Basically converts day to a number by using 'D' format from to_char function.

to_number(to_char(hire_date ,'D')

But that causes Sunday to be 1 and Monday to be 2 and so on so I subtract 1 to make Monday 1.

(to_number(to_char(hire_date ,'D') - 1

Then I use DECODE function to basically say if 0, then 7, else original number. That just makes Sunday be 7 instead of 0 and everything else is in its proper place(Monday is 1, Tuesday is 2...so on).

DECODE(to_number(to_char(hire_date ,'D'))-1,
                                                0, 7,
                to_number(to_char(hire_date ,'D'))-1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.