I’ve seen a few other examples of this for SQL, but I am looking particularly for MySQL.
This is the code I have (which works, but I think it’s drastically inefficient). I am using the arbitrary date ‘2011-05-15’ which should and does return ‘2011-06-30’.
DATE_SUB(
DATE_ADD(
CONCAT(
YEAR( CURDATE() ),
'-01-01'
),
INTERVAL QUARTER('2011-05-15') QUARTER
),
INTERVAL 1 DAY
)
What is the better way to do this?
In general, built-in functions in
MySQLare very fast compared to other things like disk and memory I/O so they have little impact on efficiency.You could probably save some milliseconds by getting rid of string conversions:
but, again, I wouldn’t even worry for such optimizations.