Click here to Skip to main content
15,927,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have a datagrid in my proj that has a date field.
the problem is that i want to show the date at run time as dd/mm/yyyy and want to sort the date by month in ascending order.
if i take date formate as mm/dd/yyyy then the month is sorted perfectly but if i take dd/mm/yyyy formate then it sorts by day ie dd instead of month .
plz help me with the Query.
Posted

Try:
SQL
SELECT * FROM myTable ORDER BY Year(myDateColumn), Month(myDateColumn)


Never try to sort only by the month: You need the year as well to be meaningfull.

[edit]Example given made no sense when I posted it... :doh: - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Vandana87 13-Apr-11 6:40am    
THANKYOU FOR YOUR GUIDENCE.
OriginalGriff 13-Apr-11 6:42am    
You are welcome - but please DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
SQL
SELECT tblBills.*,  MonthName(month(tblBills_1.Due_Date)) AS month1
FROM tblBills inner join tblBills AS tblBills_1
on tblBills.ID=tblBills_1.ID
order by tblBills_1.Due_Date


try This code ,it will work!!
 
Share this answer
 
Comments
Vandana87 13-Apr-11 6:40am    
THANKYOU FOR YOUR GUIDENCE.
Try this link

Click[^]
 
Share this answer
 
Comments
Vandana87 13-Apr-11 6:40am    
THANKYOU FOR YOUR GUIDENCE.
You can actually do a group by allowing you to do a sort by month.
See here[^] + this discussion here.
 
Share this answer
 
SELECT * FROM myTable ORDER BY MyDatecolumn asc
 
Share this answer
 
Why do you convert the date data into string before the sort? If you have the actual data in a date field, just use that in your ORDER BY clause:
... ORDER BY DateField

If you sort with partial date information the rows in the same month (or year unless you include that also) will be in 'random' order. I take it that is not what you want unless you have some other field you want to use for ordering within a month.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900