Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - BIT data type Issue

I have a table in my SQL Server 2012 as follows,

CREATE TABLE TEST (ID INT,NAME VARCHAR(100),STATUS BIT);

I inserted the data like this, by mistake,

INSERT INTO TEST SELECT 1,'A',2;

When I queried the result, I found the column "STATUS" storing "1". Whatever value I insert instead of "0", it stores "1". I felt if I define a column as BIT then it should not allow inserting any value apart from 0 and 1.

Is there any trace flag that restricts such insertion? I'm using SQL Server 2012 SP3 now.

like image 255
Yashwanth Aluru Avatar asked Feb 29 '16 09:02

Yashwanth Aluru


People also ask

How use bit data type in SQL Server?

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.

Is SQL 32 or 64 bit?

With most versions of Microsoft® SQL Server® there is a 32-bit version and a 64-bit version.

Does SQL Server support UTF 16?

Microsoft SQL Server and Microsoft SQL Server Express do not support UTF-8 at the database level. They support nchar, nvarchar, and ntext to store fixed format Unicode data (UTF-16).

Can a bit datatype be NULL?

Columns with a datatype of bit cannot be NULL and cannot have indexes on them.


1 Answers

Using the Bit Data type.

"The SQL Server Database Engine optimizes storage of bit columns. If there are 8 or less 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.

Converting to bit promotes any nonzero value to 1."

MSDN

like image 154
Harry Avatar answered Sep 20 '22 19:09

Harry