开发者

transforming from 'Y' or 'N' to bit

开发者 https://www.devze.com 2022-12-24 03:48 出处:网络
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

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?


This will get you your bit..

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


See if this works:

SELECT CASE WHEN Direct = 'Y' THEN 1 ELSE 0 END FROM YOURTABLE


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

... should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号