SQL View does not accept Subclause

This question already has an answer here:

  • How do I perform an IF…THEN in an SQL SELECT? 22 answers

  • 试试下面的代码:

    SELECT CAST(CASE WHEN MasterId IS NULL THEN 0 ELSE 1 END AS BIT) [HasMaster]
    FROM entries
    

    Try this,

    SELECT (CASE WHEN MasterId IS NOT NULL THEN 1 ELSE 0 END) AS HasMaster 
    FROM  entries
    

    There is no Boolean values in SQL Server , so you can use '0' and '1' in CASE expression here.


    Question: I need to get a boolean field in a view from a string field in the table. The boolean field should be True if the string is filled

    Answer: use case

    select case when MasterId is null then 0 else 1 end
    from table_name
    
    链接地址: http://www.djcxy.com/p/94350.html

    上一篇: 如何在if中使用select

    下一篇: SQL视图不接受子条款