Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a view column out of separate columns

In our database, we have a system set up to keep track of applications. We have a bool column that indicates whether or not the application is approved. Then there's another column that indicates whether or not the application is denied. If neither column is true, then the application is considered to be pending.

Is there any easy way to merge those into one value (like say a tinyint or maybe a string that says "approved", "denied", or "pending") in a view? Or is this going to require something like a Table-valued function?

UPDATE: It's difficult to choose an answer choose since they were all helpful. I'll go with baldy's since he posted first.

like image 274
Jason Baker Avatar asked Dec 10 '22 22:12

Jason Baker


1 Answers

You can use a case statement like this:

select case 
  when Approved = 1 then 'Approved'
  when Denied = 1 then 'Denied'
  else 'Pending'
  end 'Status'
like image 95
BenR Avatar answered Dec 13 '22 14:12

BenR