Boolean Expressions in SQL Select list

前端 未结 4 799
死守一世寂寞
死守一世寂寞 2021-02-02 05:07

I want to create a SQL Select to do a unit test in MS SQL Server 2005. The basic idea is this:

select \'Test Name\', foo          


        
相关标签:
4条回答
  • 2021-02-02 05:48

    Use the case construct:

    select 'Test Name', 
        case when foo = 'Result' then 1 else 0 end 
        from bar where baz = (some criteria)
    

    Also see the MSDN Transact-SQL CASE documentation.

    0 讨论(0)
  • 2021-02-02 05:49
    SELECT 'TestName', 
        CASE WHEN Foo = 'Result' THEN 1 ELSE 0 END AS TestResult
    FROM bar 
    WHERE baz = @Criteria
    
    0 讨论(0)
  • 2021-02-02 05:54

    Use CASE:

    SELECT 'Test Name' [col1],
      CASE foo
        WHEN 'Result' THEN 1
        ELSE 0
      END AS [col2]
    FROM bar
    WHERE baz = (some criteria)
    
    0 讨论(0)
  • 2021-02-02 06:04

    You can also use:

    select 
        'Test Name', 
        iif(foo = 'Result', 1, 0)
    from bar 
    where baz = (some criteria)
    

    I know this was asked a while back, but I hope this helps someone out there.

    0 讨论(0)
提交回复
热议问题