Solved

SQL Server DatePart HOUR

Posted on 2016-08-11
6
39 Views
Last Modified: 2016-08-11
Does my select below differentiate on the 24 hour clock?

        DECLARE @Hours INT
        SET @Hours = DATEPART(HOUR, GETDATE())
0
Comment
Question by:lrbrister
  • 3
  • 2
6 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 400 total points
ID: 41752400
Yes, as internally dates are stored as HH:MM:DD with a 24-hour day.
SELECT DATEPART(HOUR, GETDATE())      -- 11 when run now in CST
SELECT DATEPART(HOUR, GETDATE() + .5) -- 23 when run now in CST

Open in new window

Can you give us more details as to what your requirements are?
1
 

Author Comment

by:lrbrister
ID: 41752405
That was it... I was just verifying that this would work...
Maybe the every two hours part is clunky?

DECLARE @LastEntry DATETIME
DECLARE @Hours INT
SET @Hours = DATEPART(HOUR, GETDATE())
SET @LastEntry = ( SELECT   MAX(ISNULL(DateAdded, GETDATE()))
                    FROM     dbo.SendGridAllocations
                    )

IF (@Hours - DATEPART(HOUR, @LastEntry) > 0 AND @Hours IN (2,4,6,8,10,12,14,16,18,20,22))
    BEGIN

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41752415
Explain for us at Barney-level what you are trying to pull off, as I'm not getting what you are asking us.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:lrbrister
ID: 41752434
Well...
I am getting usage stats from an api in my own .Net app
Those stats come in on the same task that runs every 10 minutes for other "methods"
I want to store the usage stats every two hours instead of every 10 minutes.

So... without touching the .Net application that ALREADY is inserting the data
I want to modify the stored procedure to do it only every 2 hours
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 100 total points
ID: 41752453
You can shorten the code.  Btw, it's somewhat iffy to assume that the LastEntry day will be today, but presumably you know that it "can't" fail and will always be the current date:

IF (@Hours - DATEPART(HOUR, @LastEntry) > 0 AND @Hours % 2 = 0
0
 

Author Closing Comment

by:lrbrister
ID: 41752494
Perfect... thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

863 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

22 Experts available now in Live!

Get 1:1 Help Now