CASE语句:不一致的转换失败错误 - varchar到int

时间:2016-05-03 18:55:01

标签: sql sql-server sql-server-2014

    SELECT
   CASE
      WHEN Employees.first_name IS NULL
      OR Employees.first_name = 'x' THEN Employees.last_name
      WHEN Employees.credentials IS NULL THEN Employees.last_name + ', ' + Employees.first_name
      ELSE Employees.last_name + ', ' + Employees.first_name + ' - ' + Employees.credentials
   END,
   Employees.num3,
   Employees.address1 + '  ' + Employees.city + ', ' + Employees.state + '  ' + Employees.zip,
   Employees.work_phone,
   CASE
      WHEN Clients.age <= 18 THEN 'Youth'
      ELSE 'Adult'
   END,
   Clients.client_id,
   Clients.last_name + ', ' + Clients.first_name,
   ClientVisit.cptcode,
   ClientVisit.visittype,
   ClientVisit.rev_timeout,
   ClientVisit.timein,
   ClientVisit.duration,
   SUM(CASE
      WHEN ClientVisit.cptcode = 90791 THEN 200
      WHEN ClientVisit.comb_units = 1 THEN 85.67
      ELSE ClientVisit.comb_units * 21.4175
   END),
   DATEDIFF(d, ClientVisit.rev_timeout, ClientVisit.signature_datetime)
FROM dbo.ClientVisit
INNER JOIN dbo.Employees
ON (
   ClientVisit.by_emp_id = Employees.emp_id
)
INNER JOIN dbo.Programs
ON (
   ClientVisit.program_id = Programs.program_id
)
INNER JOIN dbo.Clients
ON (
   Clients.client_id = ClientVisit.client_id
)
WHERE (
   ClientVisit.rev_timeout BETWEEN '20160401 11:40:00.000' AND '20160415 11:40:16.000'
   AND Programs.program_desc IN ('Off Panel')
   AND ClientVisit.non_billable = 0
   AND ClientVisit.cptcode NOT IN ('00000', '0124', '100', '1001', '101', '102', '103', '80100', '9079', '99999')
   AND Employees.num3 IS NOT NULL
)
GROUP BY 
   CASE
      WHEN Clients.age <= 18 THEN 'Youth'
      ELSE 'Adult'
   END,
   CASE
      WHEN Employees.first_name IS NULL
      OR Employees.first_name = 'x' THEN Employees.last_name
      WHEN Employees.credentials IS NULL THEN Employees.last_name + ', ' + Employees.first_name
      ELSE Employees.last_name + ', ' + Employees.first_name + ' - ' + Employees.credentials
   END,
   DATEDIFF(d, ClientVisit.rev_timeout, ClientVisit.signature_datetime),
   ClientVisit.cptcode,
   Clients.last_name + ', ' + Clients.first_name,
   Clients.client_id,
   Employees.address1 + '  ' + Employees.city + ', ' + Employees.state + '  ' + Employees.zip,
   Employees.work_phone,
   ClientVisit.duration,
   ClientVisit.visittype,
   ClientVisit.rev_timeout,
   ClientVisit.timein,
   Employees.num3

给我错误:

  

转换varchar值时转换失败&#39; H2019&#39;数据   输入int。

我无法找到具体发生转换的位置以及可能的修复方法。

编辑:在cptcode列中找到具有字母数字条目的问题。但是,在WHERE子句中更改日期范围会为某些日期而不是其他日期提供结果。

2 个答案:

答案 0 :(得分:2)

注释掉选择片段中的所有列,并在每次执行选择时逐个开始逐个注释。当您取消注释有问题的行时,您将找到错误的来源。

顺便说一句:我猜这个问题出现在"ClientVisit"."cptcode" = 90791

答案 1 :(得分:2)

使用时

"ClientVisit"."cptcode" = 90791

数据类型90791将是整数。如果用

替换它
"ClientVisit"."cptcode" = '90791'

等式的两边都是字符。您还可以执行以下操作:

 "ClientVisit"."cptcode" = CAST(90791 AS VARCHAR(20))

您的问题的原因是SQL Server将执行implicit conversion。在这种情况下为整数,因为Integer的Data Type Precedence比(n)(var)char数据类型更高。

当然我不知道您的数据,但我猜有数据范围,其中只有cptcode的数值。因此,您的代码将适用于他们,但如果您遇到类似于H006

希望有所帮助; - )