MySQL is the last day of the quarter (the most efficient way)

I have seen several other examples of this for SQL, but I am looking especially for MySQL.

This is the code that I have (which works, but I find it extremely inefficient). I use an arbitrary date "2011-05-15", which should and should be returned "2011-06-30".

DATE_SUB( DATE_ADD( CONCAT( YEAR( CURDATE() ), '-01-01' ), INTERVAL QUARTER('2011-05-15') QUARTER ), INTERVAL 1 DAY ) 

What is the best way to do this?

+6
source share
1 answer

In general, the built-in functions in MySQL very fast compared to other things like disk and I / O, so they have little effect on performance.

Perhaps you could save a few milliseconds by eliminating the string conversion:

 SELECT MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE()) QUARTER - INTERVAL 1 DAY 

but, again, I would not even worry about such an optimization.

+7
source

Source: https://habr.com/ru/post/894333/


All Articles