¿Cómo selecciono un 1 como bit en una vista sql-server?
Quiero crear una vista en la que seleccione algo como lo siguiente:
select id, name, 1 as active
from users
Sin embargo, quiero que el campo activo, que estoy creando en la instrucción select (no existe en la tabla), sea un campo de bits. ¿Hay alguna manera de hacer esto?
32
4 answers
Puede utilizar el operador CONVERT.
SELECT id, name, CONVERT(bit, 1) AS active
FROM users
CAST o CONVERT funcionará.
51
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-10-18 21:16:20
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-10-18 21:16:20
select id, name, CAST(1 AS bit) as active
from users
1
es la pantalla para un bit verdadero. ¿Qué estás tratando de lograr?
Haciendo
select CAST('true' AS bit) as active
Devuelve 1
también.
10
Author: Dustin Laine,
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-10-18 21:18:23
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-10-18 21:18:23
Sí, lo lanzas a bit:
select id, name, cast(1 as bit) as active
from users
Esto también puede ser útil para mejorar el rendimiento cuando se compara con un valor de bit en algunas situaciones:
select id, name
from users
where active = cast(1 as bit)
(En este ejemplo podría no hacer ninguna diferencia práctica, pero he visto una diferencia real en consultas más complicadas.)
4
Author: Guffa,
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-10-18 21:17:09
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-10-18 21:17:09
select id, name, Convert(bit, 1) as active
from users
Es lo que probablemente quieras hacer.
1
Author: msarchet,
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-10-18 21:49:35
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-10-18 21:49:35