Solved

SQL Server DatePart HOUR

Posted on 2016-08-11
6
31 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

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

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 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