【问题标题】:Convert money datatype to 3 decimal places将货币数据类型转换为小数点后 3 位
【发布时间】:2015-11-21 09:07:59
【问题描述】:

我正在尝试以小数点后 3 位的十进制格式显示我的 PT.TotalAmount(money) 字段。我尝试使用

CAST(ROUND(123.4567, 3) AS MONEY)

但我得到错误:

对象或列名丢失或为空。对于选择进入 语句,验证每一列都有一个名称。对于其他陈述,请查看 对于空别名。不允许使用定义为 "" 或 [] 的别名。 将别名更改为有效名称。

这是我的 SP:

Select PT.[ID] 'TransactionID', PT.BatchNumber, PT.SequenceNumber, PT.TransactionDate,   
 PT.TerminalID ,CAST(ROUND(PT.TotalAmount, 2) AS MONEY), PT.TransactionTypeID, TT.TransactionType,     
 PT.PAN 'EmbossLine',PT.PreBalanceAmount, PT.PostBalanceAmount, RefTxnID,   
SettlementDate,PaidCash, CreditAmount, DiscountAmount,    
RefPAN, PT.Remarks, '  ' + CashierCard as 'SupervisorCard',St.StoreID    
into #Temp    
from POS_Transactions PT inner join TransactionType TT on TT.TransactionTypeID = PT.TransactionTypeID     
inner join Staff St on St.CardNumber=PT.CashierCard     
where     
PT.[ID] not in (Select distinct isnull(TransactionID,0) from Testcards)    
and (PT.TransactionDate >= @DateFrom)    
and (PT.TransactionDate < @DateTo)    
and (PT.TransactionTypeID = @TransactionTypeID or @TransactionTypeID = -999)    



select T.*, '&nbsp; '+  C.EmbossLine as 'EmbossLine', '&nbsp;'+ C.EmbossLine as 'EmbossLine1',    

isnull(C.FirstName,'') +' '+ isnull(C.LastName,'') 'EmbossName',C.FirstName,C.LastName,City.CityName,Country.CountryName,Country.CurrencyName,  PM.MerchantID , PM.MerchantName1, C.AccountNumber, C.VehicleNumber    

from #Temp T     

inner join Card C on C.EmbossLine= T.EmbossLine    

inner join Terminal on Terminal.TerminalID = T.TerminalID    

inner join Merchant PM on  PM.MerchantID = Terminal.MerchantID    

inner join City on City.CityID = PM.CityID    

inner join Country on Country.CountryID = PM.CountryID     

where C.Status <>'E3'    

and C.CardID not in (Select distinct isnull(CardID,0) from Testcards)    

and (PM.MerchantID =@MerchantID or @MerchantID='-999')    

and (C.EmbossLine like '%'+@EmbossLine+'%' or @EmbossLine like '-999')    

and (C.FirstName like '%'+@FirstName+'%' or @FirstName like '-999')    

and (C.LastName like '%'+@LastName+'%' or @LastName like '-999')    

and (PM.CountryID = @CountryID or @CountryID ='-999')    

and(PM.CityID = @CityID or @CityID ='-999')    

order by T.TransactionDate, MerchantName1, T.BatchNumber, T.SequenceNumber    

drop table #Temp 

为什么会出现此错误?转换有什么问题?当我在我的Select语句中简单地写PT.TotalAmount时,查询成功完成

【问题讨论】:

    标签: sql-server decimal type-conversion currency


    【解决方案1】:

    只需在您的 sql 查询中将 CAST(ROUND(PT.TotalAmount, 2) AS MONEY) 的任何别名添加为 roundedamont(whatever column name you want)

    【讨论】:

    • 谢谢。我还有一个问题;它仍然显示最多 4 个小数位的值。为什么?我是否需要在我的 VB 代码中进行转换才能显示最多 3 位小数?
    • 我在 SQL Server 2008 R2 中使用过,它可以工作(意味着它只显示 3 个十进制值)
    【解决方案2】:

    Select * into 语句要求每列有不同的名称

    Select PT.[ID] 'TransactionID', PT.BatchNumber, PT.SequenceNumber, PT.TransactionDate,   
     PT.TerminalID ,CAST(ROUND(PT.TotalAmount, 2) AS MONEY)<----Give Alias HERE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2022-12-03
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多