这里我尝试将位值转换为varchar,我在MS sql server
中尝试此查询CASE WHEN cast(ISNULL(O.isTerminated,0)as varchar) = 1 THEN 'Yes' ELSE '' END AS isTerminated.
提供错误“将varchar值'是'转换为数据类型位时转换失败。”
这将是什么解决方案。
答案 0 :(得分:1)
错误不是由您显示的语句引起的,而是由您之后使用isTerminated
别名执行的操作引起的。
您的陈述不会引发任何错误。
DECLARE @isTerminated BIT = 1
SELECT CASE WHEN CAST(ISNULL(@isTerminated, 0) AS VARCHAR) = 1
THEN 'yes'
ELSE ''
END AS isTerminated
但是将别名isTerminated
视为bit
可以。
DECLARE @isTerminated BIT = 1
SELECT *
FROM (SELECT CASE WHEN CAST(ISNULL(@isTerminated, 0) AS VARCHAR) = 1
THEN 'yes'
ELSE ''
END AS isTerminated) t
WHERE isTerminated = 1 -- isTerminated is a VARCHAR at this point
答案 1 :(得分:0)
为什么要将bit
投射到varchar
。你能做到这一点:
CASE WHEN ISNULL(O.isTerminated,0) = 1 THEN 'Yes' ELSE '' END AS isTerminated
修改强>
尝试此示例时。
DECLARE @T TABLE(isTerminated BIT)
INSERT INTO @T
VALUES(1),(NULL),(0)
SELECT
CASE WHEN ISNULL(O.isTerminated,0) = 1 THEN 'Yes' ELSE '' END AS isTerminated
FROM
@T AS O
用您的案例陈述。我没有修改错误。你错过了任何细节吗?