I am selecting data from one table that have percentage sign in some column names like:

| name | val% | vale2% | 

This table is ofted use with select * from table, but in this case I need only the field names with percentage sing, when I run a select in query tool of pgadmin this notice this error:

ERROR:  syntax error at or near "as"
LINE 1: SELECT val% as value, val2% as value2 from table

How I can run this query and get only val% and val% without the rest of fields.

NOTE: I can change the database structure, only query it data

  • Have you tried SELECT [val%] as value, [val2%] as value2 from table? – RDFozz Mar 6 '17 at 16:51
  • 2
    Have you tried quoting the names, like SELECT "val%" as value, "val2%" as value2 from ...? – mustaccio Mar 6 '17 at 16:54
  • @RDFozz SELECT "val%" as value...works for me, thank you – xzegga Mar 6 '17 at 17:03
  • In general, you should avoid such column names if you can help it. In my experience, it only results in confusion when something does not work because of the lack of quoting. – dezso Mar 6 '17 at 19:24

Moved from comment to answer, as it seemed to resolve the OP's problem:

It sounds like the actual column names are causing your problem. If so, then you need to quote them somehow. Depending on the settings for your database:

SELECT "val%" as value, "val2%" as value2 from table

may work. In some SQL variants, you can use square brackets [] to quote column names that conflict with language keywords and operators:

SELECT [val%] as value, [val2%] as value2 from table
| improve this answer | |
  • do you know how I can do dot notation when there is a space?, example: x.val but with percentage sign and spage between both? return x['val %']; – xzegga Mar 6 '17 at 19:00
  • The same quoting that worked with a % should work with a space: SELECT "val %" as value, "val 2 %" as value2 from table (assuming postgresql allows it at all, which it must if that's the column name). – RDFozz Mar 6 '17 at 19:11
  • sorry I have explained bad, I am using GraphQL, and I need to get a fieldname with x.myfield but my field have spaces beetween val and % and x."val %" get an error in graphql editor – xzegga Mar 6 '17 at 19:14
  • I don't know GraphQL, so I can't really help you there. Not clear if the problem is with retrieving the data youo want for postgresql with GraphQL, or with using the same field names. If the problem is the field names you get back from postgresql, then alias them (as in your example above, where the column names in the results would be "value" and "value2". – RDFozz Mar 6 '17 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.