Solved

SQL where clause to compare times

Posted on 2015-02-04
14
104 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 65

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 48

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 48

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 48

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 48

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Giving  direct access rights to main database  issues 9 59
Using rowversion for incremental load of datawarehouse. 12 30
mssql 7 32
Datatable / Dates ? 4 24
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

740 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