Q
Problem solve Get help with specific problems with your technologies, process and projects.

# How do I put days of the week in order...Monday, Tuesday, Wednesday, etc?

How do I put days of the week in order...Monday, Tuesday, Wednesday, etc.?

If you want to order by the day of the week, then just order by column which contains the date. For testing purposes, I've set up a little test table which contains only one column (DAY). Below is a query which shows the contents of that column:

```   SQL> select day,to_char(day,'DAY') from test;

DAY       TO_CHAR(D
--------- ---------
01-MAY-01 TUESDAY
02-MAY-01 WEDNESDAY
03-MAY-01 THURSDAY
04-MAY-01 FRIDAY
05-MAY-01 SATURDAY
06-MAY-01 SUNDAY
07-MAY-01 MONDAY
08-MAY-01 TUESDAY
09-MAY-01 WEDNESDAY
10-MAY-01 THURSDAY```

Now, they were inserted into this table in sorted order. So let's see how to put them in *reverse* order!

```   SQL> select day,to_char(day,'DAY') from test
2  order by day desc;

DAY       TO_CHAR(D
--------- ---------
10-MAY-01 THURSDAY
09-MAY-01 WEDNESDAY
08-MAY-01 TUESDAY
07-MAY-01 MONDAY
06-MAY-01 SUNDAY
05-MAY-01 SATURDAY
04-MAY-01 FRIDAY
03-MAY-01 THURSDAY
02-MAY-01 WEDNESDAY
01-MAY-01 TUESDAY```

Notice that the days of the week go into reverse order now! I've just used the ORDER BY clause on the column of DATE datatype.

If you want to group all of the "Tuesdays" together, then you'll have to order by the TO_CHAR column as can be seen in the following example:

```   SQL> select day,to_char(day,'DAY') from test
2  order by 2;

DAY       TO_CHAR(D
--------- ---------
04-MAY-01 FRIDAY
07-MAY-01 MONDAY
05-MAY-01 SATURDAY
06-MAY-01 SUNDAY
03-MAY-01 THURSDAY
10-MAY-01 THURSDAY
01-MAY-01 TUESDAY
08-MAY-01 TUESDAY
02-MAY-01 WEDNESDAY
09-MAY-01 WEDNESDAY```

Notice that all of the Wednesday's are together, etc. But if you want this sorted further so that all of the Monday's fall before the Tuesday's, then you'll need one more trick. You'll have to use the TO_CHAR function again, but this time, get the day of the week as a number. For instance, see below:

```   SQL> select day,to_char(day,'DAY'),to_char(day,'D') from test
2  order by to_char(day,'D');

DAY       TO_CHAR(D T
--------- --------- -
06-MAY-01 SUNDAY    1
07-MAY-01 MONDAY    2
01-MAY-01 TUESDAY   3
08-MAY-01 TUESDAY   3
02-MAY-01 WEDNESDAY 4
09-MAY-01 WEDNESDAY 4
03-MAY-01 THURSDAY  5
10-MAY-01 THURSDAY  5
04-MAY-01 FRIDAY    6
05-MAY-01 SATURDAY  7```

Notice now that all of the Tuesday's fall before the Wednesday's, etc.

I wasn't sure which way you wanted to sort your data so that is why I gave you three examples. Hopefully, one of these fit the bill!!

#### Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Where InfluxDB time series database is going

Users need more than SQL for querying databases, according to Paul Dix, co-founder and CTO of InfluxData. That's why the vendor ...

• ### MariaDB X4 brings smart transactions to open source database

Open source database vendor MariaDB updates its flagship platform with new features to enable the convergence of transactional ...

• ### How data lineage tools boost data governance policies

Organizations can bolster data governance efforts by tracking the lineage of data in their systems. Get advice on how to do so ...

## SearchBusinessAnalytics

• ### Storytelling using data makes information easy to digest

In a Q&A, Nate Nichols and Anna Schena Walsh of AI-based analytics vendor Narrative Science talk about how data storytelling can ...

• ### Data-driven storytelling makes data accessible

In a Q&A, Nate Nichols and Anna Schena Walsh of data storytelling vendor Narrative Science discuss their book on storytelling and...

• ### Startup Uplevel targets software engineering efficiency

Startup vendor Uplevel emerged from stealth on Wednesday touting a platform aimed at improving the efficiency of software ...

## SearchSAP

• ### SAP S/4HANA migration: Critical advice for moving off ECC

With the end of SAP ECC support looming in 2025, organizations must make some tough decisions. Here's a look at your choices.

• ### New SAP leadership faces big challenges in 2020

Industry analysts discuss SAP's biggest issues in 2020, including how the two new CEOs will guide the company deeper into the ...

• ### SAP Data Hub opens predictive possibilities at Paul Hartmann

When medical supply firm Paul Hartmann AG tested a supply chain analysis system built on SAP Data Hub, it found that it could ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Don't ever put a non-Java LTS release into production

Development teams should avoid non-long-term support releases at all costs. Pay attention to the Java release cycle to make sure ...

• ### Public API strategy considerations for enterprise adoption

As organizations look for more cost-effective ways to manage data, an evolving landscape with APIs has made the technology more ...

• ### Ideas on how to hold a successful code hackathon

Want to host a hackathon? Here are some ideas on what a company can do to host an event that solves problems and reenergizes the ...

## SearchDataCenter

• ### Organizations try to predict the effect of 5G infrastructure

With more 5G and IoT devices emerging, admins must prepare their data centers to support low-latency apps and edge computing with...

• ### Top infrastructure and operations technology myths of 2019

Admins are consistently evaluating technology to improve I&O efficiency. Cost, integration and business goals are key components ...

• ### Improve efficiency with server energy consumption tools

Deciding what servers are most efficient for your infrastructure requires research. Hardware-level certifications and web-based ...

## SearchContentManagement

• ### 4 popular content collaboration platforms to consider

Companies need to be organized if they want to be efficient. Content collaboration platforms are useful, but first, ensure that ...

• ### AI can enhance content security with a bit of planning

Microsoft and Box both use AI technologies to keep content secure in the cloud. But before using such tools, businesses first ...

• ### Ex-SAP exec steers Episerver CMS toward digital experience market

Alex Atzberger discusses leaving the helm of SAP's CX platform to become Episerver CEO. Now, Episerver looks to reinvent itself ...

## SearchHRSoftware

• ### Why mobile recruiting is the future

Recruiters can use text recruiting to connect with great candidates. Here's a look at how mobile recruiting works, why it's ...

• ### Top 7 HR trends for 2020 and beyond: The change decade

Because HR requires a balance between the needs of the organization with the needs of employees, it's always been a balancing act...

• ### For insider threat programs, HR should provide checks and balances

Insider threat programs may backfire if employees feel they are intrusive and violate privacy, Forrester Research warns. Making ...

Close