Postgresql – How to return a varchar type in a select of a free string. example: Select ‘Test’ as field from table

postgresql

Unlike other databases, PostgreSQL returns a TEXT type by default every time you run a select with a free string. Example:

SELECT 'Text' as FIELD FROM TABLE

in this case above the field FIELD will be returned with type TEXT

Does anyone know if it is possible to change some parameter in PostgreSQL so that it returns a CHAR or VARCHAR type in the above case?

Firebird and Oracle return a CHAR type.

I know this would be solved with a CAST however for this I would have to replicate the same for all SQL's of the system besides having to make using CAST a standard routine of developing new SQL's.

Anyway, does anyone have any idea of ​​anything that might have changed? I'm studying the files pg_cast.h and pg_type.h

Best Answer

Maybe you just need to cast it.

 SELECT CAST( 'Text' as CHARACTER VARYING(99999) ) as FIELD FROM TABLE;

or using an alternative pg-specific casting syntax.

 SELECT 'Text'::varchar as FIELD FROM TABLE;

If you select a string constant and don't cast it to something it has type unknown. but I'm guessing your connection library converts this to the preferred postgres string type 'text'