1

I have an interesting situation. my Table has a numeric field called "Discount". It's an integer. My table has another boolean field called "isPercent".

I would like to SELECT the discount field, let's assume the value is 50.

If isPercent is TRUE, I'd like to output it as "50%", else I'd like to output it cast to Money "$50".

How do I 'conditional select' this?

1 Answer 1

1

You already have the boolean value stored. Simply do an if on it and concat the correct sign :


MySQL

select if(isPercent, concat(discount, '%'), concat('$', discount)) from ...

PostgreSQL

SELECT CASE 
  WHEN isPercent THEN discount || '%'
  ELSE '$' || discount END
FROM ...
Sign up to request clarification or add additional context in comments.

Comments

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.