Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF Condition in an SQL query

I'm a newbie to SQL Server. Please help me to write the following Logic in a query.

If getnow() > today 4 PM
Then
    SELECT *
    FROM table
    WHERE MailDate is Tomorrow
Else
    SELECT *
    FROM table
    WHERE MailDate is Today
like image 521
user1345260 Avatar asked Apr 19 '12 22:04

user1345260


2 Answers

select *
from table
where DATEDIFF(day, GETDATE(), maildate) = case when
    DATEPART(hour, GETDATE()) >= 16 then 1 else 0
end
like image 60
RobIII Avatar answered Sep 27 '22 20:09

RobIII


IF datepart(hh, getdate()) >= 16
    BEGIN
        SELECT *
        FROM table
        WHERE DateDiff(day, getdate(), MailDate) = 1
    END
ELSE
    BEGIN
        SELECT *
        FROM table
        WHERE DateDiff(day, getdate(), MailDate) = 0
    END
like image 32
Taryn Avatar answered Sep 27 '22 22:09

Taryn