Search code examples
sql-server-2005t-sqlcomparerelationaldateadd

DATEADD - How do i make comparisons


I am trying to check if the second attempt(u can imagine anything like password attempt, download attempt etc) is made within a time limit(@window) starting from attempt1 time(@start). Why does this report a Syntax error when other datetime comparisons work with relational operators

declare @start datetime
declare @window INT
select @start = GETDATE(),@window = 10

select CAST((DATEADD(MINUTE,@window,@start) <= @start) as BIT)

Expected output was something like 0 but i get Incorrect syntax near '<'.


Solution

  • Try this:

    SELECT CASE WHEN DATEADD(MINUTE,@window,@start) <= @start THEN 1 ELSE 0 END