DateTime to a preferred (15 minutes) interval

Seriously, this piece is another great addition to your cookbook!
Before this…make a guess….I used a basic CASE-statement to retrieve the right intervals I needed.
This one is a life changer!

SELECT
(ABS (DATEPART( MINUTE, '2012-05-22 08:09:00') / 15)) AS AbsoluteDivide9Minutes,
(ABS (DATEPART( MINUTE, '2012-05-22 08:33:00') / 15)) AS AbsoluteDivide33Minutes,
(ABS (DATEPART( MINUTE, '2012-05-22 08:09:00') / 15) * 15) as '0-15 minute interval',
(ABS (DATEPART( MINUTE, '2012-05-22 08:22:00') / 15) * 15) as '15-30 minute interval',
(ABS (DATEPART( MINUTE, '2012-05-22 08:33:00') / 15) * 15) as '30-45 minute interval',
(ABS (DATEPART( MINUTE, '2012-05-22 08:46:00') / 15) * 15) as '45-60 minute interval'

Thanks to Jean-Pierre Broeders! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s