Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

pass pure time to my query

 
   Database Help (Home) -> Programming RSS
Next:  [Info-Ingres] Autoreply: Autoreply: Autoreply: Au..  
Author Message
iccsi

External


Since: May 05, 2011
Posts: 7



(Msg. 1) Posted: Tue Aug 16, 2011 11:48 am
Post subject: pass pure time to my query
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I have a datetime field which contains data like following:

03:15:00 PM
04:25:00 PM

I have a query and would like to query on this field like following:

Select myfield from mytable where mydatetime = @MyDateTime

I pass '03:15:00 PM' to the function.
For some reason, it does not return the record.
I tried to use convert(char(15), @MyDateTime, 108) which does not work
and tried to use cast which does not work either.

I would like to know how I can query a datetime field for pure time
information,
Your help is great appreciated,

 >> Stay informed about: pass pure time to my query 
Back to top
Login to vote
rpresser

External


Since: Jan 17, 2008
Posts: 19



(Msg. 2) Posted: Tue Aug 16, 2011 1:42 pm
Post subject: Re: pass pure time to my query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

A datetime value represents an instant in history. This is a date plus a time. Today at 3:15:00 PM is a different datetime than tomorrow at 3:15:00 PM.

When you feed the string '03:15:00 PM' to a query that wants to compare it to a datetime, it will convert the string to a datetime first ... and that string will represent the time 3:15:00 PM *ON THE DATE 1/1/1900*.

Comparing that datetime to any datetime in your table is unlikely to find a match, unless you have dates in your table from the early 20th century.

You need to either compare the string to the time format of the original datetime --

Select myfield from mytable where convert(char(15), mydatetime, 108) = convert(char(15), @MyDateTime, 108)

or use other methods of matching the time:

-- this expresses datetimes as doubles, then throws away the integer part of it,
-- which represents the date; leaving a double between zero and one that
-- expresses the time
Select myfield from mytable
where convert(double, mydatetime) - floor(convert(double, mydatetime))
= convert(double, @mydatetime) - floor(convert(double, mydatetime))

-- this computes the number of seconds since a reference point,
-- then subtracts (the number of days since that reference point) * 86400
Select myfield from mytable
where datediff(second, mydatetime, '2000-01-01') - datediff(day, mydatetime, '2000-01-01')*86400
= datediff(second, @mydatetime, '1900-01-01')

 >> Stay informed about: pass pure time to my query 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 3) Posted: Tue Aug 16, 2011 5:25 pm
Post subject: Re: pass pure time to my query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

iccsi ( ) writes:
> I have a datetime field which contains data like following:
>
> 03:15:00 PM
> 04:25:00 PM
>
> I have a query and would like to query on this field like following:
>
> Select myfield from mytable where mydatetime = @MyDateTime
>
> I pass '03:15:00 PM' to the function.
> For some reason, it does not return the record.
> I tried to use convert(char(15), @MyDateTime, 108) which does not work
> and tried to use cast which does not work either.

There is no time-only data type in SQL 2000. (It was added in SQL 2008),
so you cannot have a value of 02:12:23 in datetime column. If you do:

DECLARE @d datetime
SELECT @d = '03:15:00 PM'
SELECT @d

You will find that it returns a value of 1900-01-01 15:15:00.000,
1900-01-01, being the default date when no date is provided.

If you want to work with time-only data, the best is probably to stick
with this default date, and then filter it out in displays.


--
Erland Sommarskog, SQL Server MVP, esquel.RemoveThis@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
 >> Stay informed about: pass pure time to my query 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
How to pass query hints up to CTEs? - Hello, I know that you can not use OPTION in a CTE but if you use it on the SELECT that uses the CTE will the hint be used by the CTE? For a simple example; ;WITH HintPlease AS (SELECT .... WHERE Column1 = @ParamValue) SELECT .... FROM HintPlease..

Pass Variables to BEGIN or END T-SQL query - I have to run a SQL job based on ----------------------------------------------- USE Process GO Update TA set sap_no=TL.sap_no,product_order=TL-product_order from teosauto AS TA join teoslot AS TL on TA.product_order=TL.product_order --The above..

Time query - Hi I have been struggling with a query where I need to summerize per quarter (15minuts) , I have made this query which almost works select digits, cast(cast(DATEPART (hour , DATECALLINITIATED ) as varchar(2))+':'+ cast((DATEPART ( minute ,..

Problem with query taking a long time. - I have a large SQL table, called Event, that has 4.6 million records in it. My problem is that one particular query on the table is taking over 30 seconds to run. All the other queries that I have take under 1 second to run. I have tried numerous..

Find the common time zone by query - Hi, I have two tables. Table1 Start time End Time ----------------------------------------------------- DateTime1 DateTime2 DateTime3 DateTime4 DateTime5 DateTime6..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]