I'm modifying a query in Oracle, in which the data from the previous month is needed. At present, the query has a where clause with the following:
...
...
...
WHERE cust_dt BETWEEN TO_DATE('05-01-2022','mm-dd-yyyy) AND TO_DATE('05-31-2022', 'mm-dd-yyyy')
I am modifying the query so that the start date and end date do not need to be manually changed every month to run the query. After doing some research, I came up with the following:
...
...
...
WHERE TO_CHAR(cust_dt, 'MM-YYYY') = TO_CHAR(add_months(sysdate, -1), 'MM-YYYY')
The results I get back are as I want them, but I am curious as to which query will be better performance-wise given a larger set of data. All the posts I saw online used BETWEEN, so I was wondering if there was some reason for this.
I am a complete novice as far as tuning, testing, performance, etc. goes on queries. The actual query itself it fairly complex with several joins, so performance is important. At present, I only have a small amount of test data to work with, so I am limited in what all I can do to find the best result.
So to circle back to my question, which query would be best? The one that uses a BETWEEN, or the one that uses TO_CHAR?