Whats the best MS SQL datatype to store the time of day independently from the date ?

Whats the best way do deal with collecting a date and an hour seperately in setting the data types of the fields in MS SQL 2005.  I have a web form where I need to capture both the day date ( like 3/24/2010) as well as the time ( like 4:00 PM ).  The data type in SQL for the day date could be "datetime" but what about the hour?   "datetime" also ?     That seems the wrong way to go about this... probably thinking of this the wrong way.
bcombeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
The best way would be to combine the values from your two inputs and put it into a single datetime value!
Because really, it's a date and time (datetime)
Amandeep Singh BhullarCommented:
Its not possible to save only time part in datetime or smalldatetime datatype.

You can also check the msdn discussion on this topic from link:
http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/76281bc8-a97a-46df-a78a-4864ba4cdb1b

Hope this will help you
cyberkiwiCommented:
There are plenty of queries shown in EE that teach you how to extract just the date or time portion from a datetime, but normally, it is a sort of "timestamp" or "chronology" or "schedule" so it does not make sense to store separately.

E.g. select * from events order by eventdatetime  -- makes sense
E.g. select * from events order by eventdate, eventtime  -- hmm... not optimised

E.g. select * from events where eventdatetime >= dateadd(dd, -1, getdate()) -- within last 24 hours
The equivalent with the split fields would be much more tricky.

However, if you must store them separately, then I would still recommend 2 fields, both datetime because it is the datatype for date/time/datetime values.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Kusala WijayasenaSoftware EngineerCommented:
LowfatspreadCommented:
use datetime for both...

ensure that the date is stored without a time...   use midnight 00:00:00.000
 or just store it as a "date" component e.g. '20100426'
and ensure that the time is stored without a date ... use 19000101

that way you can still use the Date/Time functions to extract the individual components...

(probably should have an insert/update tigger to conform the columns to only date/time usage...  those developers will take there eyes off the ball at some stage )


alternatively the 2008 version of sql server supports separate date and time datatypes...
dshrivallabhCommented:
If its Hours Mins Secs...and you want to store them differently then

INT is the best

and You can have a function GetTime which will return the time of the day as a VARCHAR string
bcombeAuthor Commented:
I like cyberkiwis angle but I see that then Im taking the two entered values from the page and properly combining them into a single field.  When making reports or read only pages I would split the field into the seperate date and time components.  

The only reason to possibly store them into different fields was for the sake of doing some date/time math but in retrospect that could be done anyhow, right

I'll use dsrivallabh's suggestion with the GetTime function too - but should I use cast to change the data type from varchar to datetime before saving when I use that function?
dshrivallabhCommented:
Yes..you can to that if the date is also stored separately then you can just get the date part also form a varchar string and CAST it to DATETIME
cyberkiwiCommented:
>> The only reason to possibly store them into different fields was for the sake of doing some date/time math but in retrospect that could be done anyhow, right

There really is no date time math that you can do with 2 fields that you cannot do with one.
However, with 2 fields, it only makes sense indexing-wise if you have a requirement for counting, say, data between 3:00pm and 5:00pm each day, to ignore the date portion.

Most usages of 2 fields I have seen are simply for input purposes, and in reality, the field is always used as a time-in-the-continuum sense.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bcombeAuthor Commented:
Thank you much!

Although I wont use the following approach I thought it was interesting:
http://weblogs.sqlteam.com/jeffs/archive/2007/10/31/sql-server-2005-date-time-only-data-types.aspx
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.