Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 625
Kev Avatar asked Dec 29 '10 17:12

Kev


People also ask

How do I add another column to a select query?

The basic syntax for adding a new column is as follows: ALTER TABLE table_name ADD column_name data_type constraints; The SQL ALTER TABLE add column statement we have written above takes four arguments. First, we specify the name of our table.

What is dummy column?

A dummy column is one which has a value of one when a categorical event occurs and a zero when it doesn't occur. In most cases this is a feature of the event/person/object being described.

What is dummy in SQL?

DUMMY is a public synonym for system table "SYS"."DUMMY" and what you should receive from there is via SQL is one row with a value 'X' for one column called "DUMMY" as well if select with SELECT * FROM "DUMMY"; Unless I missunderstood your question.


2 Answers

Yes, it's actually a constant value.

SELECT id, '~' AS EndOfcol FROM Main WHERE id > 40 
like image 171
bobs Avatar answered Oct 08 '22 06:10

bobs


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  
like image 24
HLGEM Avatar answered Oct 08 '22 07:10

HLGEM