Convert INT to BIT

I tried the following query

DECLARE @Input INT = 300 DECLARE @Ouput TINYINT SET @Ouput = @Input SELECT @Ouput 

While executing the above statement, I received the following error.

Arithmetic overflow error for tinyint data type, value = 300.

The input value exceeds the limit, so an error is displayed.

I tried another request

 DECLARE @Input INT = 300 DECLARE @Ouput BIT SET @Ouput = @Input SELECT @Ouput 

When I execute the statement, I really wondered: it does not show any errors . If the input value is <> 0 (negative or positive), the output value is always 1 .

+1
sql-server int bit
source share
2 answers

Converting to bit raises a nonzero value to 1.

Sqlserver has the power, or try converting the value implicitly as your input and output type without using the cast or convert function if you did not specify one.

When a conversion error arrives at the default logic or scenerio, it gives a specific type conversion error.

So your tiny int and int length are different, 300 is not suitable for tiny int. Tiny int allows max at a value of 255

Here is the implicit and explicit conversion chart

+2
source share

You get arithmetic overflow when setting the int value to tinyint , because the range of int values โ€‹โ€‹is larger than tinyint (you know. "Small" (in voice)).

Anf, if you use bit that has an O or 1 value, the base value in Computer Basics, and I think that the value you get is 1, because it matters if it is missing, you will get 0.

0
source share

All Articles