下面是查询,当不使用convert to varchar时返回0。比较整数值
SELECT
*,
CASE
WHEN Duration IS NULL
THEN CONVERT(VARCHAR, '')
ELSE Duration
END AS Duration
FROM
Contribution
WHERE
CreatedBy = @in_userID AND ContentStatus = @ContentStatus
UNION ALL
SELECT
*,
CASE
WHEN Duration IS NULL
THEN CONVERT(VARCHAR, '')
ELSE Duration
END AS Duration
FROM
Training
WHERE
CreatedBy = @in_userID AND ContentStatusValue = @TrainingStatus
使用全部并集时,不使用转换,大小写将返回0而不是“使用转换时,问题已解决。
有人可以说为什么会这样吗?
答案 0 :(得分:1)
我怀疑您的Duration
类型为INT
,因此您可以进行对话:
SELECT c.col1, c.col2, ISNULL(CAST(c.Duration AS VARCHAR(255)),'') Duration
FROM Contribution c
WHERE c.CreatedBy = @in_userID AND
c.ContentStatus = @ContentStatus
UNION ALL
SELECT t.col1, t.col2, ISNULL(CAST(t.Duration AS VARCHAR(255), '')
FROM Training t
WHERE t.CreatedBy = @in_userID AND
t.ContentStatusValue = @TrainingStatus;
答案 1 :(得分:0)
您还可以使用COALESCE
SELECT *,COALESCE(convert( varchar(255),Duration ),'')
AS Duration FROM Contribution WHERE CreatedBy = @in_userID
and ContentStatus = @ContentStatus
UNION ALL
SELECT *,COALESCE(convert( varchar(255),Duration ),'') AS Duration
FROM Training WHERE CreatedBy = @in_userID
and ContentStatusValue=@TrainingStatus.