转换varchar值“。”时转换失败。数据类型为int

时间:2020-03-13 07:08:37

标签: sql sql-server database type-conversion

我有一个如下查询:

SELECT activity_code, 
       activity_name, 
       Count(*) 
       Total_Response, 
       yearnumber, 
       Cast(Avg(Cast(Isnull([response], 0) AS NUMERIC)) AS DECIMAL(10, 2)) 
       [Ratings] 
FROM   (SELECT code         [Activity_Code], 
               activityname [Activity_Name], 
               enddt.yearnumber, 
               CASE 
                 WHEN Isnumeric(asmt.userresponse) = 1 THEN asmt.userresponse 
                 ELSE 0 
               END          [Response] 
        FROM   (SELECT userid, 
                       questionid, 
                       activityid, 
                       attemptenddateid, 
                       CASE 
                         WHEN Isnumeric(userresponse) = 1 THEN userresponse 
                         ELSE '0' 
                       END AS UserResponse 
                FROM   factassessment) asmt 
               INNER JOIN dimuser usr 
                       ON usr.id = asmt.userid 
               INNER JOIN (SELECT 
                                               id, 
                                  Cast(questionidentifier AS VARCHAR(1000)) AS 
                                               QuestionIdentifier 
                           FROM   dimquestion) ques 
                       ON ques.id = asmt.questionid 
               INNER JOIN dimactivity act 
                       ON act.id = asmt.activityid 
               INNER JOIN dimdate enddt 
                       ON enddt.dateid = asmt.attemptenddateid 
        WHERE  ( act.code LIKE 'U-%' 
                  OR act.code LIKE 'CC-%' ) 
               AND usr.empcntry <> 'administrator' 
               AND enddt.yearnumber IN ( 2015, 2016, 2017, 2018, 
                                         2019, 2020, 2021, 2021, 2022 ) 
               AND ques.questionidentifier = 'course_content' 
               AND asmt.userresponse NOT IN ( '0', '-1' )) tab 
GROUP  BY activity_code, 
          activity_name, 
          yearnumber 
ORDER  BY activity_code 

在执行上述代码时,出现以下错误:

[SQLServer]转换varchar值'。时转换失败。至 数据类型为int。

我尝试如下修复它,但是仍然出现相同的错误:

SELECT activity_code, 
       activity_name, 
       Count(*) 
       Total_Response, 
       yearnumber, 
       Cast(Avg(Cast(Isnull([response], 0) AS NUMERIC)) AS DECIMAL(10, 2)) 
       [Ratings] 
FROM   (SELECT code         [Activity_Code], 
               activityname [Activity_Name], 
               enddt.yearnumber, 
               CASE 
                 WHEN Isnumeric(asmt.userresponse + 'e0') = 1 THEN 
                 asmt.userresponse 
                 ELSE 0 
               END          [Response] 
        FROM   (SELECT userid, 
                       questionid, 
                       activityid, 
                       attemptenddateid, 
                       CASE 
                         WHEN Isnumeric(userresponse + 'e0') = 1 THEN 
                         userresponse 
                         ELSE '0' 
                       END AS UserResponse 
                FROM   factassessment) asmt 
               INNER JOIN dimuser usr 
                       ON usr.id = asmt.userid 
               INNER JOIN (SELECT 
                                               id, 
                                  Cast(questionidentifier AS VARCHAR(1000)) AS 
                                               QuestionIdentifier 
                           FROM   dimquestion) ques 
                       ON ques.id = asmt.questionid 
               INNER JOIN dimactivity act 
                       ON act.id = asmt.activityid 
               INNER JOIN dimdate enddt 
                       ON enddt.dateid = asmt.attemptenddateid 
        WHERE  ( act.code LIKE 'U-%' 
                  OR act.code LIKE 'CC-%' ) 
               AND usr.empcntry <> 'administrator' 
               AND enddt.yearnumber IN ( 2015, 2016, 2017, 2018, 
                                         2019, 2020, 2021, 2021, 2022 ) 
               AND ques.questionidentifier = 'course_content' 
               AND asmt.userresponse NOT IN ( '0', '-1' )) tab 
GROUP  BY activity_code, 
          activity_name, 
          yearnumber 
ORDER  BY activity_code 


我仍然遇到相同的错误。帮助解决此转换错误。

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,则可以将该错误的原因简化为以下语句,该语句返回Conversion failed when converting the varchar value '.' to data type int.错误:

SELECT CASE WHEN ISNUMERIC(asmt.UserResponse) = 1 THEN asmt.UserResponse else 0 END [Response]
FROM (
    SELECT CASE WHEN ISNUMERIC('.') = 1 THEN '.' ELSE '0' END AS UserResponse
) asmt

最好考虑以下内容:

  • 来自ISNUMERIC('.')的结果为1,来自内部CASE的结果为文本.。但是,外部CASE会失败,因为来自CASE语句的return type是结果表达式中类型集中的最高优先级类型,因此在这种情况下.是隐式转换为int
  • 不信任ISNUMERIC()函数。请改用TRY_CONVERT()

您可以尝试使用下一种方法来解决此错误:

SELECT 
   CASE 
       WHEN TRY_CONVERT(int, asmt.UserResponse) IS NOT NULL THEN TRY_CONVERT(int, asmt.UserResponse) 
       ELSE 0 
   END [Response]
FROM (
    SELECT 
       CASE 
          WHEN TRY_CONVERT(int, '.') IS NOT NULL THEN TRY_CONVERT(int, '.') 
          ELSE 0 
       END AS UserResponse
) asmt