About MEDITECH Reports

 

View John Sharpe's profile on LinkedInI'm your host, John Sharpe, a MEDITECH Consultant living in Spokane, WA. Read more ...

Connect ...

Expand your network by connecting on these social media sites:

Sign up for the MEDITECH Reports Blog; details to attend MEDITECH workshops are sent out 1 week in advance to all MEDITECH Report blog subscribers.

These MEDITECH Workshops are sponsored by Donna Carroll at the MEDITECH Community Bulletin

MEDITECH NPR & RW RESOURCES:

Ascii Table: For NPR Report Writer
Loop Builder: For NPR Macros
Key Codes: For CDS Attributes
List Builder: For NPR Macros
MT Report Names: For NPR Reports
MT Structure Viewer: For Physicals
Strings: Format & Sort

HIT Topics

MEDITECH Reports

Current Articles | RSS Feed RSS Feed

MEDITECH DR: Comparing Date Time Fields Using SQL

 
See if you can spot the error in my query's WHERE clause for a BAR table in MEDITECH DR.

datetime compare 1 resized 600

When comparing a date time in SQL to a range of literal dates, we want to keep in mind that datetime is formatted as YYYY-MM-DD hh:mm:ss.nnn.  The nnn stands for fractional seconds. 

The T.ServiceDateTime is formatted as datetime '2010-07-26 10:03:56.000'.  The CONVERT converts '07/25/2010' to '2010-07-25 00:00:00.000' & '07/31/2010' to '2010-07-31 00:00:00.000'. 

Spot my problem now?  The ServiceDateTime that is '2010-07-31 01:56:00.001' will not be included in my data set.

Fortunately, converting the field ServiceDateTime to '2010-07-31 00:00:00.000' resolves the problem when the dates are compared.

WHERE CONVERT(datetime, CONVERT(CHAR(8), T.ServiceDateTime, 112)) BETWEEN CONVERT(
datetime, '07/25/2010') AND CONVERT(datetime, '07/31/2010') 


Want an alert when the next post is published? Click the phone and Subscribe to the MEDITECH Reports Blog!

Subscribe to the Meditech Blog     

                                                                                                                                                                                                          

Comments

How about using an end date one day past the ending date your looking for: 
 
WHERE  
(T.ServiceDateTime >= {d '2010-07-25'}) AND 
(T.ServiceDateTime < {d '2010-08-31'}) 
 
(using ODBC date escapes)
Posted @ Monday, August 09, 2010 9:45 PM by Leslie Mann
Thats a good one Leslie.
Posted @ Tuesday, August 10, 2010 1:15 PM by John Sharpe
Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics