其他分享
首页 > 其他分享> > select exists

select exists

作者:互联网

is it possible to select EXISTS directly as a bit?

Question :  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%') as bit)

Seems like it should be doable, but lots of things that should work in SQL don't ;) I've seen workarounds for this (SELECT 1 where... Exists...)

but it seems like I should be able to just cast the result of the exists function as a bit and be done with it.

 

Answer:

If you must return a conditional bit 0/1 another way is to:

SELECT CAST(
   CASE WHEN EXISTS(SELECT * FROM theTable where theColumn like 'theValue%') THEN 1 
   ELSE 0 
   END 
AS BIT)

Or without the cast:

SELECT
   CASE
       WHEN EXISTS( SELECT 1 FROM theTable WHERE theColumn LIKE 'theValue%' )
            THEN 1 
       ELSE 0 
   END
 

参考资料:

       sql - is it possible to select EXISTS directly as a bit? - Stack Overflow

  Subqueries in Google Standard SQL  |  Cloud Spanner  |  Google Cloud

  MySQL Exists - javatpoint

  

标签:exists,EXISTS,select,bit,SELECT,like
来源: https://www.cnblogs.com/appinn/p/16381702.html