Solved

SQL Server DatePart HOUR

Posted on 2016-08-11
6
81 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
[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
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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

Independent Software Vendors: 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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

749 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