Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server DatePart HOUR

Posted on 2016-08-11
6
Medium Priority
?
133 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 66

Accepted Solution

by:
Jim Horn earned 1600 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 66

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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 

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 400 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

730 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