Converting numeric data value to string

  • Hi,

    In what way we can convert a numeric value (int, smallint, money) to a string with a special format.

    For example

    Example 1 :

    the numeric value is : 234423476.889

    the string value I want is : 234,423,476.8890

    the format string is : #,##0.0000

    exemple 2 :

    the numeric value is : 21

    the string value I want is : 0021

    thank you for your help,

    Ikka Vertika

    Jakarta - Indonesia

  • Check the articles 'Tame Those Strings'.

    One article (part 4) is about numeric conversions, and should get you pointed in the right direction :

    http://www.sqlservercentral.com/columnists/sjones/20010422115809_1.asp

  • I'm not sure how you'd tackle Example 1, but it gives me something to play with this afternoon, thanks!

    Example two, I can help with. To left zero pad a number you need the Replicate function.

    If you want a field to be 4 characters, you're given 21, and you need to left zero pad:

    --instead of a field name, we'll use @x

    declare @x int
    
    set @x = 21

    print replicate('0', 4 - len(cast(@x varchar(4))) + cast(@x varchar(4))

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply