Kev
Kev

Reputation: 323

SQL: Is it possible to add a dummy column in a select statement?

I need to add a dummy column to a simple select statement in certain circumstances:

Select Id, EndOfcol default '~' from Main where id > 40

Upvotes: 30

Views: 86485

Answers (4)

steve
steve

Reputation: 1

An easy solution is to add a column like this:

Select Id, EndOfcol default '~', space(2) as Dummy from Main where id > 40

Upvotes: -1

HLGEM
HLGEM

Reputation: 96590

Sometimes you may want to cast the datatype of the constant especially if you plan to add other data to it later:

SELECT id, cast('~' as varchar(20)) AS EndOfcol FROM Main WHERE id > 40 

This is especially useful if you want to add a NULL column and then later figure out the information that goes into it as NULL will be cast as int automatically.

SELECT id,  cast(NULL as varchar(20))  AS Myfield FROM Main WHERE id > 40 

Upvotes: 16

shankhan
shankhan

Reputation: 6571

Yes, it is possible it can be constant or can be conditional

SELECT id, '~' EndOfcol FROM Main WHERE id > 40

Upvotes: 2

bobs
bobs

Reputation: 22204

Yes, it's actually a constant value.

SELECT id, '~' AS EndOfcol
FROM Main
WHERE id > 40

Upvotes: 49

Related Questions