如何在 sql-server 视图中选择 1 作为位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3963457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:58:58  来源:igfitidea点击:

How do I select a 1 as a bit in a sql-server view?

sqlsql-server-2000

提问by dmr

I want to create a view in which I select something like the following:

我想创建一个视图,在其中选择如下内容:

select id, name, 1 as active
from users

However, I want the active field, which I am creating in the select statement (it doesn't exist in the table), to be a bit field. Is there a way to do this?

但是,我希望我在 select 语句中创建的活动字段(它不存在于表中)是一个位字段。有没有办法做到这一点?

回答by bobs

You can use the CONVERT operator.

您可以使用 CONVERT 运算符。

SELECT id, name, CONVERT(bit, 1) AS active
FROM users

CAST or CONVERT will work.

CAST 或 CONVERT 将起作用。

回答by Dustin Laine

select id, name, CAST(1 AS bit) as active
from users

1is the display for a true bit. What are your trying to achieve.

1是真位的显示。你想达到什么目的。

Doing

正在做

select CAST('true' AS bit) as active

returns 1also.

1也返回。

回答by Guffa

Yes, you cast it to bit:

是的,你把它扔掉了:

select id, name, cast(1 as bit) as active
from users

This can also be useful to improve performance when comparing to a bit value in some situations:

在某些情况下与位值进行比较时,这对于提高性能也很有用:

select id, name
from users
where active = cast(1 as bit)

(In this example it might make no practical difference, but I have seen an actual difference in more complicated queries.)

(在这个例子中,它可能没有实际差异,但我已经看到在更复杂的查询中存在实际差异。)

回答by msarchet

select id, name, Convert(bit, 1) as active
from users

Is what you probably are wanting to do.

是您可能想要做的。