Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Check if DateTime value is after a certain hour?

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2013-07-01 : 16:21:23
I need to write a IF statement that looks at a DateTime variable which holds a full date and time (i.e. 07/01/2013 1619 Hours) and see if that variable happened after a certain time of day (i.e. 0100 hours). Is there an easy and efficient way of doing this? I originally though about splitting out the time format and then converting to military time then doing the compare but that seems overly complicated.

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-07-01 : 16:48:09
You can use the datepart function, like in this example:
DECLARE @x DATETIME = GETDATE();

IF DATEPART(HOUR,@x) > 1
PRINT 'Yes, it is 1:00 AM or later'
ELSE
PRINT 'No, it is not yet 1:00 AM'
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2013-07-01 : 16:48:45
Again, you come to my rescue. Thanks

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-07-01 : 17:33:01
Again , you are very welcome - glad to help.
Go to Top of Page
   

- Advertisement -