Brendan Gooden Brendan Gooden - 1 month ago 10
SQL Question

SQL Query Date between, if date column not null

I have a query as follows

SELECT ajd.Make, ajd.Model, ajd.Filter, ajd.Notes, ajd.CatamacPartNumber, ajd.FromDate, ajd.ToDate
FROM ApplicationJapData ajd
WHERE ajd.Model LIKE '%FVR34%'
AND FromDate <= '20140701' AND ToDate >= '20140701'


What this query does is select rows based on
Model
containing certain search string, and where supplied date is between columns
FromDate
and
ToDate
.

The problem is that sometimes ToDate is
null
, because it is up to current date (For instance,
FromDate
=
1/1/2015
, and
ToDate
is
null
because it is
1/1/12
-
Present
)

So if I supply a value of 12-12-2015, I would still like to return rows if it meets the
FromDate
condition.

However, I can't use
OR
in
FromDate <= '20140701' OR ToDate >= '20140701'

because there might be
ToDate
of
1-6-15
, in which case it would be incorrect with supplied date of
12-12-2015


Any help is much appreciated, and please ask if anything is unclear or needs clarifying!

Answer
AND FromDate <= '20140701' 
AND (ToDate is null OR ToDate >= '20140701')