In postgres I have a custom function that will join first and last name.
CREATE FUNCTION full_name(u users) RETURNS varchar
LANGUAGE plpgsql VOLATILE
AS $$
BEGIN
RETURN CONCAT_WS(' ', u.first_name, u.last_name);
END;
$$;
It would be nice if I didn't have to set the the name of the column name and it was determined by the function eg.
eg. here I have to say the column name is full_name
SELECT
full_name(users) as full_name
But it would be nice if it atomically name it full_name
SELECT
full_name(users)
Is this possible to set in a custom function?
As the function parameter is the name of the table, you don't have to pass it.
You can call it like this:
select users.full_name
from users;
Note that you have to prefix the function name with the table name. You can also use an alias, but you still need the prefix.
select u.full_name
from users u;
In that case the column from the result set will be named full_name (the name of the function)
Btw: you don't need PL/pgSQL for the function. A plain SQL will likely be faster for this. Eespecially when you declare it stable instead of volatile - the it can be inlined and the overhead of calling the function is eliminated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With