Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Rounding Off Datetimes - SQL Server

RSS
Modified on Thu, Oct 13, 2016, 10:17 AM by Administrator Categorized as SQL Server
The following SQL statement demonstrates how to TRUNCATE a datetime to seconds — to discard fractional seconds. The same can be done for any unit of time (day, month, year, etc.)

declare @BaseDate datetime = '2016-01-01'

select top 10 
     BatchDate = dateadd(SECOND, datediff(SECOND, @BaseDate, UpdatedOn), @BaseDate)
    ,Qty = count(2)
from
    dbo.UploadedData
where 1=1
    and UpdatedOn >= '2016-10-10'
group by
    dateadd(SECOND, datediff(SECOND, @BaseDate, UpdatedOn), @BaseDate)

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.