Question : DateTime and smallDateTime

Below is my query.  The part I am having a question on is the very last line where I am finding a date between two dates.

In the sample below I want to see all rows where van.Countersigned_Date  = 09/16/2008.  If I take out the +1 nothing is returned.  This field is a DateTime field.  When I use the below query but search on a field which is a smallDateTime I can't use the +1.  If I do I get rows where the date is on 09/17/2008.

Why is there a difference?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
SELECT p.LastName + ', ' + p.FirstName as Provider, c.LastName + ', ' + c.FirstName as Client, van.serviceDate, 
van.Signed_Date, van.Countersigned_Date
FROM v_allNotes van INNER JOIN  Clients c ON c.ID = van.ClientID 
LEFT JOIN Personnel p ON van.ProviderID = p.ID 
LEFT JOIN fPayorNote(14) pi on pi.noteId = van.id AND pi.note_tables_id = van.note_tables_id 
WHERE c.[AgencyID]=14 AND van.not_billable_reason_id IS NULL 
AND van.Countersigned_Date BETWEEN CONVERT(DATETIME, '09/16/2008') AND CONVERT(DATETIME, '09/16/2008') +1
Open in New Window Select All

Answer : DateTime and smallDateTime

Hi,

The difference is that between includes both end points.

I prefer to write my datetime queries like this
AND van.Countersigned_Date >= CONVERT(DATETIME, '09/16/2008')
AND van.Countersigned_Date < dateadd( day, 1, CONVERT( DATETIME, '09/16/2008' )

which avoids including the 17th, and gets all datetimes for the 16th regardless of what the time component is.

HTH
  David

PS Attached a _corrected_ query for you
 

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
SELECT 
    p.LastName + ', ' + p.FirstName as Provider
    , c.LastName + ', ' + c.FirstName as Client
    , van.serviceDate
    , van.Signed_Date
    , van.Countersigned_Date
FROM v_allNotes van INNER JOIN  Clients c ON c.ID = van.ClientID 
LEFT JOIN Personnel p ON van.ProviderID = p.ID 
LEFT JOIN fPayorNote(14) pi on pi.noteId = van.id AND pi.note_tables_id = van.note_tables_id 
WHERE 
    c.[AgencyID]=14 
    AND van.not_billable_reason_id IS NULL 
    AND van.Countersigned_Date >= CONVERT(DATETIME, '09/16/2008') 
    AND van.Countersigned_Date < dateadd( day, 1, CONVERT( DATETIME, '09/16/2008' )
 
Open in New Window Select All
Random Solutions  
 
programming4us programming4us