Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 field. Is there a way to do this?

like image 430
dmr Avatar asked Oct 18 '10 21:10

dmr


People also ask

How do I toggle a bit in SQL?

A simple method to toggle a bit column (MySQL, SQL Server, and others) As you can see, the XOR operator returns “0” if the two arguments match and “1” if they don't. This makes it easy to flip the bit in a boolean column without having to first check it's existing value.

How do I return a bit value in SQL Server?

We need to directly return the calculated BIT value based on condition. So we use the CAST() method available in SQL. We can use the CAST method to cast from one data type to another. You might want to cast a float value to int value for that we use CAST().

How do you write a bit in SQL?

If there are 8 or fewer bit columns in a table, the columns are stored as 1 byte. If there are from 9 up to 16 bit columns, the columns are stored as 2 bytes, and so on. The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0.

What is bit value in SQL?

SQL Server BIT data type is an integer data type that can take a value of 0, 1, or NULL . The following illustrates the syntax of the BIT data type: BIT. SQL Server optimizes storage of BIT columns. If a table has 8 or fewer bit columns, SQL Server stores them as 1 byte.


2 Answers

You can use the CONVERT operator.

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

CAST or CONVERT will work.

like image 151
bobs Avatar answered Oct 06 '22 01:10

bobs


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.

like image 38
Dustin Laine Avatar answered Oct 06 '22 00:10

Dustin Laine