0 votes
1 view
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 (88.2k 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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...