Question

How do I do something like the following

SELECT ADDDATE(t_invoice.last_bill_date, INTERVAL t_invoice.interval t_invoice.interval_unit) 
  FROM t_invoice

...where the column t_invoice.last_bill_date is a date, t_invoice.interval is an integer and t_invoice.interval_unit is a string.

What I like about the ADDDATE() function is that if I write a

SELECT ADDDATE('2010-01-31', INTERVAL 1 MONTH)

It conveniently returns the last day of February - 2010-02-28. So it knows that 1 MONTH means 28 days for Feb (29 on leap year), 31 if odd number month, 30 if even number month. I would like to preserve this feature in my query.

Basically, I want to determine a person's next bill date based on his desired billing frequency. Is there a better way to achieve this?

Was it helpful?

Solution

SELECT t_invoice.last_bill_date +  INTERVAL 
   CASE t_invoice.interval_unit 
    WHEN 'minute' THEN t_invoice.interval 
    WHEN 'hour' THEN t_invoice.interval*60
    WHEN // etc...
    END MINUTE as col1
 FROM t_invoice

OR

SELECT 
   CASE t_invoice.interval_unit 
    WHEN 'minute' THEN t_invoice.last_bill_date +  INTERVAL t_invoice.interval MINUTE
    WHEN 'hour' THEN t_invoice.last_bill_date +  INTERVAL t_invoice.interval HOUR
    WHEN // etc...
    END as col1
 FROM t_invoice
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top