SQL: What is better a Bit or a char(1)

后端 未结 5 532
谎友^
谎友^ 2020-12-17 18:06

Is there any performance difference on retrieving a bit or a char(1) ?

Just for curiosity =]

UPDATE: Suposing i\'m using SQL Server 2008!

5条回答
  •  臣服心动
    2020-12-17 18:37

    a bit and a char(1) will both take a 1 byte to store,assuming you only have 1 bit column in the table, SQL Server will store up tp 8 bit columns in 1 byte. I don't think there is a difference in performance.

    One thing to be aware of is that you can't do sum on a bit column

    CREATE TABLE #test( a BIT)
    
    INSERT #test VALUES (1)
    INSERT #test VALUES (1)
    
    SELECT sum(a) FROM #test
    

    Msg 8117, Level 16, State 1, Line 1
    Operand data type bit is invalid for sum operator.

    you have to convert it first

    SELECT sum(CONVERT(INT,a)) FROM #test
    

提交回复
热议问题