使用 Len 进行 SQL 选择 - 转换失败



这是我的查询:

SELECT at.AssetTag, 
dp.Custodian,
CASE WHEN PATINDEX('%_%', dp.Custodian) = '0'
THEN dp.Custodian 
ELSE RIGHT(LTRIM(RTRIM(dp.Custodian)),LEN(LTRIM(RTRIM(dp.Custodian)) - PATINDEX('%_%', dp.Custodian))) END AS FirstName, 
CASE WHEN PATINDEX('%_%', dp.Custodian) = '0' 
THEN '' 
ELSE LEFT(dp.Custodian, PATINDEX('%_%', dp.Custodian)-1) END AS LastName, RIGHT(at.Assettag,3) AS MediaTypeSuffix
FROM tblAssetTracking at (NOLOCK) 
INNER JOIN tblDiscoveryProcessing dp (NOLOCK) ON dp.FKAsset = at.ID 
WHERE dp.Custodian Is Not Null

当我运行它时,我收到一个错误"将 nvarchar 值'XXX'转换为数据类型 int 时转换失败"(xxx 是我必须替换的实际值)。 请问谁能帮忙? 附言我希望该语句正确进入代码块。 显然,我不明白如何正确粘贴代码。

谢谢!

关系。 我不得不删除修剪:

SELECT at.AssetTag, 
dp.Custodian, 
CASE WHEN PATINDEX('%[_]%', dp.Custodian) = '0'
    THEN dp.Custodian 
    ELSE RIGHT(dp.Custodian,LEN(dp.Custodian) - PATINDEX('%[_]%', dp.Custodian))
END AS FirstName, 
CASE WHEN PATINDEX('%[_]%', dp.Custodian) = '0' 
    THEN '' 
    ELSE LEFT(dp.Custodian, PATINDEX('%[_]%', dp.Custodian)-1) 
END AS LastName, 
RIGHT(at.Assettag,3) AS MediaTypeSuffix
FROM tblAssetTracking at (NOLOCK)
INNER JOIN tblDiscoveryProcessing dp (NOLOCK) ON dp.FKAsset = at.ID
WHERE dp.Custodian Is Not Null

相关内容

  • 没有找到相关文章

最新更新