• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 112
  • Last Modified:

SQL where clause to compare times

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
GPSPOW
Asked:
GPSPOW
  • 5
  • 4
  • 4
  • +1
2 Solutions
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give us a data mockup of what you're trying to pull off here.
0
 
GPSPOWAuthor Commented:
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
 
Scott PletcherSenior DBACommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
PortletPaulCommented:
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
 
GPSPOWAuthor Commented:
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
 
PortletPaulCommented:
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
 
GPSPOWAuthor Commented:
thanks.

But how do I substitute getdate() for the '20150204' above?
0
 
Scott PletcherSenior DBACommented:
WHERE
    column_name >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 1, 0) --yesterday
    AND
    column_name < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) --today
0
 
PortletPaulCommented:
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
 
PortletPaulCommented:
sorry Scott
0
 
GPSPOWAuthor Commented:
I am comparing Transaction Date and Time between the prior day at 11 PM and today at 3 AM.

Thanks
0
 
Scott PletcherSenior DBACommented:
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
 
Scott PletcherSenior DBACommented:
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
 
GPSPOWAuthor Commented:
Thanks Scott.

Exactly what I needed.

I did change the 4 to a 3.

Glen
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now