How can I create a blank/hardcoded column in a sql query?

前端 未结 6 832
刺人心
刺人心 2020-12-15 15:39

I want have a query with a column that is a hardcoded value not from a table, can this be done? I need it basically as a placeholder that I am going to come back to later a

相关标签:
6条回答
  • 2020-12-15 16:19

    Thank you, in PostgreSQL this works for boolean

    SELECT
    hat,
    shoe,
    boat,
    false as placeholder
    FROM
    objects
    
    0 讨论(0)
  • 2020-12-15 16:22

    This should work on most databases. You can also select a blank string as your extra column like so:

    Select
      Hat, Show, Boat, '' as SomeValue
    From
      Objects
    
    0 讨论(0)
  • 2020-12-15 16:25

    For varchars, you may need to do something like this:

    select convert(varchar(25), NULL) as abc_column into xyz_table
    

    If you try

    select '' as abc_column into xyz_table
    

    you may get errors related to truncation, or an issue with null values, once you populate.

    0 讨论(0)
  • 2020-12-15 16:28
    SELECT
        hat,
        shoe,
        boat,
        0 as placeholder -- for column having 0 value    
    FROM
        objects
    
    
    --OR '' as Placeholder -- for blank column    
    --OR NULL as Placeholder -- for column having null value
    
    0 讨论(0)
  • 2020-12-15 16:29
    SELECT
        hat,
        shoe,
        boat,
        0 as placeholder
    FROM
        objects
    

    And '' as placeholder for strings.

    0 讨论(0)
  • 2020-12-15 16:33

    The answers above are correct, and what I'd consider the "best" answers. But just to be as complete as possible, you can also do this directly in CF using queryAddColumn.

    See http://www.cfquickdocs.com/cf9/#queryaddcolumn

    Again, it's more efficient to do it at the database level... but it's good to be aware of as many alternatives as possible (IMO, of course) :)

    0 讨论(0)
提交回复
热议问题