相关疑难解决方法(0)

有什么方法可以检查两个日期时间是否在TSQL中的同一个日历日?

这是我遇到的问题:我有一个大型查询需要比较where子句中的日期时间,以查看两个日期是否在同一天.我当前的解决方案很糟糕,是将日期时间发送到UDF以将它们转换为同一天的午夜,然后检查这些日期是否相等.当涉及到查询计划时,这是一场灾难,几乎所有联接中的UDF或where子句都是如此.这是我的应用程序中唯一一个我无法根除函数并为查询优化器提供实际可用于查找最佳索引的地方之一.

在这种情况下,将函数代码合并回查询似乎是不切实际的.

我想我在这里缺少一些简单的东西.

这是参考功能.

if not exists (select * from dbo.sysobjects 
              where id = object_id(N'dbo.f_MakeDate') and               
              type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
  exec('create function dbo.f_MakeDate() returns int as 
         begin declare @retval int return @retval end')
go

alter function dbo.f_MakeDate
(
    @Day datetime, 
    @Hour int, 
    @Minute int
)
returns datetime
as

/*

Creates a datetime using the year-month-day portion of @Day, and the 
@Hour and @Minute provided

*/

begin

declare @retval datetime
set @retval = cast(
    cast(datepart(m, @Day) as varchar(2)) …
Run Code Online (Sandbox Code Playgroud)

sql t-sql sql-server datetime user-defined-functions

23
推荐指数
3
解决办法
4万
查看次数