May 2, 2008 at 1:37 pm
I have a problem with file export. I need to create a process to create and extract file and the format is new to me. For the currency fields the end user expects the fields to have an implied decimal point, be zero filled to the field width and have a trailing sign (either a '-' for negative or space for positive) example:
$102.50 would be '00010250 '
-$57.75 would be '00005750-'
Is there a standard format in SQL, either DTS or t-SQL that can accomplish this? I figure I can multiply each number by 100 to get rid of the decimal; write a case statement to get the sign and handle the 0 fill. This seems like a lot of work.
Thanks in advance for any help!
May 2, 2008 at 1:51 pm
Your way or create a function to do it.
______________________________________________________________________
Personal Motto: Why push the envelope when you can just open it?
If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.
Jason L. SelburgMay 2, 2008 at 8:16 pm
Don't need a case statement to figure the zero's...
DECLARE @DemoTable TABLE (Amount VARCHAR(15))
INSERT INTO @DemoTable (Amount)
SELECT '$102.50' UNION ALL
SELECT '-$57.75'
SELECT REPLACE(STR(ABS(CAST(Amount AS MONEY)*100.0),8),' ','0')
+ CASE
WHEN CAST(Amount AS MONEY) < 0
THEN '-'
ELSE ' '
END
FROM @DemoTable
This could actually be used in a computed column in a table or you could turn it into a function.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 2, 2008 at 8:20 pm
Sorry, my mistake... you don't need a CASE at all...
DECLARE @DemoTable TABLE (Amount VARCHAR(15))
INSERT INTO @DemoTable (Amount)
SELECT '$102.50' UNION ALL
SELECT '-$57.75'
SELECT REPLACE(STR(ABS(CAST(Amount AS MONEY)*100.0),8),' ','0')
+ LEFT(STR(SIGN(CAST(Amount AS MONEY)),2),1)
FROM @DemoTable
--Jeff Moden
Change is inevitable... Change for the better is not.
May 5, 2008 at 7:35 am
Thanks for the help. I will investigate user defined functions. I think I will be able to use these in other areas.
I tested the other solution provided and that works well and seems to have an easier learning curve.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply