T-SQL to trim a datetime to the nearest date?
Asked Answered
D

8

21

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?

Deflective answered 11/2, 2009 at 19:38 Comment(0)
W
20

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

select dateadd(dd, datediff(dd, 0, getdate())+0, 0)
Witting answered 11/2, 2009 at 19:43 Comment(1)
I don't think the +0 is required (at least using sql server 2012+). This is the most creative and useful answer I've seen.Buskined
D
42

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)
Dufy answered 5/1, 2012 at 20:24 Comment(3)
Easily the best answer for its simplicity and clarity.Bandsman
Not if you are using SqlServer 2005Breakfront
@nalply because SQL Server 2005 doesn't have the date data typeSaari
W
20

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

select dateadd(dd, datediff(dd, 0, getdate())+0, 0)
Witting answered 11/2, 2009 at 19:43 Comment(1)
I don't think the +0 is required (at least using sql server 2012+). This is the most creative and useful answer I've seen.Buskined
S
5

Here's another solution:

SELECT CAST( FLOOR( CAST( GETDATE() AS float) ) AS smalldatetime)
Seessel answered 27/7, 2009 at 21:58 Comment(0)
S
3

This is a simple way to get the date (as a string) from a datetime:

convert(varchar, <the date field/value/etc>, 101)

But note that ordering on this field will be alphabetical rather than by date since it's now a string

Seabrook answered 11/2, 2009 at 19:40 Comment(0)
P
2

I simply do cast(getdate() as date)

Prescriptible answered 17/5, 2018 at 18:14 Comment(0)
S
1

Yes. There are many formats to choose from so I'll link it instead.

http://library.cirr.com/Microsoft/SQL-Server-v7/html/ca-co_1.htm

If you wish to zero out the time like your post implies, you can try this:

select cast(convert(varchar, getdate(), 101) as datetime)
Singspiel answered 11/2, 2009 at 19:40 Comment(2)
convert(datetime, convert(varchar, getdate(), 101))Remanent
broken link. New link for the page library.cirr.com/Microsoft/SQL-Server-v7/html/ca-co_1.htmNatelson
R
0
declare @CurrentDate datetime
set @CurrentDate = dateadd(dd, datediff(dd, 0, getdate()), 0)

--or--

select dateadd(dd, datediff(dd, 0, MyDateColumn), 0) as DateOnly
from tblX
Remanent answered 11/2, 2009 at 19:43 Comment(0)
D
0

In my searches I came across the following solution, it strips time out of UTC time only, but I found it interesting, so I thought someone else would too:

FUNCTION TrimDate(@dt AS DATETIME) RETURNS DATETIME
BEGIN
    RETURN CAST(CAST((@dt - 0.500000038580247) AS INT) AS DATETIME) 
END

I would presume that it runs quickly since all it's doing is rounding and casting.

Deflective answered 11/2, 2009 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.