Hello DBA's. I have a query that selects all the insured policies from the previous year, as shown below:
SQL
select MotorCINo,
IssuanceType,
VehicleType,
VehiclePower,
VehicleChasisNo,
InsuranceType,
InsuranceOption,
PeriodFrom,
PeriodTo
from tblMotorInsurance_eCI
where YEAR(PeriodFrom) = YEAR(DATEADD(year,-1,GETDATE())) and PeriodTo 'Short Term'
order by PeriodFrom desc
The output is also shown below:

How do I get the list of motor vehicles that did not renew the policy this year? Thank you in advance.


Jignesh KumarRank #91 · 22.9K reputationPosted Nov 3, 2023, 12:00 AM EDT
Hello Hold,
Please check for PeriodFrom, it should be in current year and not null.
select MotorCINo, IssuanceType, VehicleType, VehiclePower, VehicleChasisNo, InsuranceType, InsuranceOption, PeriodFrom, PeriodTo from tblMotorInsurance_eCI where (YEAR(PeriodFrom) = YEAR(DATEADD(year,-1,GETDATE())) or PeriodFrom IS NULL) and IssuanceType <> 'Short Term' order by PeriodFrom descJes SieRank #1,285 · 1.3K reputationPosted Nov 3, 2023, 12:06 AM EDT
Thank you so much, Jignesh. I tested your recommended query; however, I found that the motor vehicle that was renewed this year exists in the result set.
Jes SieRank #1,285 · 1.3K reputationPosted Nov 3, 2023, 12:49 AM EDT
If querying the whole year, it takes so long for the data to display. So, I made a between-period. Below is my query and so far it serves the purpose:
select MotorCINo, IssuanceType, VehicleType, VehiclePower, VehicleChasisNo, InsuranceType, InsuranceOption, PeriodFrom, PeriodTo from tblMotorInsurance_eCI where (PeriodFrom BETWEEN '2022-06-01' AND '2023-11-03') and PeriodTo 'Short Term' order by PeriodFrom desc