is it possible to select EXISTS directly as a bit?

后端 未结 9 1170
轻奢々
轻奢々 2020-12-02 09:09

I was wondering if it\'s possible to do something like this (which doesn\'t work):

select cast( (exists(select * from theTable where theColumn like \'theValue

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 09:18

    SELECT CAST(COUNT(*) AS bit) FROM MyTable WHERE theColumn like 'theValue%'
    

    When you cast to bit

    • 0 -> 0
    • everything else -> 1
    • And NULL -> NULL of course, but you can't get NULL with COUNT(*) without a GROUP BY

    bit maps directly to boolean in .net datatypes, even if it isn't really...

    This looks similar but gives no row (not zero) if no matches, so it's not the same

    SELECT TOP 1 CAST(NumberKeyCOlumn AS bit) FROM MyTable WHERE theColumn like 'theValue%'
    

提交回复
热议问题