TheCommunicator
asked on
Dates are sorted in wrong manner while trying to wirk with Order by on dateTime field
Hello guys,
I am putting an order by clause on a datetime field and when I try to do this t sorts the date in a way where all the am/Pm are side by side.
I mean date are not sorted the way they are but they are ordered like following
Thanks
I am putting an order by clause on a datetime field and when I try to do this t sorts the date in a way where all the am/Pm are side by side.
I mean date are not sorted the way they are but they are ordered like following
May 3 2011 12:00AM 0 CF1
May 2 2011 12:00PM 0 CF1
May 2 2011 12:00AM 0 CF1
May 2 2011 11:00PM 0 CF1
May 2 2011 11:00AM 2 CF1
May 2 2011 10:00PM 0 CF1
May 2 2011 10:00AM 0 CF1
May 2 2011 9:00PM 0 CF1
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Is the data type of the column is datetime?
... also can you post the exact ORDER BY clause you're using?
... also can you post the exact ORDER BY clause you're using?
order by convert(varchar, getdate(), 109)
PatelAlpesh,
>>order by convert(varchar, getdate(), 109) <<
Just out of curiousity do you actually understand what you write?
>>order by convert(varchar, getdate(), 109) <<
Just out of curiousity do you actually understand what you write?
ASKER
Hi Guys,
I am so sorry for all the Hoopla. data was actually travelling from original table to a temp table and getting sorted and all. Temptable had been defined to have a varchar() column there and that was the reason of this not getting sorted properly. Once I changed the data type from varchar to date time, it started working great.
BUT
The fact that I was using Convert(datetime, mycolumnname) while doing selection, it should actually convert it into the dateTime first and then sort it.
I am so sorry for all the Hoopla. data was actually travelling from original table to a temp table and getting sorted and all. Temptable had been defined to have a varchar() column there and that was the reason of this not getting sorted properly. Once I changed the data type from varchar to date time, it started working great.
BUT
The fact that I was using Convert(datetime, mycolumnname) while doing selection, it should actually convert it into the dateTime first and then sort it.
HOW is converting at selection time different than declaring column as datetime?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ohhhh i got it. Thanks. Yeah I just missed that comment. Thanks.
ASKER
Open in new window