Reputation: 33
When I select time in SQL it is returned as 2011-02-25 21:17:33.933. But I need only the time part. How can I do this? I'm trying to: order by "Onlie Duration" DESc , but need it to be ordered by time
Upvotes: 0
Views: 2056
Reputation: 814
Use Convert function
SELECT * FROM MyTable
ORDER BY CONVERT(varchar(10), TimeField, 108)
UPD Or using DATEDIFF:
SELECT * FROM MyTable
ORDER BY DATEDIFF(MILLISECOND, CAST(TimeField AS DATE), TimeField)
Upvotes: 0
Reputation: 106
You can get only the time part using:
select convert(char,[YourDateColumn],108) from [YourTable]
Sorting your data based on time can be done by ordering using:
select * from [YourTable] order by convert(char,[yourDateColumn],108)
The downside to this is that, sorting with just the time when there are dates for different days will mix the time for the various days.
Upvotes: 0
Reputation: 5542
You can do something like this:
SELECT time_column FROM dbo.table
ORDER BY CONVERT(time, time_column)
Upvotes: 4