如何比较sql server中忽略秒的两个日期

时间:2018-03-15 06:09:52

标签: sql-server tsql datetime

我在SQL Server中有两个日期

@dt1 = 2018-03-15 11:12:10
@dt2 = 2018-03-15 11:12:45

我想要(@dt1 = @dt2)

这种情况应该是真的。

简而言之,我想忽略秒,只考虑日期和时间&分钟。

如何在SQL Server中执行?

5 个答案:

答案 0 :(得分:3)

所有这些舍入值的答案都忽略了我们可以只询问两个值之间的分钟差异,如果它是0,我们知道它们在同一分钟内发生:

DATEDIFF

这是有效的,因为DATEDIFF(minute,'10:59:59','11:00:01')计算过渡,这通常与“人类”解释相反 - 例如<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <main> <section> <form> <label for="txtFullName">Full Name</label> <input type="text" id="txtFullName"> <label for="txtUsername">Username</label> <input type="text" id="txtUsername"> <label for="txtEmail">Email</label> <input type="email" id="txtEmail"> <label for="txtPsw">Password</label> <input type="password" id="txtPsw"> <input type="button" id="btnRegister" value="Register"> </form> </section> <section> <label for="txtLoginEmail">Email</label> <input type="email" id="txtLoginEmail"> <label for="txtLoginPsw">Password</label> <input type="password" id="txtLoginPsw"> <input type="button" id="btnLogin" value="Login"> </section> </main> <script> 'use strict'; document.getElementById("btnRegister").addEventListener("click", function(){ let registerAttempt = new HandleRegister(); registerAttempt.saveRegister(); //validateLogin(); }); document.getElementById("btnLogin").addEventListener("click", function(){ //let loginAttempt = new HandleLogin(); //loginAttempt.checkLogin(); }); class HandleRegister{ constructor(){ this.fullName= document.getElementById('txtFullName').value; this.userName= document.getElementById('txtUsername').value; this.email= document.getElementById('txtEmail').value; this.password= document.getElementById('txtPsw').value; //alert (`${this.fullName} ${this.userName} ${this.email} ${this.password}`); } saveRegister(){ var storage = window.localStorage; alert (`${this.fullName} ${this.userName} ${this.email} ${this.password}`); if (typeof(Storage) !== "undefined") { if (storage.fullName == undefined){ storage.fullName= this.fullName; alert('saved'+this.fullName); }else{ alert('fullname already saved'); } if (storage.userName == undefined){ storage.userName= this.userName; alert('saved'+this.userName); }else{ alert('userName already saved'); } if (storage.password == undefined){ storage.password= this.password; alert('saved'+this.password); }else{ alert('fullname already saved'); } if (storage.email == undefined){ storage.email= this.fullName; alert('saved'+this.email); }else{ alert('email already saved'); } alert ('saving completed'); }else{ alert("your browser don\'t support localstorage"); } } } </script> </body> </html> 将返回1而不是0,尽管时间间隔仅为2秒 - 但这正是您正在寻找的解释,其中日期时间内的所有较小单位都将被忽略。

答案 1 :(得分:1)

这应该适合你。

@dt1 = SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, yourcolumn), 0)
@dt2 = SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, yourcolumn), 0)

IF @dt1 = @dt2
BEGIN

END

答案 2 :(得分:1)

将日期时间向下舍入到分钟:

DATEDIFF(MINUTE, @d1, @d2)

当且仅当值与分钟不同时才会出现这种情况。

答案 3 :(得分:0)

@dt1withoutsecs = DATETIMEFROMPARTS(year(@dt1), month(@dt1), day(@dt1), DATEPART(hh,@dt1), DATEPART(mi,@dt1), 0, 0)
@dt2withoutsecs = DATETIMEFROMPARTS(year(@dt2), month(@dt2), day(@dt2), DATEPART(hh,@dt2), DATEPART(mi,@dt2), 0, 0)

你应该可以比较这两个

编辑:我去实际测试了它(sqlfiddle今天不想工作):

test

答案 4 :(得分:-1)

这是您的解决方案: How can I truncate a datetime in SQL Server?

简而言之,这取决于您的版本。在SQL Server 2008中有cast(getDate as date)