replacement of format(0,"percent") in SQL

  • do we have a replacment for replacement of format(0,"percent") in SQL?

  • not built in , no; typically formatting is left for the presentation layer, like vb;

    you can do something pretty close:

    declare @val decimal (12,6)

    set @val = 0.43365

    select convert(varchar,convert(decimal(5,2),@val* 100.0)) + '%'

    --43.37%

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Glad Lowell and I posted the same answer since the post was duplicated (http://www.sqlservercentral.com/Forums/Topic672270-338-1.aspx). Please don't post more than once.

  • just realized that sorry...

    But thank you both for your posts.

    Many Thanks....

  • Lowell (3/10/2009)


    not built in , no; typically formatting is left for the presentation layer, like vb;

    you can do something pretty close:

    declare @val decimal (12,6)

    set @val = 0.43365

    select convert(varchar,convert(decimal(5,2),@val* 100.0)) + '%'

    --43.37%

    Strangely enough, the Format() function has been added to SQL Server 2012.

    http://technet.microsoft.com/en-us/library/hh213505.aspx

    No idea why they would have added this, as Lowell said, better left to the presentation layer. Maybe if your presentation layer is a bit lacking, or for ad-hock querying it's useful.

  • davoscollective (11/18/2013)


    Lowell (3/10/2009)


    not built in , no; typically formatting is left for the presentation layer, like vb;

    you can do something pretty close:

    declare @val decimal (12,6)

    set @val = 0.43365

    select convert(varchar,convert(decimal(5,2),@val* 100.0)) + '%'

    --43.37%

    Strangely enough, the Format() function has been added to SQL Server 2012.

    http://technet.microsoft.com/en-us/library/hh213505.aspx

    No idea why they would have added this, as Lowell said, better left to the presentation layer. Maybe if your presentation layer is a bit lacking, or for ad-hock querying it's useful.

    Sometimes... there is no "presentation layer". For example, I didn't write an app for my morning reports and I've never written an app to export data, which may be required in a certain format by the receiver of the data.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 1 through 5 (of 5 total)

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