My first SQL Server 2000 function

SQL Server 2000 gives us the ability to write our own (user-defined) functions. I am sure this is not news to most of you, but up until today I have not taken the time to actually write one, but after spending too much time on debugging a divide by zero error in one of my views yesterday (due to all the funkie convert logic = hard to read on a single monitor) I decided to write one that would simplify my currency formatting.

Currently for a view that a report of mine uses I have a statement that is something like this:

SELECT LEFT(CONVERT(VARCHAR(CONVERT(MONEY,345.54))),LEN(CONVERT(VARCHAR(CONVERT(MONEY,345.54))))-3)

Needless to say, there are several columns that I have to do this funkie conversion stuff just to spit the numbers out the way the user wants to see it on the report...so today I decided to "simply and economize" - (think Po Bronson's movie The First $20 Million Is Always the Hardest)

This is the code for the function:

CREATE FUNCTION dbo.FormatCurrency
(
 @number DECIMAL (15,6),
 @dec INT,
 @dollar BIT
)
RETURNS varchar(20) AS
BEGIN
 DECLARE @str VARCHAR(20)
  DECLARE @cur VARCHAR(20)
 -- use default covert from decimal -> money -> varchar
 SET @str = CONVERT(VARCHAR,CONVERT(MONEY,@number),1)
 -- determine whether to prepend a $ sign
 IF (@dollar = 1)
  SET @str = '$' + @str
 -- determine trim off digits as asked for
 IF (@dec = 0) -- take off period and 2 digits
  SET @cur = LEFT(@str, LEN(@str)-3)
 ELSE IF (@dec = 1) -- trim off
  SET @cur = LEFT(@str, LEN(@str)-1)
 ELSE IF (@dec = 2)
  SET @cur = @str
 -- return currency formatted varchar
 RETURN @cur
END

Now that I have an inline function I can use, the SQL looks a little easier:
SELECT dbo.formatcurrency(345.54, 2, 1) AS Amount
Amount
--------------------
$345.54

Since this is my first SQL User defined function, I am sure there are ways to improve this function, any feed back on how to do so is welcomed!

posted on Wednesday, March 03, 2004 6:45 AM

Feedback

# re: My first SQL Server 2000 function

Very Usful , Great Work , Wish U All The Best
1/3/2005 1:51 PM | Amila

# re: My first SQL Server 2000 function

Thanks dude, this function saved my butt today!!
2/21/2005 4:21 AM | Scott

# re: My first SQL Server 2000 function

Thanks, great little function.... saved me load of time!
2/22/2005 8:32 PM | James

# re: My first SQL Server 2000 function

Thanks a lot.. superb function... Very useful ..
2/24/2005 6:14 PM | Anu

# re: My first SQL Server 2000 function

How would I use this function during an insert ?
Pass what is now varchar through it to get it converted to 'money' and then store it ?

Help please!?!?!
3/22/2005 3:49 AM | SQL Novice

# re: My first SQL Server 2000 function

Great job.
You saved me sometime.
If need help just shot me an email.
coelhoteam@aol.com
3/24/2005 11:16 PM | marcio coelho

# re: My first SQL Server 2000 function

the function is very good! It's possible use this function for EURO currency?
thank's a lot
5/16/2005 3:01 AM | Fabio

# re: My first SQL Server 2000 function

Thanks very much for this function dude. I've spent hours in C# trying to achieve this with no success. So all praise goes to you!! I'm sorry that I can't offer any ways of improving it - this is the first MS SQLServer user-defined function which I've ever seen ;)
8/10/2006 4:28 AM | Ernest Fakudze

# re: My first SQL Server 2000 function

good job. Can i handle round up also.
Can contact me on panwar_satish@rediffmail.com with same subject.
9/27/2006 8:30 AM | satish panwar

# re: My first SQL Server 2000 function

This is exactly what I need for my project. Million thanks!
12/8/2006 3:05 PM | khautinh

# re: My first SQL Server 2000 function

Dude..You are Good!!!
5/11/2007 1:20 AM | Malebo - South Africa

# re: My first SQL Server 2000 function

Thanks. Very nice.
6/21/2007 6:11 AM | rpgivpgmr

# re: My first SQL Server 2000 function

I don't like the fact that this function is converting to varchar. When save report to XLS i have to convert back to currency to calculate sum.
CONVERT(NUMERIC(8,2),table1.currencyColumn) works better for me...
12/26/2007 9:08 PM | Adam

Post Comment

Title  
Name  
Url
Comment   
Please enter the following code into the box below to stop spammers

  
Enter Code Here *