TSQL如何在Case语句中将varchar转换为numeric

时间:2016-02-05 23:01:28

标签: sql sql-server tsql

这是我的疑问。我想采用[市场价值]案例表达并将其添加到“认证”中。案例表达。

  DECLARE @AS_OF_DATE DATE
SET @AS_OF_DATE = '01/27/2016'

SELECT
    'AS OF DATE'    = @AS_OF_DATE   
,   A.AcctNbr
,   A.CUSIPNumber
,   B.Desc1
,   B.Symbol
,   'SEC TYPE'  = RTRIM (B.SecType)+B.CmpQual+B.SecQual
,   A.AcctType
,   A.LocMemo
,   A.BegSdQuantity
,   B.ClosePrice
,   [MARKET VALUE]  = (CASE WHEN RTRIM (B.SECTYPE)+B.CmpQual+B.SecQual IN ('A01', 'A02', 'B01', 'B02', 'C01', 'C02', 'F01', 'F02', 'F03',
    'F04', 'F05', 'F06', 'G01', 'G02', 'G03', 'G04', 'H01', 'H02', 'T01', '501', '502', '503', '504', '601', '602', '603')
    THEN CONVERT(DECIMAL(25,3),A.BegSdQuantity * B.ClosePrice)
    ELSE CONVERT(DECIMAL(25,3),A.BegSdQuantity /100 * B.ClosePrice) END)
,   'TOTAL DEBIT'   = C.SETTLEBalance
,   'HYPOTHECATION' = C.SETTLEBalance * 1.40
,   'CERTIFIED'     =  (CASE WHEN ((('MARKET VALUE') < (C.SETTLEBalance * 1.40)) OR
                        (C.SETTLEBalance * 1.40 = '0.00')) THEN 'CERTIFIED'
                        ELSE 'EXCEPTION'
                        END)

FROM vw_Table A
INNER join vw_Table B on A.FirmCUSIPId = B.FirmCUSIPId
    AND @AS_OF_DATE between a.EffectiveDate and a.ExpirationDate
    AND @AS_OF_DATE between b.EffectiveDate and b.ExpirationDate
INNER JOIN vw_Table C ON A.FirmAccountId = C.FirmAccountId
    AND @AS_OF_DATE BETWEEN C.EffectiveDate AND C.ExpirationDate

当运行上面的查询时,我收到错误消息:&#34;将数据类型varchar转换为数字&#34;

时出错

有关如何解决这个问题的想法吗?

谢谢,

1 个答案:

答案 0 :(得分:2)

下面

CASE WHEN ((('MARKET VALUE') < (C.SETTLEBalance * 1.40))

您正在将varchar值&#34; MARKET VALUE&#34; 与数值进行比较。你期待什么?

可能是你应该把关闭的paranthesis放在后面&#34; A.BegSdQuantity&#34;,不知道那里有什么数据类型......:

CONVERT(DECIMAL(25,3),A.BegSdQuantity) * B.ClosePrice

但是如果不知道你的桌子的声明,那就是读一个神奇的玻璃灯泡......