Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

sort by date and time in SQL

Posted on 2013-10-30
3
Medium Priority
?
557 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
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

810 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