我在尝试运行SQL查询时遇到此问题。无法弄清楚转换到" int"正在发生。有人可以指出我正确的方向吗?
SELECT TOP 100 PERCENT
a.S1 as "SegNumber",
a.S2 as "SegmentName",
a.S3 as "Description",
a.S4 as "SegmentValue"
FROM
(
SELECT 3 as "S1",
LTRIM(RTRIM(b.SUMMARY_ACCT)) + '_' + 'Accounts' as "S2",
b.ACCOUNT_DESC as "S3",
'^' + LTRIM(RTRIM(c.SUMMARY_ACCT)) + '_' + 'Accounts' as "S4"
FROM TEST.dbo.GLCHARTSUM b
inner join TEST.dbo.GLCHARTSUM c ON c.PARENT_OBJ_ID = b.SUMRY_ACCT_ID
UNION ALL
SELECT 3 as "S1",
LTRIM(RTRIM(c.SUMMARY_ACCT)) + '_' + 'Accounts' as "S2",
c.ACCOUNT_DESC as "S3",
LTRIM(RTRIM(convert(varchar(100),b.ACCOUNT))) as "S4"
FROM TEST.dbo.GLCHARTDTL b
INNER JOIN TEST.dbo.GLCHARTSUM c ON c.SUMRY_ACCT_ID = b.SUMRY_ACCT_ID
UNION ALL
SELECT 2 as "S1",
LTRIM(RTRIM(ACCT_UNIT)) + '_' + 'Unit' as "S2" ,
DESCRIPTION as "S3",
'^' + LTRIM(RTRIM(ACCT_UNIT)) + '_Unit' as "S4"
FROM TEST.dbo.GLNAMES
WHERE POSTING_FLAG ='S'
UNION ALL
SELECT 2 as "S1",
LTRIM(RTRIM(ACCT_UNIT)) + '_' + 'Unit' as "S2",
DESCRIPTION as "S3",
LTRIM(RTRIM(ACCT_UNIT)) as "S4"
FROM TEST.dbo.GLNAMES
WHERE POSTING_FLAG ='P'
UNION ALL
SELECT 3 as "S1",
MATRIX_CAT + Substring(MX_VALUE,1,5) AS "S2",
MX_VALUE as "S3",
ACCOUNT AS "S4"
FROM TEST.dbo.GLACCMXVAL b
INNER JOIN TEST.dbo.GLCHARTDTL c ON b.OBJ_ID = c.OBJ_ID
) a
WHERE 1 = 1
ORDER BY a.S2
错误:
Msg 245,Level 16,State 1,Line 1
转换varchar值时转换失败' ^ ASSETS_Accounts'数据类型int。
答案 0 :(得分:3)
您之前看到此错误的原因是您上一次UNION ALL
。 ACCOUNT
值为INT
,但其他S4
列为VARCHAR
。
您应该将ACCOUNT
列投放到VARCHAR
...
UNION ALL
SELECT 3 as "S1",
MATRIX_CAT + Substring(MX_VALUE,1,5) AS "S2",
MX_VALUE as "S3",
CONVERT(VARCHAR (100), ACCOUNT) AS "S4"
...