Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么不';sql日期不能比较吗?_Sql_Sql Server_Tsql_Stored Procedures_Sql Server 2014 - Fatal编程技术网

为什么不';sql日期不能比较吗?

为什么不';sql日期不能比较吗?,sql,sql-server,tsql,stored-procedures,sql-server-2014,Sql,Sql Server,Tsql,Stored Procedures,Sql Server 2014,我正在比较日期和时间,但有时它不能正常工作 代码: Select nit_no, workno, convert(datetime,convert(varchar,w.ExpiryDate,106) + ' ' + w.ExpiryTime), getdate() from works w where NIT_No= 3594 and WorkNo=1 and convert(datetime,convert(varchar,w.ExpiryDate,106) + ' ' + w.Expir

我正在比较日期和时间,但有时它不能正常工作

代码:

Select nit_no, workno, convert(datetime,convert(varchar,w.ExpiryDate,106) +  ' ' + w.ExpiryTime), getdate()
from works w
where NIT_No= 3594 and WorkNo=1
and convert(datetime,convert(varchar,w.ExpiryDate,106) +  ' ' + w.ExpiryTime) <= getdate()
convert(datetime,convert(varchar,w.ExpiryDate,106) +  ' ' + w.ExpiryTime)= 2017-06-08 16:50:54.000  
getdate()= 2017-06-08 17:50:54.000
ExpiryDate为DATE类型,ExpiryTime为VARCHAR

它无法正常工作,getdate()小于另一个表达式,并且仍然返回数据

更新: 我要做的是将ExpiryDate和ExpiryTime与当前日期时间(即Getadate())进行比较,如果到期日期和时间小于当前日期时间,则不应显示它。是否可以尝试以下操作:

convert(datetime,convert(varchar,w.ExpiryDate,121) +  ' ' + w.ExpiryTime, 121 )

与其转换为字符并转换回,只需将
expirytime
转换为
time
数据类型,然后将该时间添加到转换为
datetime
expirytate
,如下所示:

select *
  , expirydatetime = dateadd(millisecond
        ,datediff(millisecond,0,convert(time(7),w.expirytime))
        ,convert(datetime,w.expirydate)
      )
from works w
where dateadd(millisecond
        ,datediff(millisecond,0,convert(time(7),w.expirytime))
        ,convert(datetime,w.expirydate)
      ) < getdate()

rextester演示:

您不必显式地转换日期以将其与字符串进行比较。顺便说一句,请解释您正在尝试做什么。样本数据和期望的结果很有用。@GordonLinoff没有什么特别的。我发布的内容就是我要做的,将ExpiryDate和time与current进行比较,如果它小于current datetime,则不应显示它,否则displayedit的值不小于。更多。17:50在16:50之后。这就是它返回数据的原因。@Covert 2017-06-08 17:50:54.000>=2017-06-08 16:50:54.000这就是为什么这是真的。我不知道你不明白什么
select *
  , expirydatetime2 = dateadd(millisecond
        ,datediff(millisecond,0,convert(time(7),w.expirytime))
        ,convert(datetime2(7),w.expirydate)
      )
from works w
where dateadd(millisecond
        ,datediff(millisecond,0,convert(time(7),w.expirytime))
        ,convert(datetime2(7),w.expirydate)
      ) < sysdatetime()