ses
ses

Reputation: 13352

PostgreSQL replace on alias column value

I have a table names_table, where the column user_name has value like "bob_marley" (separated by underscore).

On select I would like to add one more column user_name_display that would exclude "_" from the value.

As result to have "bob marley", "xxx xx", etc.

I try:

select  user_name, user_name as user_name_display, replace(user_name_display,'_',' ') from names_table;

but seems I can not use user_name_display alias in the repalce() function.

Q: What is the way to get a new column with values where with no "_" in it?

Upvotes: 1

Views: 611

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521997

You don't even need to use that alias, just use the column name:

select
    user_name,
    user_name as user_name_display,
    replace(user_name, '_', ' ')
from names_table;

Upvotes: 1

You indeed cannot use an alias in the select list, but you can use the column original name like this:

select  user_name, user_name as user_name_display, replace(user_name ,'_',' ') as user_name_display2 from names_table;

Upvotes: 2

Related Questions