Stephane Grenier
Stephane Grenier

Reputation: 15925

SQL - How do I create a SELECT with formatted name

I have the table:

ID, firstname, lastname, companyName, isCompany

How do I run a query such that I can do:

SELECT ID, name FROM myTable ...

where the name is an algorithm along the lines of

if(isCompany)
  name = companyName
else
  name = "lastname, firstname"

Upvotes: 0

Views: 107

Answers (1)

user330315
user330315

Reputation:

You didn't state your DBMS but this is ANSI SQL:

select id,
       case
         when is_company then companyName
         else lastname||', '||firstname
       end as name
from my_table

This assumes that is_company is of type boolean

Upvotes: 2

Related Questions