Search code examples
sqlpostgresqlquoting

Write a query to select columns wrapped in single quotes


I am trying to write a select query which should return the column value wrapped in single quote. Say the column (ABC) has

Values: 123
        567

The query should return the

Output: '123'
        '567'

Solution

  • While dealing with numerical data, you can simply concatenate. NULL values stay NULL. But for character data (or similar) that might need escaping, use proper functions.

    quote_nullable() or quote_literal() - depending on whether you have NULL values:

    SELECT quote_nullable(val) AS quoted_val FROM tbl;
    

    Details for quoting: