How to set bool value in SQL

后端 未结 4 1795
天涯浪人
天涯浪人 2020-11-30 14:39

I have a column which is bool. How can I set true, false values for that? Here is my query :

Update [mydb].[dbo].[myTable]
SET isTrue =
(
CASE WHEN Name = \         


        
相关标签:
4条回答
  • 2020-11-30 15:25

    Sql server does not expose a boolean data type which can be used in queries.
    Instead, it has a bit data type where the possible values are 0 or 1.
    So to answer your question, you should use 1 to indicate a true value, 0 to indicate a false value, or null to indicate an unknown value.

    Update [mydb].[dbo].[myTable]
    SET isTrue =
    CASE WHEN Name = 'Jason' THEN 
        1
    ELSE 
        0
    END
    
    0 讨论(0)
  • 2020-11-30 15:33

    You need case statement with when and else if not any condition satisfied

    Update [mydb].[dbo].[myTable]
    SET isTrue = ( CASE WHEN Name = 'Jason' 
                       THEN 1 else 0 
                   END)
    
    0 讨论(0)
  • 2020-11-30 15:41

    The query you added will work fine, but it will you have to take care of "FALSE" part as well otherwise it will try to enter NULL in your column. Do you have any default value constrain on isTrue column?

    Update [mydb].[dbo].[myTable]
    SET isTrue =
    (
       CASE WHEN Name = 'Jason' THEN 1 ELSE 0
    END
    )
    
    0 讨论(0)
  • 2020-11-30 15:46

    Use IIF function of sql server

    DECLARE @a int = 45, @b int = 40;  
    DECLARE @a int = 45, @b int = 40;  
    SELECT IIF ( @a > @b, 'TRUE', 'FALSE' ) AS Result; 
    
    Result  
    --------  
    TRUE  
    
    (1 row(s) affected)
    

    For Your Problem

    Update [mydb].[dbo].[myTable]
    SET isTrue = ( Name = 'Jason', 'TRUE', 'FALSE' )
    
    
    
    0 讨论(0)
提交回复
热议问题