Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Formating money

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-09-23 : 08:04:59
Nick writes "If I have a figure of say 1000.00 how do I format it with commas like 1,000.00 ?"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-09-23 : 08:05:47
In your application's presentation layer, i.e. user interface or report layout. Don't do it in the database layer.
Go to Top of Page

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2003-09-23 : 09:14:46
... and if you really really want to do it using SQL Server, create a UDF.


CREATE FUNCTION DBO.FORMATNUMBER (@VALUE NUMERIC(19,6))
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @PART1 VARCHAR(100) -- HOLDS ALL THE DIGITS ON THE LEFT OF THE DICMAL PLACE
DECLARE @PART2 VARCHAR(100) -- HOLDS ALL DIGITS ON THE RIGHT SIDE OF THE DECIMAL PLACE
DECLARE @HOLDER VARCHAR(100) -- HOLD THE MAIN NUMBER
DECLARE @COUNTER INT -- COUNTER FOR THE WHILE LOOP
DECLARE @FLG INT -- INDICATOR FOR POSITIVE/NEGATIVE NUMBERS

SET @FLG = 0

IF @VALUE < 0
BEGIN
SET @FLG = 1
END

SET @HOLDER = (CONVERT(VARCHAR(100),ABS(@VALUE)))

SET @PART1 = SUBSTRING(@HOLDER, 1, CHARINDEX('.', @HOLDER)-1)
SET @PART1 = REVERSE(@PART1)
SET @PART2 = SUBSTRING(@HOLDER, CHARINDEX('.', @HOLDER),LEN(@HOLDER)-CHARINDEX('.', @HOLDER)+1)

SET @COUNTER = 1
SET @HOLDER = ''

WHILE @COUNTER <= LEN(@PART1)
BEGIN
IF @COUNTER%3 = 0
BEGIN
SELECT @HOLDER = @HOLDER + SUBSTRING(@PART1, @COUNTER, 1)+','
END
ELSE
BEGIN
SELECT @HOLDER = @HOLDER + SUBSTRING(@PART1, @COUNTER, 1)
END
SET @COUNTER = @COUNTER + 1
END

IF SUBSTRING(REVERSE(@HOLDER), 1, 1) = ',' BEGIN SET @HOLDER = SUBSTRING(@HOLDER, 1, LEN(@HOLDER)-1) END

IF @FLG = 1
RETURN '-'+REVERSE(@HOLDER)+CASE WHEN CAST(@PART2 AS NUMERIC(19,4)) = 0 THEN '' ELSE @PART2 END

RETURN REVERSE(@HOLDER)+CASE WHEN CAST(@PART2 AS NUMERIC(19,4)) = 0 THEN '' ELSE @PART2 END

END


SELECT 100000505, DBO.FORMATNUMBER(100000505)

Sorry Rob!

__________________
Make love not war!
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-09-23 : 11:33:47
NEVER do this at the database; notice you are no longer returning a NUMBER to the client, but rather TEXT. they cannot add it, compare it, sort it, etc -- without needing to convert it BACK to a number!

ALWAYS return raw data to the clients and let them do the formatting.

I cannot stress this enough. Can it be done in SQL with a long UDF ? sure. Should you? No.

- Jeff
Go to Top of Page

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2003-09-23 : 12:20:16
Hey, calm down mate. I did this for a laugh quite a while back and I posted it here just because. And who's talking clients anyway?! I mean the guy could be doing this to please his girlfriend for all we know!

quote:

ALWAYS return raw data to the clients and let them do the formatting.


Oh yeah?!
Only if the cheeky sods behave themselves and send you meaningful data in the first place!

__________________
Make love not war!
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-09-23 : 12:31:55
oh, I know, I just don't want to encourage people to do things the hard way ....

Probably becuase i'm lazy but I prefer the easy, most efficient and straight-forward way of getting the job done ...

quote:

the guy could be doing this to please his girlfriend for all we know



It's true, chicks dig SQL !!!

- Jeff
Go to Top of Page
   

- Advertisement -