views:

275

answers:

4

I have a records of events with starttime and endtime for a calendar control.

I want to get the events done on a particular date say 2/28/2009

But the db table has date data in form 2/28/2009 10:00:00, 2/28/2009 12:00:00.

I tried this query in a sproc created in VS 2005 IDE but it didn't work

ALTER PROCEDURE dbo.spSelectBenchEvent
(   
@EVENT_DATE DATE // BTW, THIS RETURNS ERROR :CANNOT FIND DATE DATATYPE.
                 //@EVENT_DATE HAS INVALID DATATYPE 
)
AS
BEGIN TRAN
SET NOCOUNT ON;

SELECT     ID, EID, BENCHID, PACCODE, START_TIME, END_TIME
    FROM         tbl_benchbook
    WHERE START_TIME=EVENT_DATE
    ORDER BY START_TIME

Thanks in advance.

+2  A: 

Hi,

If you wish to evaluate all events on the date 3/29/2009 use the following where cluase.

SELECT *
FROM tableName
WHERE Date >= '2009/03/29' AND  Date < '2009/03/30'

The key point to take away here is that in order to capture all records that occured on a given date you need to define a date range that includes all time values for that day.

Make sense?

Cheers, John

John Sansom
+1 for method which utilizes index coverage
Learning
btw : just noticed that it would indeed be a great Feb if it had 30 days :)
Learning
With this method, you obviously can't just add a day to the day part of the date :)
ck
@Learning: Updated just for you Tipex ;-)
John Sansom
@Learning: My code is oh so efficient that it can actually bend time and space.
John Sansom
John : Ha! I suspected that ... that I pointed to it was just a blip in that space time continuum. Sorry. :)
Learning
If I recall correctly, the ISO date format will always be recognised: '2009-02-15T00:00:00.000'
MatthieuF
+2  A: 

There were many questions related to this one, check out:

and for best performance-wise solution check: MS SQL Date Only Without Time

Basically your code could look like:

select     
    id, eid, benchid, paccode, start_time, end_time
from  
    tbl_benchbook
where start_time >= dateadd(dd, datediff(dd, 0, @event_date), 0)
and start_time < dateadd(dd, datediff(dd, 0, @event_date)+1, 0)
kristof
+2  A: 

Based on your updated ques , this should work :

   SELECT  ID, EID, BENCHID, PACCODE, START_TIME, END_TIME
   FROM    tbl_benchbook
   WHERE   START_TIME >= @EVENT_DATE 
   AND     START_TIME < DATEADD(day,1,@EVENT_DATE)
   ORDER BY START_TIME
Learning
Much clearer, but you'll pick up any start times with a time of midnight the following day ("between" is inclusive). I think you'll need >= and <. You'll also need @EVENT_DATE, not EVENT_DATE.
Hobo
@All, i have a new problem, date is not getting recognised as a valid datatype.
kk
@Hobo : Good catch! I'll update.
Learning
this will not work if there is time on the @EventDate
KM
A: 
--1. you need to make sure there is no time on the parameter
--2. you can just use "+1" on the datetime, to get the next day



    enter ALTER PROCEDURE dbo.spSelectBenchEvent
    (   
    @EVENT_DATE DATETIME 
    )
    AS
    BEGIN TRAN
    SET NOCOUNT ON;

    --remove any time from given date
    SET @EVENT_DATE=CONVERT(char(10),@EVENT_DATE,111)


    SELECT     ID, EID, BENCHID, PACCODE, START_TIME, END_TIME
            FROM         tbl_benchbook
            WHERE START_TIME>=@EVENT_DATE AND START_TIME<@EVENT_DATE+1
            ORDER BY START_TIME
    code here
KM
Casting a date (numeric) to a char is much slower than just leaving it numeric.
StingyJack
it only converts it one time, hardly a cpu killer!
KM