Solved

MSSQL - Select data from now to yesterday at a specific hour

Posted on 2014-02-08
3
981 Views
Last Modified: 2014-02-08
I have a table (SQLJobStatus) with a column (Job_Date) that has date/time records.  (2014-02-07 23:30:00.000)

I am trying to get all records from last night at 11:30pm  (23:30:00) until now.

select * from SQLJobStatus where ((Job_Date >= (DATEADD(d,-1,GETDATE()))) and ((convert(varchar, job_date, 108) >= '23:30:00')))

I tried this query however, I only get yesterday jobs from 23:30.  The >= on selecting the date is only giving me yesterday.  I see that is caused by the time portion being selected as > 23:30 which would only select the jobs on the next date that were greater than this.

Not sure how to write the correct syntax.

Would appreciate help writing a clean query to give me all records as needed.

Thanks
0
Comment
Question by:rsmith1969
[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
  • 2
3 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39844737
Something like this perhaps:

SELECT  *
FROM    SQLJobStatus
WHERE   Job_Date >= DATEADD(minute, 1410, DATEADD(DAY, -1, DATEDIFF(DAY, 0, GETDATE())))

Open in new window

0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 100 total points
ID: 39844740
This might be a tad clearer:
SELECT  *
FROM    SQLJobStatus
WHERE   Job_Date >= DATEADD(minute, 1410, DATEDIFF(day, 1, GETDATE()))

Open in new window

0
 

Author Comment

by:rsmith1969
ID: 39844741
Thanks...  For some reason I couldn't wrap by head around this... I kept running into a roadblock.  Looks like I need to research datedff.

Now any time I run my program it will give me all of the jobs from last night instead of the last 6 hours.

Thanks again....
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

695 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