ReneK ReneK - 7 months ago 13
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

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
Comments