0

I have a database as attached, in which i have a BirthDate column which is split into BirthYear, BirthMonth and BirthDayNumber.

Database Image

eg. For BirthDate:

Wednesday, May 10, 1899

BirthYear:1899
BirthMonth : 5
BirthDayNumber :10

I want to sort the database in the following format( the month and date should be in ascending order and in case if the month and date are equal then it should sort the year in descending order)

01 January 2011 
02 January 2012 
02 January 2008 
05 August 2012 
06 September 2001 
12 December 1899 

Can you please help me out with the query for displaying the list in this format.

2
  • BirthMonth ASC, BirthDay ASC or OrderYear DESC this is what you want? Commented Oct 4, 2012 at 6:27
  • I am using sqlite as I have to then connect this to an iphone application. In BirthMonth ASC, BirthDay ASC or OrderYear DESC i am getting it sorted by year. The year should be taken into consideration only if the month and date are equal. Commented Oct 4, 2012 at 6:40

6 Answers 6

1

Think this works

SQLFiddle

Using this code

SELECT birthdate FROM testdate 
ORDER BY 
BirthDayNumber ASC, BirthMonth ASC, BirthYear Desc
Sign up to request clarification or add additional context in comments.

Comments

0

Please try query given below

SELECT * FROM `tblename`  order by BirthYear DESC, BirthMonth ASC,BirthDayNumber ASC

thanks

1 Comment

I have tried this but I have to consider the year only if the day and month are equal. I this case I am getting: 02 January 2012 05 August 2012 01 January 2011 02 January 2008 06 September 2001 12 December 1899 But I require output like: 01 January 2011 02 January 2012 02 January 2008 05 August 2012 06 September 2001 12 December 1899
0

try this

SELECT * FROM TABLE ORDER BY BirthYear DESC, BirthDayNumber ASC, BirthMonth ASC

Comments

0

Try this

SELECT CONCAT(BirthDayNumber,' ', MonthName(CONCAT('1900-',CONVERT(BirthMonth,char(2)),'-1')), ' ', Birthyear)
FROM table
ORDER BY BirthMonth ASC, BirthDay ASC, BirthYear DESC

Comments

0
SELECT birthdate FROM testdate 
ORDER BY 
BirthMonth  ASC,BirthDayNumber  ASC, BirthYear Desc

Comments

0

Try something like that:

SELECT ROW_NUMBER() OVER (PARTITION BY DT ORDER BY YE DESC, MO ASC, DT ASC) AS NR, DT, MO, YE FROM SOMEDATA

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.