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

7

42

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 and fill in.

example:

SELECT
hat,
shoe,
boat,
somevalue = 0 as placeholder
FROM
objects

then I would loop through this query later and fill in the placeholder

in this example someValue is not a field in objects, I need to fake it. I am doing this in coldfusion and using two datasources to complete one query. I have tried the space() function but have been unable to get it to work.

Thanks.

Cavicorn answered 3/3, 2011 at 19:50 Comment(3)
Yes. You can hardcode a value to be returned.Isolationism
The trouble is the equals sign I believe. See my answer below.Chainsmoke
Yes, but the somevalue = 0 as placeholder syntax doesn't make sense. Simply 0 as placeholder as suggested by Galz is the way to go.Rusert
W
101
SELECT
    hat,
    shoe,
    boat,
    0 as placeholder
FROM
    objects

And '' as placeholder for strings.

Wingo answered 3/3, 2011 at 19:52 Comment(1)
How about for type timestamptz?Animatism
C
10

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
Chainsmoke answered 3/3, 2011 at 19:52 Comment(0)
M
4

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.

Manvell answered 12/3, 2015 at 18:40 Comment(1)
Yes, when using select...into to create a new table it is important to be more explicit about data type and capacity. Otherwise, the db will use the empty string to "guess" about the target column's properties. In the case of an empty string - it will almost certainly come up with the wrong answer.Alarise
N
2

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) :)

Nummulite answered 4/3, 2011 at 20:32 Comment(0)
P
0
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
Podgy answered 25/2, 2016 at 9:14 Comment(1)
This is a repeat of the existing answers.Alarise
M
0

Thank you, in PostgreSQL this works for boolean

SELECT
hat,
shoe,
boat,
false as placeholder
FROM
objects
Marriage answered 25/1, 2019 at 12:50 Comment(0)
F
0

If you do not want to initiate your column with zeros or empty strings but with NULL, the correct way to do this in PostgreSQL would be to cast the Null values to whatever type you want your new column to have:

SELECT
       hat
       , shoe
       , boat
       , null::int as myint
       , null::float as myfloat
       , null::bool as mybool
       , null::varchar as myvarchar
FROM 
   object
Fyrd answered 5/2 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.