Solved

SQL where clause to compare times

Posted on 2015-02-04
14
105 Views
Last Modified: 2015-02-04
I am trying to write a where clause to compare a datetime field to extract data that occurs with a stated time range.

I am using the statement to extract the time part of the field:

cast(DateTime as time)

What is the syntax to get the data for time range from the field formatted as YYYY-MM-DD HH:MM:SSS for a range of times on a specific date?

Thanks

Glen
0
Comment
Question by:GPSPOW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +1
14 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40589527
Give us a data mockup of what you're trying to pull off here.
0
 

Author Comment

by:GPSPOW
ID: 40589547
VisitID      QueryID      Query      Response      RowUpdateDateTime      GrpResp      GrpID      RespName
A10000422844      GU.VOID      Voiding Method      7      2015-02-01 12:52:31.000      7      GU.VOID_7      Indwelling Catheter
A10000422844      GU.VOID      Voiding Method      7      2015-02-01 15:42:02.000      7      GU.VOID_7      Indwelling Catheter
A10000422844      GU.VOID      Voiding Method      7      2015-02-02 20:16:19.000      7      GU.VOID_7      Indwelling Catheter
A10000422844      GU.VOID      Voiding Method      7      2015-02-03 00:22:21.000      7      GU.VOID_7      Indwelling Catheter

Here are a few lines of data.

thanks

glen
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40589582
If it's a contiguous range of time, use >= and <.  For example, for Feb 3, 2015 from 5AM thru all entries for 5PM:

WHERE
    DateTime >= '20150203 05:00' AND
    DateTime < '20150203 18:00'

If you only want specific times for a specific range of days -- such as 2-4pm on feb 2, 3 and 4 -- naturally you'll have to add a time check to the date range check:

WHERE
    DateTime >= '20150202'
    DateTime < '20150205' AND
    HOUR(DateTime) IN (14, 15, 16)

Btw, I use format:
YYYYMMDD[ hh:mm:ss[.sss]]
because that format always works correctly, regardless of date and/or language settings.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 49

Expert Comment

by:PortletPaul
ID: 40589808
Could you specify what you mean by the following please:

          for a range of times on a specific date

is it one time range? e.g. 08:15 to 17:15
or many? e.g. 08:00 to 12:30 then 13:00 to 17:00

Do the time ranges differ by date? (i.e. what is the relevance of "on a specific date")


------------------
just a small note.

If a field is datetime then "the format" (YYYY-MM-DD HH:MM:SSS) is irrelevant.
Datetime data is actually stored as numbers and then presented in a default format for us humans to read.
The default format can be changed and/or overridden but that does not alter the stored data at all.
------------------
0
 

Author Comment

by:GPSPOW
ID: 40589857
Here is what I need:

The dates are stored as YYYY-MM-DD HH:MM:SSS
The SSS part is always 000.
I need to pull all the transactions when the Date is between:

Example: 2/3/2015 - 2/4/2015 from 11 PM to 3 AM

2015-02-03  23:00.000  through 2015-02-04 03:00:000

The from and to dates will be variable depending on what date this process runs.  I usually use the getdate() function for this.  If I use getdate(), I am comparing it to 0 Hours and 0 minutes.

thanks

Glen
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 75 total points
ID: 40589925
For your example above (as Scott Pletcher has already shown) use >= and < 


WHERE  RowUpdateDateTime  >= '20150203 23:00.000'
 AND      RowUpdateDateTime  <   '20150204 03:00:000'


You do not need to use cast(... as time)

also see: "Beware of Between"
0
 

Author Comment

by:GPSPOW
ID: 40589934
thanks.

But how do I substitute getdate() for the '20150204' above?
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40589940
WHERE
    column_name >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 1, 0) --yesterday
    AND
    column_name < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) --today
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40589948
Oh, is that what you meant... but I still don't follow completely (what is " I am comparing it to 0 Hours and 0 minutes. " ?)

if you want to get data prior to the start of today:


WHERE  RowUpdateDateTime  >= '20150203 23:00.000'
 AND      RowUpdateDateTime  <   dateadd(day, datediff(day,0, getdate() ), 0)

-- or --

WHERE  RowUpdateDateTime  >= '20150203 23:00.000'
 AND      RowUpdateDateTime  <   cast(getdate() as date)
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40589950
sorry Scott
0
 

Author Comment

by:GPSPOW
ID: 40589955
I am comparing Transaction Date and Time between the prior day at 11 PM and today at 3 AM.

Thanks
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 425 total points
ID: 40589965
Sorry, wasn't sure of the specific times you needed:

WHERE  RowUpdateDateTime  >= dateadd(hour, -1, dateadd(day, datediff(day,0, getdate() ), 0))
  AND     RowUpdateDateTime  <  dateadd(hour, 4, dateadd(day, datediff(day,0, getdate() ), 0))

That will include all activity from 11pm-3:59:59am.

If you only want activity thru 2:59:59am, change "hour, 4" to "hour, 3".
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40589971
Note that the technique is the same:

Use dateadd to get to the day you need, then use dateadd to also adjust hours if needed.

As noted above, you do not want to use any functions on the table column, instead use them only on the date constants / comparison values.
0
 

Author Closing Comment

by:GPSPOW
ID: 40589979
Thanks Scott.

Exactly what I needed.

I did change the 4 to a 3.

Glen
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question