Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Time Value (only) from DateTime field

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-03-11 : 10:54:09
D. Smith writes "Gentlemen:

I have been pounding my head against the wall for days trying to figure out how to extract the Time value (only) from a datetime field while maintaining it as a datetime data type.

I know that I can convert it to a string, parse the string, and so on, which I have done effectively, but this converts it to a string data type which sorts alphabetically (i.e. 1:00 AM, 10:00 AM, 2:00 AM, etc) rather than by the chronoligical time value. Also, if I try converting it back to a datetime data type, it puts this rediculous '1/1/1900' before the time value, and I can't have that either in my application.

Any suggestions? You assistance is GREATLY appreciated!

D. Smith"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-03-11 : 11:02:25
How about this:

SELECT Convert(char(8), dateCol, 108) AS Time
FROM myTable
ORDER BY Convert(datetime, Convert(char(8), dateCol, 108))


The ORDER BY will convert the time portion back to a datetime value, so it will sort properly. The SELECT statement won't display the 1/1/1900, and you can modify it to suit your needs, as long as the ORDER BY clause remains the same.

Go to Top of Page
   

- Advertisement -