Search code examples
sqlpostgresqljdbctrim

How to avoid postgresql from changing column names when using "select trim"


I run the following query in my PostgreSql version 9.3 (I'm using PgAdmin III):

Select trim(both ' ' from column1) from myTable

it returns the data I want, but it also renames the column name from 'column1' to 'btrim'. I need the original column name because I'm using this query from a Java app. This renaming happens with ALL the columns where I use the Trim function, no matter if I run the query directly in pgAdmin or via my postgres jdbc driver (this is why I think the problem is in postgres itself).

How can I get the data and the columns with their right names when using the Trim function?


Solution

  • Use column aliases:

    Select trim(both ' ' from column1) AS column1 
    from myTable
    

    SqlFiddleDemo

    If you need case sensitive aliases you need to quote them with ".

    Idenfifiers:

    Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other. (The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard, which says that unquoted names should be folded to upper case. Thus, foo should be equivalent to "FOO" not "foo" according to the standard. If you want to write portable applications you are advised to always quote a particular name or never quote it.)