mssql 2008 datediff与分钟

时间:2014-06-22 10:10:58

标签: sql .net sql-server sql-server-2008

我正在使用这个:

DECLARE @startdate datetime2 = '08:00:00.000';
DECLARE @enddate datetime2 = '08:04:21.000'; 
SELECT DATEDIFF(minute, @startdate, @enddate) as minute; 

我得到了输出 分钟:4

但是当我在下面输入时:

DECLARE @startdate datetime2 = '08:00:00.000';
DECLARE @enddate datetime2 = '07:59:21.000'; 
SELECT DATEDIFF(minute, @startdate, @enddate) as minute; 

然后我得到输出:-1

所以我不想要输出:-1。它必须是空白的输出

谢谢

1 个答案:

答案 0 :(得分:3)

您可以使用CASE语句应用if @startdate is greater than @enddate then null else datediff

等逻辑
SELECT CASE WHEN @startdate > @endate THEN NULL 
       ELSE DATEDIFF(minute, @startdate, @endate) END