Rod
Rod

Reputation: 15477

form sql to return bool to c#

what's the best way to form my sql so it will return a bool into a c# variable? Or is just easier to inspect the scalar value on the return of an .ExecuteScalar function?

Basically, want to know if row exists or not.

select COUNT(COLUMN_NAME) from INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='Customers' 
AND
COLUMN_NAME = 'birthdate'
AND
DATA_TYPE = 'datetime'

for example, if the row exists above return true.

Upvotes: 2

Views: 412

Answers (1)

tawman
tawman

Reputation: 2498

You can use a CASE check on the result of the COUNT() to force 0 or 1:

SELECT CASE WHEN COUNT(COLUMN_NAME) = 0 THEN 0 ELSE 1 END AS IsColumPresent
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='Customers' 
AND
COLUMN_NAME = 'birthdate'
AND
DATA_TYPE = 'datetime'

This will return 0 or 1

Upvotes: 5

Related Questions