表A有一个列X,它是由表B中Y和Z列(它们都是浮点数)的串联组成的int。我想以类似于这样的方式连接表A和B:
select *
from tableA a inner join tableB b
on a.X = b.cast(concat(cast(b.Y as varchar), cast(b.Z as varchar)) as integer
除此之外,显然,我的例子没有正确完成。
答案 0 :(得分:4)
你可以这样做:
select *
from tableA a
inner join tableB b
on a.X = cast(cast(b.Y as varchar) + cast(b.Z as varchar) as int)
如果你的任何浮点数都有小数点,转换为int
将会失败。
,例如,这有效:
declare @f1 as float
declare @f2 as float
set @f1 = 1
set @f2 = 7
select cast(cast(@f1 as varchar) + cast(@f2 as varchar) as int)
输出: 17
但这不是:
declare @f1 as float
declare @f2 as float
set @f1 = 1.3
set @f2 = 7
select cast(cast(@f1 as varchar) + cast(@f2 as varchar) as int)
输出: Conversion failed when converting the varchar value '1.37' to data type int.
答案 1 :(得分:3)
听起来像计算列的作业,然后它可以索引。
答案 2 :(得分:2)
你能在名为x的b中创建另一列,其中包含你想要的值吗? 然后加入A很容易。