string formatting

  • in Access, you can use the following sql statement to format a number:

    format(number,"0000") as result

    An example of result could be 0023, rather than 23

    WHen doing in SQL Server, I am advised that format is not a recognised function. WHat can I do to achieve the same result in SQL Server? Cheers.

  • I don't know of a function in SQL that will format a number that way. I generally do something like this.

    Declare @i int

    Set @i = 23

    Select Right('0000' + Cast(@i as varchar(4)),4)

  • thanks Ken

    it worked perfectly.

  • This type of situation is encountered frequently (see Dave's post re: Converting Numeric to String http://www.sqlservercentral.com/Forums/Topic594180-8-1.aspx )

    The approaches listed there and in the previous reply work, but for singular occurences of this type of query. I creaeted the two following UDF's that in any situation allow formatting of decimal or integer values with a given "pad" character.

    create function dbo.formati(@value int, @pad int, @padchar char(1) )

    returns varchar(max)

    as

    begin

    return (select REPLICATE( @padchar ,@pad-len(@value)) + convert(varchar,@value) )

    end

    go

    create function dbo.formatd(@value decimal(10,2), @pad int, @padchar char(1) )

    returns varchar(max)

    as

    begin

    return (select REPLICATE( @padchar ,@pad-len(@value)) + convert(varchar,@value) )

    end

    go

    select dbo.formati( MONTH('2008-09-01'),2, '0' )

    select dbo.formati( 123,5, '0' )

    select dbo.formatd( 123.45,8, 'x' )

    Too bad you can't overload functions in T-SQL and use just one function call and let the engine chose the correct function based on the signature (hint hint, MS 🙂 )

    DAB

  • Thanks DAB! That's very useful to know. 😀

    _____________________________________________________________________________MCITP: Business Intelligence Developer (2005)

  • DAB

    i've just tried to implement this on an SQL2000 database, but I getting an error on both functions:

    Msg 170, Level 15, State 1, Procedure formati, Line 2

    Line 2: Incorrect syntax near 'max'.

    Msg 178, Level 15, State 1, Procedure formati, Line 6

    A RETURN statement with a return value cannot be used in this context.

    Msg 170, Level 15, State 1, Procedure formatd, Line 3

    Line 3: Incorrect syntax near 'max'.

    Msg 178, Level 15, State 1, Procedure formatd, Line 7

    A RETURN statement with a return value cannot be used in this context.

    any ideas?

    _____________________________________________________________________________MCITP: Business Intelligence Developer (2005)

  • mikemireku (10/30/2008)


    in Access, you can use the following sql statement to format a number:

    format(number,"0000") as result

    An example of result could be 0023, rather than 23

    WHen doing in SQL Server, I am advised that format is not a recognised function. WHat can I do to achieve the same result in SQL Server? Cheers.

    Where do you want to show data?

    If you use front end application, you can use format function there


    Madhivanan

    Failing to plan is Planning to fail

  • Hi Dave,

    VARCHAR(MAX) was only introduced in SS2K5, so you won't be able to use it in 2K.

    Cath

  • I copied and pasted this into a deummy db and it all compiled fine. Are you using SQL 2005? I If not, you'll need to change varchar(max) to something like varchar(10) or some other value large enough to hold your results. made the assumption that since you posted in the 2005 are that you were. If you're using

    DAB

  • Ok cool.

    All the help is appreciated!

    - thanks all.

    _____________________________________________________________________________MCITP: Business Intelligence Developer (2005)

Viewing 10 posts - 1 through 9 (of 9 total)

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