开发者

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

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 fiel开发者_C百科d. Is there a way to do this?


You can use the CONVERT operator.

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

CAST or CONVERT will work.


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

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

Doing

select CAST('true' AS bit) as active

returns 1 also.


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.)


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

Is what you probably are wanting to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜