我正在尝试将两个字段连接成一个,第一个是varchar,第二个是int。我在将数据类型varchar转换为数据类型int时出现“转换失败”错误,而且我对sql不熟悉,无法自行解决为什么会这样。
代码如下:
concat(polpolicynumber,' - ',convert(varchar(10),poltermsequence))为RefValue
我也不确定为什么它会尝试将其转换为int。保持它作为varchar是我想要的。
任何帮助都将非常感激。
非常感谢!
以下是有帮助的整个代码:
declare @rundate date = '10-02-2015'
select @rundate as RunDate, 'FINYS' as Source, 'Agency' as ReferenceTable, 'AgcID' as RefID, agcid as RefValue,
agcname as LastName, '' as MiddleName, '' as FirstName
from COHPROD_Policy..agency
where AgcExpirationDate is NULL or agcexpirationdate > @rundate
union
select @rundate as RunDate, 'FINYS' as Source, 'Agent' as ReferenceTable, 'AgtID' as RefID, agtid as RefValue,
agtname as LastName, '' as MiddleName, '' as FirstName
from COHPROD_Policy..agent
where AgtTerminationDate is NULL or AgtTerminationDate > @rundate
union
select @rundate as RunDate, 'FINYS' as Source, 'Policy' as ReferenceTable, 'PolNumber' as RefID,
concat(polpolicynumber, ' ', 'Term', ' ', convert(nvarchar(10),poltermsequence)) as RefValue,
isnull(connamelast,'') as LastName, isnull(connamemiddle,'') as MiddleName, isnull(connamefirst,'') as FirstName
from COHPROD_Policy..policy p
join COHPROD_Policy..ContactXref cox on coxpolid = polid
join COHPROD_Policy..Contact con on conid = coxconid
join cohprod_policy..policystatus on pospolid = polid
where pospolicystatus = 'active'
答案 0 :(得分:1)
错误似乎不是来自concat
,而是来自union
。您在第一个查询中有agcid as RefValue
(我假设是INT
),第二个查询中有agtid as RefValue
(我还假设是INT
)。
然后您在上一个查询中有concat(polpolicynumber, ' ', 'Term', ' ', convert(nvarchar(10),poltermsequence)) as RefValue
,显然是VARCHAR
,不能直接使用之前的列进行UNION
。