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.
Author |
Topic |
challapavan
Starting Member
8 Posts |
Posted - 2007-08-28 : 11:44:59
|
Hi there i have a column with datetime field...i want to extract only time field to the new column...Start Date8/24/2007 10:008/24/2007 10:008/24/2007 10:008/24/2007 10:308/24/2007 11:008/24/2007 11:008/24/2007 12:008/24/2007 12:008/24/2007 12:008/24/2007 13:008/24/2007 14:008/24/2007 15:008/24/2007 15:008/24/2007 15:008/24/2007 15:008/24/2007 17:008/24/2007 20:00i want something likeStartDate Start time8/24/2007 10:008/24/2007 10:008/24/2007 10:008/24/2007 10:308/24/2007 11:008/24/2007 11:008/24/2007 12:008/24/2007 12:008/24/2007 12:008/24/2007 13:008/24/2007 14:008/24/2007 15:008/24/2007 15:008/24/2007 15:008/24/2007 15:008/24/2007 17:008/24/2007 20:00can anyone plz find the solution for this one.Thanks a lot!!!pavan |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-08-28 : 11:50:19
|
You can use the CONVERT function with the appropriate style. But I don't recommend on splitting these though since you won't be using datetime data type anymore. It makes doing calculations harder since the data will now be varchar.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-28 : 12:02:33
|
Where do you want to show data?MadhivananFailing to plan is Planning to fail |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-08-29 : 06:25:16
|
See also: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210&SearchTerms=Time+Only+Function |
|
|
sunsanvin
Master Smack Fu Yak Hacker
1274 Posts |
Posted - 2007-08-29 : 06:53:04
|
use some thing like this to get only timeselect substring(convert(varchar(30),getdate(),120),11,6)VinodEven you learn 1%, Learn it with 100% confidence. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-29 : 07:09:37
|
quote: Originally posted by sunsanvin use some thing like this to get only timeselect substring(convert(varchar(30),getdate(),120),11,6)VinodEven you learn 1%, Learn it with 100% confidence.
However, the actual answer relies on the answer to my question MadhivananFailing to plan is Planning to fail |
|
|
sunsanvin
Master Smack Fu Yak Hacker
1274 Posts |
Posted - 2007-08-29 : 07:13:35
|
yes Madhiyou are correctVinodEven you learn 1%, Learn it with 100% confidence. |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-08-29 : 08:35:21
|
None of the answers given here return a time, they are return strings. Try to work with raw, unformatted data in SQL as much as possible, and avoid converting values to strings since at that point it is not longer an actual date or time or datetime and you are relying on date/time formats that might vary from client to server and you might have conversion or parsing issues. keep it simple, work with data in correct types.To retrieve just the decimal portion of a number like 10.25,what do you do? You subtract 10.00 from 10.25, resulting in 0.25. You use the same approach with getting a time portion of a datetime. Just subtract the Date at midnight from the datetime, and the result is just the time.this expression:dateAdd(dd,datediff(dd,0,yourdate),0) will return the yourdate at midnight, so:select yourdate - dateAdd(dd,datediff(dd,0,yourdate),0) will return just the time of yourdate.It is actually very useful to store times separate from dates; more here: http://weblogs.sqlteam.com/jeffs/archive/2004/12/02/2954.aspxalso, here's a bunch of useful datetime functions including TimeOnly():http://weblogs.sqlteam.com/jeffs/archive/2007/01/02/56079.aspx- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
|
|
|
|
|