J
JoJo Sharp via AccessMonster.com
Please help with using Access’ iif on SQL server. The following query works
perfect on Access but NOT on SQL server. I tried to use CASE to replace iif
to no avail, possibly because iif stmt could be equivalent to null. Thanks a
lot in advance!!
SELECT DISTINCT tblOrder.id,
tblOrder.dt_ordered, tblPlan.plan,
iif(tblPlan.id is Not Null,'y','n') AS covered
FROM tblOrder LEFT JOIN tblPlan
ON (tblOrder.dt_ordered Between tblPlan.start_dt And tblPlan.end_dt)
AND (tblOrder.id=tblPlan.id);
The tables and sql results sample as following:
tblOrder tblPlan
id dt_ordered start end plan
1 2/5/2004 1 1/1/2004 2/28/2004 HP
1 3/5/2004 3 6/1/2004 12/31/9999 PC
2 4/5/2004
3 5/5/2004
Desirable sql result from the qry
id dt_filled plan covered
1 2/5/2004 HP y
1 3/5/2004 n
2 4/5/2004 n
3 5/5/2004 n
I used iif statement to create a new field called “covered” and mark it as
following:
If tblOrder has matching id in tblPlan and its dt_ordered is between
tblPlan’s start and end date, then mark field “covered” as ‘Y’;
If tblOrder has mataching id in tblPlan BUT its dt_ordered NOT between
tblPlan’s start and end date, then mark field “covered” as ‘N”;
If tblOrder has NO mataching id in tblPlan then mark field “covered” as ‘y”;
Please help!
perfect on Access but NOT on SQL server. I tried to use CASE to replace iif
to no avail, possibly because iif stmt could be equivalent to null. Thanks a
lot in advance!!
SELECT DISTINCT tblOrder.id,
tblOrder.dt_ordered, tblPlan.plan,
iif(tblPlan.id is Not Null,'y','n') AS covered
FROM tblOrder LEFT JOIN tblPlan
ON (tblOrder.dt_ordered Between tblPlan.start_dt And tblPlan.end_dt)
AND (tblOrder.id=tblPlan.id);
The tables and sql results sample as following:
tblOrder tblPlan
id dt_ordered start end plan
1 2/5/2004 1 1/1/2004 2/28/2004 HP
1 3/5/2004 3 6/1/2004 12/31/9999 PC
2 4/5/2004
3 5/5/2004
Desirable sql result from the qry
id dt_filled plan covered
1 2/5/2004 HP y
1 3/5/2004 n
2 4/5/2004 n
3 5/5/2004 n
I used iif statement to create a new field called “covered” and mark it as
following:
If tblOrder has matching id in tblPlan and its dt_ordered is between
tblPlan’s start and end date, then mark field “covered” as ‘Y’;
If tblOrder has mataching id in tblPlan BUT its dt_ordered NOT between
tblPlan’s start and end date, then mark field “covered” as ‘N”;
If tblOrder has NO mataching id in tblPlan then mark field “covered” as ‘y”;
Please help!