Home » excel » Convert Excel 'if' function to SQL query

Convert Excel 'if' function to SQL query

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a function like this:

if(a>b,if(b>c,a,b),c)

This follows the syntax if(condition,true,false). I was looking for a way to convert this into an SQL query like so:

select case 
  when a>b then 
     case 
          when b>c then a 
          else b 
     end 
  else c 
end

I thought of splitting the input and passing it on my own, but I was hoping there was a better way to do this.
Any help is appreciated!
Thanks in advance.

How to&Answers:

You can use IIF function as well… Please check the link for description.

So for you it would be something like SELECT IIF (a > b, IIF ( B > C, A, B ), C ) AS Result;

Sytax is not tested but would work.