Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

postgres select non existing field as empty string

This might be a stupid question. Imaging following table in Postgres:

create table scientist (id integer, firstname varchar(100), lastname varchar(100));
insert into scientist (id, firstname, lastname) values (1, 'albert', 'einstein');

A simple select of all fields:

select  id, firstname, lastname   scientist;

results in:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

is firstname lastname
1 albert einstein

Is there some way to add a kind of non-existing empty virtual field f.e. age?
Pseudo Code:

 select 
    id, 
    firstname, 
    lastname,
    age as '' -- my non existing field that should show up in the results
    from 
    scientist;

that the result looks like?

is firstname lastname age
1 albert einstein

>Solution :

It’s the other way round: as defines a column alias, so

'' as age

will work.

However, I’d be quite surprised about a column named age in a result that is a character value, not an integer. If you want a typed value, you can also use null::int as age

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading