hackerdot
hackerdot

Reputation: 60

SQL time diff 2 hour conditon

DECLARE @CheckUserID int, @LastLog datetime

SELECT 
    @CheckUserID = COUNT(strUserID) 
FROM 
    USERDATA 
WHERE 
    strUserID = @NewCharID

SELECT 
    @LastLog = LogDate 
FROM
    _LOG 
WHERE
    AccountID = @AccountID 
ORDER BY 
    LogDate DESC

IF @CheckUserID = 0 AND @LastLog < DATEADD(hour, -2, GETDATE()) 
   OR @LastLog IS NULL
BEGIN
    ....
END

I want add 2 hours condition

Upvotes: 0

Views: 51

Answers (1)

Alexei - check Codidact
Alexei - check Codidact

Reputation: 23088

I think the problem is related to operator precedence:

IF @CheckUserID = 0 AND @LastLog < DATEADD(hour, -2, GETDATE()) 
   OR @LastLog IS NULL

should be replaced with

IF (@CheckUserID = 0 AND (@LastLog < DATEADD(hour, -2, GETDATE())
   OR @LastLog IS NULL))

Upvotes: 1

Related Questions