比较两列时,是否可以将Case函数与nvarchar数据类型一起使用?

时间:2019-04-08 11:01:45

标签: sql sql-server

我正在尝试检索满足大小写条件的值,条件是

  • 当tr.value

  • 当tr.value> tr.PanicMax然后返回tr.value

但是我收到一条错误消息,提示“将数据类型从nvarchar转换为数值时出错”,因为三列(tr.panicMin,tr.PanicMax,Tr.value)中的值不是数值, 添加case函数之前使用的原始查询是:

SELECT SP.Specialisation, TSP.SpecimenName,T.Name as Test_Name,TR.Value,TR.PanicMin ,tr.PanicMax , PA.RegCode AS UHID, TT.DoneAt AS COL_DATE, TT1.DoneAt AS ACK_DATE, TT2.DoneAt AS VER_DATE, TT2.DoneBy,
(CAST((DATEDIFF(minute, TT1.DoneAt, TT2.DoneAt))/60 AS VARCHAR)+' H:'                                 
+ CAST((DATEDIFF(minute,TT1.DoneAt,TT2.DoneAt))%60 AS VARCHAR)+' M') AS TAT,
(CASE WHEN TOI.TestID IN (2160, 2878, 956, 1000, 1002, 932, 934) AND ((DATEDIFF(minute, TT1.DoneAt, TT2.DoneAt)) > 60) THEN 'WITHOUT TAT'
WHEN TOI.TestID IN (991, 941, 942, 943, 2396) AND ((DATEDIFF(minute, TT1.DoneAt, TT2.DoneAt)) > 15) THEN 'WITHOUT TAT'
WHEN TT2.DoneAt IS NULL THEN NULL
ELSE 'WITHIN TAT' END) AS TAT_STAT 
FROM v_dbNxGtestorderitems TOI
INNER JOIN V_TestSpecimen TSP ON TSP.TestID = TOI.TestID
INNER JOIN v_dbNxgtestorders TSO ON TSO.TestOrderID = TOI.TestOrderID AND TSO.OrderTypeID = 15
INNER JOIN v_dbSpecializations SP ON SP.SpecialiseID = TOI.SpecialiseID
INNER JOIN v_dbPatientAdmissions PA ON PA.AdmissionID = TOI.AdmissionID
  join v_dbtestresults tr on tr.TestOrderID=TOI.TestOrderID
  join v_Tests t on t.TestId=tr.TestID
  join v_dbTestComponents tc on tc.TestID=tr.TestID
  join v_dbComponents c on c.ComponentID=tr.ComponentID
CROSS apply   (select top 1  TestOrderID, TestOrderItemID, DoneAt  from  v_dbtesttasks
where TestOrderID=toi.TestOrderID and    TestOrderItemID=toi.TestOrderItemID and taskStatusid=3
Order  by DoneAt desc  )  tt
CROSS apply   (select top 1  TestOrderID, TestOrderItemID, DoneAt  from  v_dbtesttasks
where TestOrderID=toi.TestOrderID and    TestOrderItemID=toi.TestOrderItemID and taskStatusid=4
Order  by DoneAt desc  )  tt1
outer  apply   (select top 1  TestOrderID, TestOrderItemID, DoneAt, SD.Name AS DoneBy from  v_dbtesttasks TTS
INNER JOIN v_dbSecurityIds SD ON SD.SID = TTS.DoneBy
where TestOrderID=toi.TestOrderID and    TestOrderItemID=toi.TestOrderItemID and taskStatusid=8
order  by DoneAt desc  )  tt2
WHERE TOI.TestID in (932, 934, 941, 942, 943, 991, 1000, 1002, 2396, 2878, 2160) AND TSO.OrderDate > '2019-01-01' AND TSO.OrderDate < DATEADD(DAY, 1, '2019-01-10') 

,查询输出为 enter image description here

2 个答案:

答案 0 :(得分:1)

您可以尝试将包含数字内容的字符串转换为浮点数

 convert(float,'9.0')

就您而言

convert(float, tr.PanicMin) 

答案 1 :(得分:1)

在SQL Server 2012及更高版本中,您可以使用:

CASE WHEN TRY_CONVERT(float, tr.value) < TRY_CONVERT(float, tr.PanicMin)
       OR TRY_CONVERT(float, tr.value) > TRY_CONVERT(float, tr.PanicMax)
  THEN tr.value END

在较旧的版本中,一种方法要求您依赖一种不稳定的函数和嵌套的CASE表达式:

CASE 
  WHEN ISNUMERIC(tr.PanicMin) = 1 
   AND ISNUMERIC(tr.PanicMax) = 1
   AND ISNUMERIC(tr.value) = 1 
  THEN 
    CASE 
      WHEN CONVERT(float, tr.value) < CONVERT(float, tr.PanicMan)
           OR CONVERT(float, tr.value) > CONVERT(floar, tr.PanixMax)
      THEN tr.value
    END
END