views:

3520

answers:

6

Duplicate of http://stackoverflow.com/questions/2775/whats-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 then by day, so I need a way of nulling out the time component of a datetime column.

How do I do this.

Thank you

+1  A: 

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)
Austin Salonen
convert(datetime, convert(varchar, getdate(), 101))
Gordon Bell
broken link. New link for the page http://library.cirr.com/Microsoft/SQL-Server-v7/html/ca-co_1.htm
monkeypushbutton
+1  A: 

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

This is the simplest way to get the date from a datetime.

notnot
+2  A: 

one way change getdate() to your column name

select dateadd(dd, datediff(dd, 0, getdate())+0, 0)
SQLMenace
A: 
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
Gordon Bell
A: 

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.

Allain Lalonde
+2  A: 

Here's another solution:

SELECT CAST( FLOOR( CAST( GETDATE() AS float) ) AS smalldatetime)
richardtallent