将数据类型nvarchar转换为数值时出错

将数据类型nvarchar转换为数值时出错

我有一个观点:

SELECT
u.display_name AS usuario,
g.parent_name AS grupo,
pr.pkey,
REPLACE(
    CONVERT (VARCHAR, ji.CREATED, 111),
    '/',
    '-'
) AS fecha,
CAST (ji.issuetype AS INT) AS issuetype,
a.customvalue AS aplicativo,
m.customvalue AS modulo
FROM
jiraissue AS ji
JOIN project pr ON pr.ID = ji.PROJECT
JOIN (
SELECT
    ms.*
FROM
    cwd_membership ms
INNER JOIN cwd_group gp ON (
    gp.ID = ms.parent_id
    AND group_name IN (
        'Grupo QA 1',
        'Grupo QA 2',
        'Grupo QA 3',
        'BH Seguros Homo'
    )
)
) g ON g.lower_child_name = ji.REPORTER
JOIN cwd_user u ON g.lower_child_name = u.user_name
JOIN (
SELECT
    ISSUE,
    customvalue
FROM
    customfieldvalue v
INNER JOIN customfield f ON (
    f.ID = v.customfield
    AND f.cfname = 'Aplicativo'
)
INNER JOIN customfieldoption o ON (o.ID = v.STRINGVALUE)
) a ON (a.ISSUE = ji.ID)
JOIN (
SELECT
    ISSUE,
    customvalue
FROM
    customfieldvalue v
INNER JOIN customfield f ON (
    f.ID = v.customfield
    AND f.cfname = 'Módulo'
)
INNER JOIN customfieldoption o ON (o.ID = v.STRINGVALUE)
) m ON (m.ISSUE = ji.ID)
WHERE
ji.issuetype IN (9, 11, 12, 13, 14, 15)
GROUP BY
ji.issuetype,
pr.pkey,
g.parent_name,
u.display_name,
REPLACE(
    CONVERT (VARCHAR, ji.CREATED, 111),
    '/',
    '-'
),
a.customvalue,
m.customvalue

这给了我这样的东西:
usuario             grupo      pkey     fecha       issuetype  aplicativo
----------------------------------------------------------------------------------
Ricardo A. Casares  Grupo QA 1  GD123   2012-11-23  12  Act-creditos-scheduler  ABM_Suc-backend

然后,当我尝试查询该 View 时,让我们说一个简单的查询:
SELECT * FROM view
WHERE pkey LIKE '%GD123%'

在某些列中,我收到“将数据类型nvarchar转换为数值时出错”
但是在其他一些列中,例如“aplicativo”,它可以正常工作。

为什么会这样呢?

最佳答案

问题在于此分配:

o.ID = v.STRINGVALUE

请更正它会解决问题。解决该问题的一种可能方法是使用ISNUMERIC,例如
o.ID = CASE WHEN ISNUMERIC(v.STRINGVALUE) = 1 THEN v.STRINGVALUE ELSE -1 END

(在ELSE中,您可以使用其他一些保证不与表'o'连接的数字,即可以使用0)

10-08 14:03