' /'中的服务器错误应用程序无法将char值转换为money

时间:2015-04-10 10:46:40

标签: asp.net sql-server casting type-conversion

我正在运行以下存储的程序。 结果集在gridView控件上绑定。

@EmbossLine varchar(20),

@TransactionTypeID int,

@DateFrom datetime,

@DateTo datetime

AS

Select 

pt.TransactionDate,
m.MerchantName1,
pt.TerminalID,
pt.SequenceNumber,
tt.TransactionType,
case TT.TransactionTypeID when 0 then PT.TotalAmount else 'null' end 'PointsEarned',
case TT.TransactionTypeID when 2 then PT.TotalAmount else 'null' end 'PointsRedemeed'
from POS_Transactions PT 
inner join TransactionType TT on TT.TransactionTypeID = PT.TransactionTypeID
inner join CardBalance CB on CB.PAN = PT.PAN
inner join Terminal T on T.TerminalID = PT.TerminalID
inner join Merchant M on M.MerchantID = T.MerchantID
where 
(PT.TransactionDate>=@DateFrom and PT.TransactionDate<=@DateTo)
and (PT.TransactionTypeID = @TransactionTypeID or @TransactionTypeID ='-999')
and(PT.PAN=@EmbossLine or PT.PAN='-999')
order by PT.TransactionDate desc

实际上,我想要做的是,我想在null不等于TransactionTypeID0时打印2。但它会引发运行时异常

Cannot convert a char value to money. The char value has incorrect syntax.

我知道这是一个转换问题,想知道如何在条件评估为false时打印一些值

2 个答案:

答案 0 :(得分:2)

更改这两行:

case TT.TransactionTypeID when 0 then PT.TotalAmount else 'null' end 'PointsEarned',
case TT.TransactionTypeID when 2 then PT.TotalAmount else 'null' end 'PointsRedemeed'

到此:

case TT.TransactionTypeID when 0 then PT.TotalAmount else null end 'PointsEarned',
case TT.TransactionTypeID when 2 then PT.TotalAmount else null end 'PointsRedemeed'

您希望在null条件中生成else值。您目前正在生成转换失败的单词'null' - 您无法将单词(char数据类型)'null'转换为money数据类型。

编辑:

您需要将值强制转换为字符串才能执行此操作。但这意味着不是将money数据类型返回给您的客户端,而是返回一个字符串。

这种事情应该在客户端而不是在SQL Server中处理。在任何情况下,尝试将所有内容转换为字符串:

case TT.TransactionTypeID when 0 then Cast(PT.TotalAmount as varchar(50)) else 'null' end 'PointsEarned',
case TT.TransactionTypeID when 2 then Cast(PT.TotalAmount as varchar(50)) else 'null' end 'PointsRedemeed'

答案 1 :(得分:0)

这样做。

case TT.TransactionTypeID when 0 then cast(PT.TotalAmount as varchar) else 'null' end 'PointsEarned',
case TT.TransactionTypeID when 2 then cast(PT.TotalAmount as varchar) else 'null' end 'PointsRedemeed'

只需将TotalAmount转换为varchar,它应该可以正常工作。