Search code examples
sqlselectdb2

How to return a constant value in select in IBM DB2?


I want to have a constant value in an sql response:

SELECT
 'constant_value' AS 'contant_column_name', -- does not work in ibm db2
  real_column1,
  real_column2
FROM real_table

This code works in MariaDB/MySQL and MS SQL databases but not in IBM DB2.


Solution

  • use double-quotes around the new column name, not single quotes, i.e.

    SELECT
     'constant_value' AS "constant_column_name",
      real_column1,
      real_column2
    FROM real_table
    

    Works for me on Db2-LUW. If you still get an error, post your Db2-server platform variant and version.

    You can also omit the double-quotes, in which case the new column-name must obey identifer rules and will be forced to uppercase.