how to cast datetime into date in sql 2005

This question already has an answer here:

  • How to return only the Date from a SQL Server DateTime datatype 37 answers

  • You can try this:

    convert(DateTime, floor(convert(float, getdate())))
    

    That gets rid of the time part, by converting it into the underlying float number and rounding down to the nearest integer. The integer part represents the date and the decimal part is the time, so converting the integer back to a DateTime give the same date you started with but without any time part.

    You can also wrap it up as a scalar-valued function:

    CREATE FUNCTION [dbo].[DateTrunc] (@date DateTime)
    RETURNS DateTime
    AS
    BEGIN
        return convert(DateTime, floor(convert(float, @date)))
    END
    
    链接地址: http://www.djcxy.com/p/94384.html

    上一篇: 如何在SQL Server中获得今天的第一个小时?

    下一篇: 如何在SQL 2005中将日期时间转换为日期