我有一个查询,我必须从 varchar 列中提取一些数字字段。当我在 select 语句中进行一些替换和子字符串并将其转换为 bigint 时,一切正常,但是当我在 join 中使用相同的转换时,它会抛出错误“将数据类型 varchar 转换为 bigint 时出错”。这怎么可能?
select CAST(
case when CHARINDEX('/',f.BML,1)>0
then substring(replace(replace(replace(replace(replace(replace(replace(replace(replace(f.BML,'A',''),'B',''),'C',''),'+',''),',',''),'S',''),'H',''),'P',''),'¸','') ,1,CHARINDEX('/',BML,1)-1)
else replace(replace(replace(replace(replace(replace(replace(replace(replace(f.BML,'A',''),'B',''),'C',''),'+',''),',',''),'S',''),'H',''),'P',''),'¸','')
end as bigint)
from TableN n join TableO o on
n.Id=o.Id
join TableF f on
f.OId=o.OId and
substring(cast(n.RJ as varchar(10)),1,3)=substring(CAST(f.MT AS varchar(10)),1,3) and
CAST(
case when CHARINDEX('/',f.BML,1)>0 then substring(replace(replace(replace(replace(replace(replace(replace(replace(replace(f.BML,'A',''),'B',''),'C',''),'+',''),',',''),'S',''),'H',''),'P',''),'¸','') ,1,CHARINDEX('/',f.BML,1)-1)
else replace(replace(replace(replace(replace(replace(replace(replace(replace(f.BML,'A',''),'B',''),'C',''),'+',''),',',''),'S',''),'H',''),'P',''),'¸','')
end as bigint) =n.mbr
order by n.Ident