?
Solved

SQL where clause to compare times

Posted on 2015-02-04
14
Medium Priority
?
109 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 300 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 1700 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

764 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