Groups | Blog | Home
all groups > sql server programming > january 2005 >

sql server programming : Extrating the Time from DateTime


JT
1/8/2005 9:33:01 PM
Hi,
I am trying to run a sproc that includes the following in the WHERE clause:

AND (ApptTime BETWEEN @StartTime AND @EndTime)

ApptTime is a datetime data type, as are the two variables. When I run the
sproc by explicitly setting the variables, I get the desired results. I
explicitly set the variables as follows:

SELECT @StartTime = CAST('07:00' AS DATETIME)

However, when I pass in the value of @StartTime from my app, it sets
@StartTime to #01/01/2005 07:00:00#. What I need to do is extract just the
07:00 from this. Can anyone give me a suggestion.
Thanks.

JT
--
JT
1/8/2005 10:01:02 PM
Thanks JT. I did a search and found something similar
with CONVERT(CHAR(20), @myDateTime, 108)

BTW, I posted to your JOIN question, although not nearly as eloquently as
Louis.
JT

[quoted text, click to view]
JT Lovell
1/9/2005 12:51:40 AM
Anything for another JT! Give this a run:

/* declare a few vars */
DECLARE @date_and_time as datetime
DECLARE @just_time as datetime
SET @date_and_time = '01/01/2005 07:00:00'

/* display start date */
SELECT @date_and_time

/* strip out the date */
SET @just_time = convert(datetime, convert(varchar(5), @date_and_time, 8))

/* output result */
SELECT @just_time


--
JT Lovell


[quoted text, click to view]

David Gugick
1/9/2005 12:52:42 AM
[quoted text, click to view]

What is the date portion you are working with here? A DATETIME has a
date portion stored as well as the time. So it must be something. If you
only care about the time portion and can set the appropriate date
portion within the procedure, mangle the date there, rather than relying
on the application. I'm not sure this is your case. if not, can you
elaborate on what you are using for the date in the datetime column?

For example, if the procedure always works with the current date, then
you could use:

Parameter @StartTime datetime
Set @StartTime = CONVERT(datetime, CONVERT(varchar(8), getdate(), 112) +
SPACE(1) + CONVERT(varchar(5), @StartTime, 108))



--
David Gugick
Imceda Software
www.imceda.com
Hugo Kornelis
1/9/2005 11:34:38 PM
[quoted text, click to view]

Hi JT,

Here's another way to strip the date part and leave the time part only.
The other suggestions use conversion to string and back to datetime, my
version uses datetime arithmatic only. This method is slightly faster than
JT Lovell's suggestion. The results will be the same:

declare @test datetime
set @test = '2005-01-01T07:00:00'
select dateadd(day, datediff(day, @test, 0), @test)

Best, Hugo
--

AddThis Social Bookmark Button