Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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 ?
CASE
In sql-server
select *,case when DATEDIFF(DAY,DOJ,getdate()) <=90 then '-' else null end as mytest from tbale
Add a comment
SELECT CASE WHEN (DATEDIFF(DAY,DOJ,getdate()) < 90) then '-' else cast(Salary as varchar(15)) end as Salary FROM EMP
select IIF ( DOJ < 90 , '-', salary ) as FormattedSalary from EMP
For most of database servers you should use CASE but MySQL offers a shortcut:
SELECT IF(DOJ<90, '-', salary) FROM EMP;
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
CASEis your friend