1

I have a table with subscription values and want to calculate the time when the subscription expires.

I use the following statement:

SELECT contractType, paymentReceivedOn FROM payments WHERE id=21
AND IF (contractType = 'abo3', ADDDATE(paymentReceivedOn, 'INTERVAL 3 MONTH') AS expiryDate, 0)

My above statement works if I leave out the AS expiryDate part, however, then I cannot seem to get the result from the caculation of ADDDATE out.

How can I adjust my query so that it gives me the expiryDate based on paymentReceivedOn + 3 months?

1 Answer 1

1

Because its not part of the selection

    SELECT contractType, paymentReceivedOn,
IF (contractType = 'abo3', ADDDATE(paymentReceivedOn, INTERVAL 3 MONTH), NULL)  
AS expiryDate FROM payments WHERE id=21

Should probably work

(removed quotes from round the interval)

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks BugFinder, your code works really well. What if I wanted to find abo6 which is 6 months into the future? I tried to put in another if but that did not work? Thanks again.
I used a very ugly way that seems to work: SELECT IF (contractType = 'abo3', ADDDATE(paymentReceivedOn, 'INTERVAL 3 MONTH'), IF (contractType = 'abo6', ADDDATE(paymentReceivedOn, 'INTERVAL 6 MONTH'), IF (contractType = 'abo12', ADDDATE(paymentReceivedOn, 'INTERVAL 12 MONTH'), NULL))) AS expiryDate FROM payments WHERE id=21
In what way didnt it work? on face value that looks like it should work.
Hmm.. I just tried your statement again and realized that the fault was with the statement. It gives me an expiryDate, but it is the same date as the paymentReceivedOn. Any ideas?
The little '' are the problem. Could you please edit your answer and I'll tick it off. Thanks again and sorry for the confusion.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.