Sacar solo la fecha de un datetime de SQLSERVER

Tenia un problema que en en mi campo datetime de SQLSEVER 2005, se grababa con la fecha y hora de la insercion. y ahora necesitaba agrupar por dia .Hay muchas formas de hacer esto pero lo mas facil es usando
CAST(FLOOR( CAST( GETDATE() AS FLOAT ) )
AS DATETIME
) que recupra solo la fecha del dia en cuestion. Tambien esta la tradicional forma de concatenacion
pero eso ya quedo en el pasado.Ya que es mas lento
CAST(
(
STR( YEAR( GETDATE() ) ) + '/' +
STR( MONTH( GETDATE() ) ) + '/' +
STR( DAY( GETDATE() ) )
)
AS DATETIME
)


Aca hay una funcion mejorada:
DATEADD(d, 0, DATEDIFF(d, 0, [tstamp]))

Edit: While this will remove the time portion of your datetime, it will also make the condition non SARGable. If that's important for this query, an indexed view or a between clause is more appropriate

Fuente= http://www.bennadel.com/blog/122-Getting-Only-the-Date-Part-of-a-Date-Time-Stamp-in-SQL-Server.htm

http://stackoverflow.com/questions/467103/ms-sql-date-only-without-time

Saludos
Adalberto Montanía

Comentarios