views:

69

answers:

3

Hello,

I have a table which has a column called Direct of type char(1). It's values are either 'Y' or 'N' or NULL. I am creating a view and I want the value to be transformed to either 0 or 1 of type bit. Right now it's of type INT. How do I go about doing this?

Following 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?

+3  A: 

See if this works:

SELECT CASE WHEN Direct = 'Y' THEN 1 ELSE 0 END FROM YOURTABLE
Leniel Macaferi
wow you're fast! how can I make sure the column is of type bit?
bit (Transact-SQL): An integer data type that can take a value of 1, 0, or NULL. This way I think the values you get from the above query fit correctly in a column of type bit.
Leniel Macaferi
@Leniel - without casting/converting the response from CASE you cannot be sure of the type it will return
CResults
+1  A: 
SELECT CASE Direct
WHEN 'Y' THEN '1' 
WHEN 'N' THEN '0' 
ELSE '0'
END as DirectDebit
FROM TableName

... should work.

Jason
+1  A: 

This will get you your bit..

CAST(CASE WHEN Direct = 'Y' THEN 1 ELSE 0 END AS BIT) AS DirectDebit
CResults
perfect. That column now returns True/False