bit null ,0

Vineet S 225 Reputation points
2024-05-27T16:52:36.8833333+00:00

Hey Team,

have column1 datatype (bit,null) so it only takes values like 0 and 1 but i want true , false as well...how to get that in same datatype or in different one

Azure SQL Database
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,080 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,579 questions
{count} votes

Accepted answer
  1. Olaf Helper 42,486 Reputation points
    2024-05-27T17:08:41.8833333+00:00

    have column1 datatype (bit,null) so it only takes values like 0 and 1 but i want true , false as we

    The data type bit can only contain numeric 0 and 1; "false"/"true" are strings, that's not possible. You have to convert it like

    SELECT CASE WHEN value = 'false' THEN 0 ELSE 1 END AS BitValue
    
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 103.5K Reputation points MVP
    2024-05-27T21:34:37.32+00:00

    SQL Server actually supports implicit conversion of the strings 'true' and 'false' to 1 and 0:

    CREATE TABLE #bit(b bit NULL)
    go
    INSERT #bit(b) VALUES('true'), ('false')
    go
    SELECT b FROM #bit
    
    0 comments No comments