sasori
sasori

Reputation: 5455

how to set the value of the column in a select statement in oracle?

how to change the output value of the column and it's column name if a certain condition was met? let's say i have table A, it has 3 columns id,salutation, name, surname,gender

i can select all of those by

select * from A

so what if I want to output the value of salutation based from what gender the data row is, how to do that in select statement, the pseudo code is like this

select everything, if the gender is Male, then set salutation to Mr. else, set salutation to Ms. but then change the salutation into alias namePrefix

how to do that in pure select statement in oracle?

Upvotes: 1

Views: 11155

Answers (1)

shree.pat18
shree.pat18

Reputation: 21757

A CASE statement seems to be what you need. You can do something like this:

select yourtable.*,
case when gender = 'Male' then 'Mr. ' || name else 'Ms. ' || name end as namePrefix
from yourtable

The CASE statement will help you generate the computed column namePrefix for each row in your table such that either Mr. or Ms. is prefixed based on the value for gender column in that row.

Upvotes: 3

Related Questions