ReneK ReneK - 2 years ago 88
SQL Question

If-Else in View?

I have this code in my C# application:

EmployeeFirstName = gc.Key.CommissionType.GetValueOrDefault() == CommissionTypeTypes.Personal ? gc.Select(ec => ec.EmployeeFirstName).FirstOrDefault() : string.Empty, //c.EmployeeFirstName,

and I want to have it in SQL

That means if my column "CommissionType" is the char 'P', take the value from EmployeeFirstName (and connect it), if not, make it null.

Columns are: EmployeeFirstName, CommissionType.

Answer Source

You can't have IF-ELSE in a view. It is a single select statement. But you could use a case expression for this.

case when CommisionType = 'P' then EmployeeFirstName end
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download