?
Solved

sort by date and time in SQL

Posted on 2013-10-30
3
Medium Priority
?
546 Views
Last Modified: 2013-10-30
Hi Expert
i have a varchar column in a table, and this column hold date and time value, i wrote a simple query to sort this column descending, its working but it not seem to sort the time, i tried to convert the field to datetime data type but still it ignoring the time

SELECT  AUDIT.CurrentActor, USR.EngName, AUDIT.ReceivedDate, AUDIT.CompletedDate, AUDIT.Status
FROM tbl_ServiceAudit AUDIT  LEFT OUTER  JOIN pr1.dbo.GetUserDetailsE USR ON USR.PERUserName = AUDIT.CurrentActor 
WHERE AUDIT.ReferenceNo =123291
GROUP BY AUDIT.CurrentActor, USR.EngName, AUDIT.ReceivedDate, AUDIT.CompletedDate, AUDIT.Status
ORDER BY
CAST(AUDIT.CompletedDate AS date)
DESC

Open in new window


any suggestion
0
Comment
Question by:AZZA-KHAMEES
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 2000 total points
ID: 39610878
You need to change your cast to:

CAST(AUDIT.CompletedDate AS datetime)

Open in new window

0
 

Author Closing Comment

by:AZZA-KHAMEES
ID: 39610883
thanks
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39610908
well, really, you should store datetime as datetime :)
less space and way less hassle (such as this one)
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

752 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