I have a column IH_Date
with data type Date
, which stores full date information including day. User will enter only from & to (from month, from year, to month, to year)
. The query should return the respective results.
I write this query with some online help it works fine but it is not an efficient & it is slow sometime. Does anyone have better solution.
Help will be appreciated.
SELECT *
FROM tbl_item_history IH
WHERE YEAR(IH.IH_Date) * 100 + MONTH(IH.IH_Date)
BETWEEN 2022 * 100 + 03
AND 2023 * 100 + 02
ORDER BY IH.IH_DATE ASC
1条答案
按热度按时间n3ipq98p1#
Avoid converting every row of data into year and month values. Instead of converting the data to suit your parameter types, convert the parameters values into dates, and I suggest DATEFROMPARTS for this. This way an index on
IH_Date
can be leveraged by your query.Note you may need to add 1 month to the latter date so that the "To Month" is "inclusive". i.e.