Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

transforming from 'Y' or 'N' to bit

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?

like image 875
Riz Avatar asked Mar 27 '10 16:03

Riz


3 Answers

This will get you your bit..

CAST(CASE WHEN Direct = 'Y' THEN 1 ELSE 0 END AS BIT) AS DirectDebit
like image 197
CResults Avatar answered Oct 30 '22 23:10

CResults


See if this works:

SELECT CASE WHEN Direct = 'Y' THEN 1 ELSE 0 END FROM YOURTABLE
like image 37
Leniel Maccaferri Avatar answered Oct 31 '22 00:10

Leniel Maccaferri


SELECT CASE Direct
WHEN 'Y' THEN '1' 
WHEN 'N' THEN '0' 
ELSE '0'
END as DirectDebit
FROM TableName

... should work.

like image 44
Jason Avatar answered Oct 31 '22 01:10

Jason