My database table: (Purchase-Details)

Table with value:

I want to find out the list of upcoming exipry medicines.
my Query:
SELECT * FROM PURCHASE_DETAILS
WHERE CONVERT(date,EXPIRY) BETWEEN CONVERT(DATE,'21-Dec-2021') AND CONVERT(DATE,'31-Oct-2022')
but error occur.
Please suggest me.




Nitin SontakkeRank #264 · 9.6K reputationPosted Dec 23, 2021, 6:24 AM EST
To begin with, not sure why the dates are being stored as nvarchar. Not a very good practise, I would say.
Anyway.
Because value stored in [Expirty] column is not a valid date, I will have to assume that it is First of every month. Calculating last day of given month in column would be complicated if not impossible.
I propose following:
select *
from [dbo].[purchase_details]
where convert(date, '01-' + [Expiry]) between convert(date, '21-Dec-2021') and convert(date, '31-Oct-2022')
Hope it helps!
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Dec 20, 2021, 11:19 PM EST