Best approach to remove time part of datetime in SQL Server

前端 未结 23 1672
南旧
南旧 2020-11-21 22:51

Which method provides the best performance when removing the time portion from a datetime field in SQL Server?

a) select DATEADD(dd, DATEDIFF(dd, 0, getdate(         


        
23条回答
  •  难免孤独
    2020-11-21 23:09

    If possible, for special things like this, I like to use CLR functions.

    In this case:

    [Microsoft.SqlServer.Server.SqlFunction]
        public static SqlDateTime DateOnly(SqlDateTime input)
        {
            if (!input.IsNull)
            {
                SqlDateTime dt = new SqlDateTime(input.Value.Year, input.Value.Month, input.Value.Day, 0, 0, 0);
    
                return dt;
            }
            else
                return SqlDateTime.Null;
        }
    

提交回复
热议问题