1

在这里,我尝试将位值转换为 varchar,我在 MS sql server 中尝试此查询

CASE WHEN cast(ISNULL(O.isTerminated,0)as varchar) = 1 THEN 'Yes' ELSE '' END AS   isTerminated.

给出错误“将 varchar 值 'Yes' 转换为数据类型位时转换失败。”

这将是什么解决方案。

4

2 回答 2

2

该错误不是由您显示的语句引起的,而是由您isTerminated之后对别名所做的操作引起的。

您的陈述原样不会引发任何错误。

DECLARE @isTerminated BIT = 1
SELECT CASE WHEN CAST(ISNULL(@isTerminated, 0) AS VARCHAR) = 1 
            THEN 'yes' 
            ELSE  '' 
       END AS isTerminated

但是将别名isTerminated视为 a 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
于 2012-05-14T08:22:03.067 回答
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

用你的案例陈述。我不修改错误。您是否遗漏了任何细节?

于 2012-05-14T08:23:36.660 回答