SQL: ¿Es posible agregar una columna ficticia en una instrucción select?


Necesito agregar una columna ficticia a una instrucción select simple en ciertas circunstancias:

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

Author: Kendrick, 2010-12-29

4 answers

Sí, en realidad es un valor constante.

SELECT id, '~' AS EndOfcol
FROM Main
WHERE id > 40
 40
Author: bobs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-29 17:48:16

A veces es posible que desee convertir el tipo de datos de la constante, especialmente si planea agregar otros datos más tarde:

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

Esto es especialmente útil si desea agregar una columna NULL y luego averiguar la información que entra en ella como NULL se emitirá como int automáticamente.

SELECT id,  cast(NULL as varchar(20))  AS Myfield FROM Main WHERE id > 40 
 13
Author: HLGEM,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-30 18:09:01

Sí, es posible que pueda ser constante o puede ser condicional

SELECT id, '~' EndOfcol FROM Main WHERE id > 40
 2
Author: shankhan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-29 17:52:15

Una solución fácil es agregar una columna como esta:

Select Id, EndOfcol default '~', space(2) as Dummy from Main where id > 40
 -1
Author: steve,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-10 09:32:19