Convert from "Y" or "N" to Bit

I have a table with a Direct column of type char (1). These values ​​are: "Y" or "N" or "NULL". I am creating a view and I want the value to be converted to 0 or 1 bit of the type. Now it is an INT type. How can I do it?

Below is the code:

CASE WHEN Direct = 'Y' THEN (SELECT 1)
WHEN Direct <> 'Y' THEN (SELECT 0) END AS DirectDebit

EDIT: How can I make sure the column type is of type BIT?

+5
source share
3 answers

This will give you your beat.

CAST(CASE WHEN Direct = 'Y' THEN 1 ELSE 0 END AS BIT) AS DirectDebit
+7
source

See if this works:

SELECT CASE WHEN Direct = 'Y' THEN 1 ELSE 0 END FROM YOURTABLE
+4
source
SELECT CASE Direct
WHEN 'Y' THEN '1' 
WHEN 'N' THEN '0' 
ELSE '0'
END as DirectDebit
FROM TableName

... must work.

+1
source

All Articles