Solved

Manipulating SMALLMONEY data type

Posted on 2006-07-05
1
981 Views
Last Modified: 2013-12-03
Hi,

I'm using the SMALLMONEY data type.  I have a function that uses a string value passed from VB.NET to add the string to the SMALLMONEY value and return the result as SMALLLMONEY.  The following function works:

CREATE FUNCTION dbo.AddMoney
      (
      @ORIGINALVALUE AS SMALLMONEY, --The original Value to be added to
      @ADDAMOUNT AS VARCHAR(10) --The value to be added
      )
RETURNS SMALLMONEY
AS
      BEGIN
      DECLARE @RETURNAMOUNT AS SMALLMONEY
      SET @RETURNAMOUNT = CAST(@ADDAMOUNT AS SMALLMONEY)
      SET @RETURNAMOUNT = @RETURNAMOUNT + @ORIGINALVALUE
      RETURN @RETURNAMOUNT
      END

My question is:  I need to calculate a percentage of a SMALLMONEY value and return the result as a SMALLLMONEY.  So I need to pass in the Price to amend, and the percentage to calculate ie (17.5% TAX).  What would be the best types to pass in to the function and how would I manage the conversions to get an accurate result?
0
Comment
Question by:AMLabels
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 17043943
NOTE:
  you should NOT use money and smallmoney as database field values, as this is a mix of value + currency.
  use decimal instead, and for DISPLAY (and possibly import), you can use the money data types.

Here a function (with money, but as noted above, you should not use money)

CREATE FUNCTION dbo.ComputeTax
     (
     @ORIGINALVALUE AS SMALLMONEY, --The original Value to be added to
     @TaxValue AS Decimal(10,4) --The tax value
     )
RETURNS SMALLMONEY
AS
     BEGIN
     DECLARE @RETURNAMOUNT AS SMALLMONEY
     SET @RETURNAMOUNT = @ORIGINALVALUE * @TaxValue / 100
     RETURN @RETURNAMOUNT
     END
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now