RockYoClock
RockYoClock

Reputation: 41

SQL: Verify if date is between two dates

How can i check if an date value equals a date within the summer period.

I'd expect it to be something like:

SELECT Row WHERE Orderdate BETWEEN '01/01/%' AND '31/08/%'

However i'm not allowed to use wildcards in a BETWEEN Clause, right?

/edit

That works, thanks both u guys! i'm sorry i can only pick one correct answer.

Upvotes: 0

Views: 107

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1271003

You want the "summer" (undefined) period. Most SQL databases would allow you to do:

WHERE month(Orderdate) in (6, 7, 8)

Otherwise, you can use extract(month from OrderDate) in (6, 7, 8)).

This assumes that the month period is defined as June, July, and August. And, there are many other database-specific solutions.

Upvotes: 1

D Stanley
D Stanley

Reputation: 152634

i'm not allowed to use wildcards in a BETWEEN, right?

No but you could do this:

SELECT Row WHERE MONTH(Orderdate) BETWEEN 1 AND 8

Note that adding an index on MONTH(Orderdate) will significantly speed up this query since otherwise it will have to do an INDEX SCAN or TABLE SCAN.

Upvotes: 1

Related Questions