Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL to trim a datetime to the nearest date?

Tags:

Duplicate of What's the BEST way to remove the time portion of a datetime value (SQL Server)?

I have a column that tracks when things are created using a datetime, but I'd like to generate a report that groups them by day, so I need a way of nulling out the time component of a datetime column.

How do I do this?

like image 643
Allain Lalonde Avatar asked Feb 11 '09 19:02

Allain Lalonde


3 Answers

Why not convert straight to date:

select convert(date, getdate())

This truncates days, not rounds. To round Days do this:

select convert(date, getdate() + 0.5)
like image 122
pieman72 Avatar answered Sep 22 '22 08:09

pieman72


One way is to change getdate() to your column name,

select dateadd(dd, datediff(dd, 0, getdate())+0, 0)
like image 20
SQLMenace Avatar answered Sep 24 '22 08:09

SQLMenace


Here's another solution:

SELECT CAST( FLOOR( CAST( GETDATE() AS float) ) AS smalldatetime)
like image 5
richardtallent Avatar answered Sep 23 '22 08:09

richardtallent