1

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.

1 Answer 1

3

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.

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

3 Comments

Thanks a lot! Just one doubt though, does it support nesting of IIF statements?
Just checked online and it does support nesting! Thanks once again!
IIF is available from SQL Server 2012 onwards. Is there an equivalent statement in SQL Server 2008?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.