views:

33

answers:

4

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?

+3  A: 

You can use the CONVERT operator.

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

CAST or CONVERT will work.

bobs
+1  A: 

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

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

Is what you probably are wanting to do.

msarchet
SQL Server doesn't have True and False constants.
bobs
+1  A: 
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.

Dustin Laine