Skip to content
Advertisement

Rounding time untill in SQL

I have a table with timestamp that I want to round off at 15 min. interval. I can round off using the below Query but it rounds off both 11:58 and 12:02 to 12:00 which is not what I want. I would like to round off timestamp at 15 min. interval which gives me time_untill ie for anything between 11:45 to 11:59 should be rounded off to 12 and anything between 12:00 to 12:14 should be rounded off to 12:15. Please let me know how can I achieve that? Thanks

SELECT transaction_id, 
 CONVERT(smalldatetime, ROUND(CONVERT(float, CONVERT(datetime,   entry_date_time)) * 96.0, 0, 1) /96.0)   as transaction_datetime
     FROM <table>

Advertisement

Answer

You can use datetimefromparts():

select dateadd(minute,
               15,
               datetimefromparts(year(entry_date_time), month(entry_date_time), day(entry_date_time),
                                 datepart(hour, entry_date_time),
                                 15 * (datepart(minute, entry_date_time) / 15), 0, 0
                                )
              ) as roundup15
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement