Results 1 to 8 of 8

Thread: [RESOLVED] transforming numeric amount

  1. #1

    Thread Starter
    Addicted Member Golfreak's Avatar
    Join Date
    Oct 2005
    Location
    stuck in a cube
    Posts
    147

    Resolved [RESOLVED] transforming numeric amount

    Hi all,

    I am trying to transform a numeric amount in SQL Server 2000. What I started with is this:
    -119.760000000000
    What I have now is this:
    -119.76
    What I need when I am done is this:
    0000011976
    I know there is a function or method to accomplish my goal.
    Can anyone help?
    jg

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: transforming numeric amount

    What is the actual datatype in SQL?

    (didn't like my last solution - eh?)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: transforming numeric amount

    Here's a function - I'm assuming a MONEY datatype - you can change it to whatever you want.

    VB Code:
    1. Create Function dbo.FormatNumber (@InVal money, @OutLen int) Returns varchar(100)
    2. As
    3. Begin
    4.     Return Left(Right(Replicate('0',@OutLen+1)+Cast(Abs(@InVal) as varchar(100)),@OutLen+1),@OutLen-2)
    5.     +Right(Cast(Abs(@InVal) as varchar(100)),2)
    6. End
    Use it like this:

    Select dbo.FormatNumber(-119.76,10)

    Returns this:

    0000011976

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Addicted Member Golfreak's Avatar
    Join Date
    Oct 2005
    Location
    stuck in a cube
    Posts
    147

    Re: transforming numeric amount

    Thanks again szlamany,

    But for some reason it doesn't work properly for me. I copied it just like you have it except I changed it slightly to fit my needs.
    I changed:
    'MONEY' to "NUMERIC'
    (that is the data type I am trying to return from the DB)
    and I changed:
    'dbo.FormatNumber(-119.76,10)' to 'dbo.FormatNumber(b.PaymentAmountCur,10)'
    because that is the field from the DB that needs formatting.
    I say it is not working properly because this is what I need:
    0000011976
    And this is what I get when I run the function in the Query analyzer:
    0000000020
    Just a side note- I found a way using what you gave me, another function I came up with, and just brainstorming and came up with this result:
    00000119760000000000
    This is the code I used:
    VB Code:
    1. Right('0000000000'+dbo.StripDecimalPoints(CONVERT(VARCHAR(20),B.PaymentAmountCur)),20)
    and this is my StripDecimal function:
    VB Code:
    1. CREATE FUNCTION dbo.StripDecimalPoints (@amt varchar(20))
    2. RETURNS varchar(20) AS  
    3. BEGIN
    4.     declare @pos int
    5.     set @pos = patindex( '%[ -. ]%' , @amt)
    6.     while @pos > 0
    7.     begin
    8.         set @amt = replace ( @amt, substring( @amt, @pos ,1), '')
    9.         set @pos = patindex( '%[ -. ]%' , @amt)
    10.     end
    11.     return rtrim( @amt)
    12. END
    Any suggestions?
    jg

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: transforming numeric amount

    I've never used NUMERIC datatypes - and you aren't giving me the (X,Y) sizing of them - that's what's causing your problem

    Try my function as I gave it to you - but...

    select dbo.FormatNumber(Cast(b.PaymentAmountCur as money),10)

    btw - why are you not using MONEY datatype?

    If that doesn't work give me the (x,y) size of the numeric so I can code for it.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Addicted Member Golfreak's Avatar
    Join Date
    Oct 2005
    Location
    stuck in a cube
    Posts
    147

    Re: transforming numeric amount

    OK,

    I tried your suggestion and it yielded the same results as before.

    The table that PaymentAmountCur is coming from has it set up like this:
    data type - Numeric
    length - 13 (but this is confusing because it prints out as many as 20 chars)

    The reason why I can't change it is because it is a table in Microsoft Axapta and for some reason it needs to be set up the way that it is.
    jg

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: transforming numeric amount

    Quote Originally Posted by Golfreak
    OK,

    I tried your suggestion and it yielded the same results as before.

    The table that PaymentAmountCur is coming from has it set up like this:
    data type - Numeric
    length - 13 (but this is confusing because it prints out as many as 20 chars)

    The reason why I can't change it is because it is a table in Microsoft Axapta and for some reason it needs to be set up the way that it is.
    Isn't there a length and decimal precision to a NUMERIC datatype? At least there is in MSSQL...

    I'm off to dinner - will work with you later if you still need assistance...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Addicted Member Golfreak's Avatar
    Join Date
    Oct 2005
    Location
    stuck in a cube
    Posts
    147

    Re: transforming numeric amount

    Yeah me too...

    Thanks for all your help szlamany, I am heading home, but I'll be back tomorrow unless I have a revalation tonight.

    Have a good one.
    jg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width