1

I want the output of SQL statement based on some if else condition.

For Ex:

select salary from EMP

EMP table has a column DOJ.

Now I want the output as '-' if the DOJ is less than 90 days.

How can i do this ?

4
  • CASE is your friend Commented Mar 19, 2014 at 6:59
  • Thanks friend. I hope this will work with multiple columns in result as well. Commented Mar 19, 2014 at 7:02
  • I am using SQL server Commented Mar 19, 2014 at 7:06
  • 1
    What datatype is DOJ? Is it a date and do you want to test 90 days from now? Commented Mar 19, 2014 at 7:17

4 Answers 4

2

In sql-server

 select *,case when DATEDIFF(DAY,DOJ,getdate()) <=90 then '-' else null end as mytest
    from tbale
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT CASE WHEN (DATEDIFF(DAY,DOJ,getdate()) < 90) then '-' else cast(Salary as varchar(15)) end as Salary
FROM EMP

Comments

0
select IIF ( DOJ < 90 , '-', salary ) as FormattedSalary
from EMP

1 Comment

You might have to cast salary as text, I don't know the format of that column of course.
0

For most of database servers you should use CASE but MySQL offers a shortcut:

SELECT IF(DOJ<90, '-', salary) FROM EMP;

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.