Solved

SQL where clause to compare times

Posted on 2015-02-04
14
99 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
  • 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:ScottPletcher
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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 69

Expert Comment

by:ScottPletcher
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:
ScottPletcher 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:ScottPletcher
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

Backup Your Microsoft Windows Server®

Backup 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.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now