Solved

Extract time from datetime data type column

Posted on 2007-12-04
3
7,251 Views
Last Modified: 2013-11-30
I have a database that has several datetime columns.  The thing is all I care about is the time portion of the column.  The thing is I need to retain the numeric time value as I will need to do time difference calculations on the remaining time values.  I know I cannot store this data in SQL as a datetime but I want to be able to display the time without on my reports without showing the bogus date used by SQL when you only load the time value in a datetime column.  Any guidance would be appreciated.  
0
Comment
Question by:ktQueBIT
3 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
SELECT convert(VARCHAR,GETDATE(),108)
0
 
LVL 25

Expert Comment

by:imitchie
Comment Utility
If your datetime field is dt, then

select convert(datetime, convert(varchar, dt, 108)), other1, other2 from table1

will result in the time portion only remaining.

To do time different calculations between two datetimes

select dt1 - dt2 from table1

the resulting type is datetime
0
 
LVL 6

Accepted Solution

by:
PaultheBroker earned 250 total points
Comment Utility
Although you don't need the date interpretation of the zero (1st Jan 1900), you are obviously best to leave this in datetime format for ease of calculation as Mitch shows above.  As mentioned above, when displaying the date in your reports there are several formats you can use to dispaly only the time using the CONVERT(datatype,date,format) function that are mentioned above.  8 or 108, 14 or 114, or one of the other formats (see BOL for full list) in combination with a RIGHT(string,length) function to chop off the unwanted date.  i.e right(convert(char(19),myTime,100),7) will return 7:23PM.

You might like to wrap whichever you choose into a simple UDF so that it is easy to consistently dispaly the time in your reports (and so you can easily changee which format you use just by changing the UDF definition....)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now