E-Mail:
Get our new Windows 7 eBook (PDF) for $7 with 70+ Tips. Download Now!

How To Calculate If Hour Is Even Or Odd In T-SQL..

Hello there…

Today is going to be a short posting on how to calculate whether or not the hour in either the function getdate() or a datetime field in a table is either even or odd. I am using this to determine which tables (evenhour or oddhour) are recording page impressions. Here is the T-SQL script:  select datepart(hour,getdate())%2 as evenodd

The % is know in SQL Server as “modulo” and what this does is it provides the remainder of one number divided by another.  When using % with the datapart and getdate functions it returns either zero if the hour of datepart is even or will return one if the hour of datepart is odd.

This logic comes in very handy when you need to know which hour table (even or odd) you need to process hits, impressions, clicks, etc.  Tomorrow I will show you how to implement this in a SSIS package to calculate the even or odd hour and to move the data to their respected tables based upon the result of %.  So keep checking back tomorrow for this solution!

What Do You Think?

 

Posted Recently

40 queries / 0.144 seconds.