Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

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 to function as a bit and be done with it.

1 Answer

0 votes
by (119k points)

You can use the following query in case you want to return a conditional bit 0/1:

SELECT CAST(

   CASE WHEN EXISTS(SELECT * FROM theTable where theColumn like 'theValue%') THEN 1

   ELSE 0

   END

AS BIT)

or you can use the following SQL query without using CAST function:

SELECT

   CASE

       WHEN EXISTS( SELECT 1 FROM theTable WHERE theColumn LIKE 'theValue%' )

            THEN 1

       ELSE 0

   END

You can learn writing complex SQL queries by registering for this SQL Course by Intellipaat.

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
2 answers
0 votes
3 answers

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...