如何将整数数据类型联合与整数数据类型列进行硬编码的空白值

问题描述 投票:0回答:1
select id , '' as employee_id from table1
union
select id , emp_id as employee_id from table2

但是我从两个加入的联合查询中得到了employee_id数据类型不兼容的错误。那么如何解决这个问题。

emp_id是数字数据类型

sql sybase
1个回答
0
投票

如果要字符串,请转换为字符串:

select id , cast('' as varchar(255)) as employee_id from table1
union all
select id , cast(emp_id as varchar(255) as employee_id from table2;

通常,您将使用NULL而不是'',因为它与更多类型兼容:

select id , NULL as employee_id from table1
union all
select id , emp_id as employee_id from table2;

注意,我将union更改为union allunion会导致删除重复项的开销。仅在您希望增加开销的情况下使用它。

© www.soinside.com 2019 - 2024. All rights reserved.