[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Problem with Sorting...

Posted on 2011-10-06
Medium Priority
Last Modified: 2012-05-12

i have following values in day column.

Each value in above column denotes F - Friday,  M-Monday, W-Wednesday, Th-Thursday, Sa-Saturday, Su-Sunday, Tu-Tuesday.

I want to sort those values by day... i.e)  M,Tu,W,Th,F.Sa,Su

Anyone please assist with me for this requirement...
Question by:gpmsqldev
LVL 32

Accepted Solution

Daniel Wilson earned 1500 total points
ID: 36924249
There are a few ways to handle this ... personally, I would have used an integer in the Day column.

But, given the data you have, I would add a DayLookup table with
Code varchar(2) -- put the codes you show me in here
Name varchar(8) -- put the full name here ... can be useful for displays in other cases
Sequence TinyInt -- put the 0-based sequence numbers here.  In your case, "M" matches up with 0.

Then to sort ...

Select Field1, ... Fieldn, Day
From MyTable Inner Join DayLookup on MyTable.Day = DayLookup.Code
order by DayLookup.Sequence
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36926065
You could also use a Computed Column to "translate" the code to a more useful tinyint...

Author Closing Comment

ID: 36928762
Its helps me to solve my problem... but i am not using this method . Anyway we have to use M as 1, and Tu as 2, and so on....

So i used CASE statement for this problem.. and finally sorted correctly..

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question