Michael Winther
Michael Winther

Reputation: 381

SQL statement to find if dates is between two dates

With a SQL statement, I want to find the entries for a single day.

Consider the following entries:

When I want to find the entries for the day:

I tried using different '<' '>' operators and the BETWEEN keyword, but couldn't get the right syntax, as it's a timeperiode I want to find, within another timeperiode.

Upvotes: 0

Views: 120

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1271231

I think you want:

where @date <= endAt and
      dateadd(day, 1, @date) > startat

This returns any timespan that includes any time on the @date.

Upvotes: 0

GMB
GMB

Reputation: 222722

Consider:

select count(*)
from mytable
where 
    @mydate >= cast(startAt as date) 
    and @mydate < dateadd(day, 1, cast(endAt as date))

Demo on DB Fiddle - you can play around with the value of @mydate to test the 3 scenarios.

Upvotes: 2

Related Questions