21 Nov 2009 
Support Center » Knowledgebase » SQL report sort by a date element i.e. year, month, or day
 SQL report sort by a date element i.e. year, month, or day
Solution

Sometimes, a client may need to sort the result of a query by a date element (year, month, or day)
Engineeing provided an example which involved the use of the  DATEPART function

IMPORTANT - The DATEPART function can only deal with ONE date element at a time (year, month, or day)
  The default sort in an SQL query report is in ascending order , and there is no need to use the keyword ASC.  If ASC is used, it really does nothing.
  To have a result sorted in descending order, the keyword DESC should be affixed to the end of the SORT BY clause.

The use of the DATEFORMAT function as in SQL 2 below separates the time component from the date.

SQL 1 ... Use this SQL script to sort Positions Fill_Date by year:
select pno, fill_date from positions order by datepart(year,fill_date)

SQL 2. ...  Use this query to sort by year and to separate the time component from the date: select pno, date(fill_date)"Date Filled" from positions where fill_date is not null order by date(fill_date)



Article Details
Article ID: 2
Created On: 14 Feb 2007 03:02 PM

 This answer was helpful  This answer was not helpful

 Back
 Login [Lost Password] 
Email:
Password:
Remember Me:
 
 Search
 Article Options
Home | Register | Submit a Ticket | Knowledgebase | Troubleshooter | News | Downloads
Language:

Help Desk Software By Kayako SupportSuite v3.50.05