Mat
Mat

Reputation: 65

How do I check if a date is between several ranges?

I have a few date ranges:

01/01/2010 - 03/01/2010
05/01/2010 - 06/01/2010
08/01/2010 - 16/01/2010

Is it possible in SQL to check if a date like 02/01/2010 is between any of these ranges?

Upvotes: 2

Views: 197

Answers (1)

Devart
Devart

Reputation: 121922

Try this one -

DECLARE @temp TABLE
(
      DateFrom DATETIME
    , DateTo DATETIME
)

INSERT INTO @temp (DateFrom, DateTo)
VALUES 
    ('20100101', '20100103'),
    ('20100105', '20100106')

IF EXISTS(
     SELECT 1
     FROM @temp
     WHERE '20100102' BETWEEN DateFrom AND DateTo
)
     PRINT '2010-01-02 between any of these ranges'

Upvotes: 4

Related Questions