right aligning

  • Comments posted to this topic are about the item right aligning

  • The correct answer (out of the ones listed with the question) is actually pretty slow compared to other methods.  Also, the correct answer takes inputs as FLOAT.  That usually doesn't mean much but if you have more than 15 significant digits, you'll see truncation.

    --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)

  • Jeff Moden - Saturday, January 21, 2017 9:50 PM

    The correct answer (out of the ones listed with the question) is actually pretty slow compared to other methods.  Also, the correct answer takes inputs as FLOAT.  That usually doesn't mean much but if you have more than 15 significant digits, you'll see truncation.

    +100
    😎

    Even this

    CONCAT(REPLICATE(CHAR(32),12 - LEN(T.mynum)),T.mynum)

    is many times faster

  • This was removed by the editor as SPAM

  • Am I completely bonkers and am I the only one that read "right align"? I got this:


           0.00
            0.01
            0.12
            1.23
            1.40
          123.45
         1234.56
        12345.67
            1.00
           12.00
          123.00
         1234.00
        12345.00
       123456.00
      1234567.00

    and actually to my understanding all the options given is wrong. Please tell me.

    :-PManie Verster
    Developer
    Johannesburg
    South Africa

    I can do all things through Christ who strengthens me. - Holy Bible
    I am a man of fixed and unbending principles, the first of which is to be flexible at all times. - Everett Mckinley Dirkson (Well, I am trying. - Manie Verster)

  • Manie Verster - Sunday, January 22, 2017 11:24 PM

    Am I completely bonkers and am I the only one that read "right align"? I got this:


           0.00
            0.01
            0.12
            1.23
            1.40
          123.45
         1234.56
        12345.67
            1.00
           12.00
          123.00
         1234.00
        12345.00
       123456.00
      1234567.00

    and actually to my understanding all the options given is wrong. Please tell me.

    I think that's down to using a non fixed width font

  • Manie Verster - Sunday, January 22, 2017 11:24 PM

    Am I completely bonkers and am I the only one that read "right align"? I got this:


           0.00
            0.01
            0.12
            1.23
            1.40
          123.45
         1234.56
        12345.67
            1.00
           12.00
          123.00
         1234.00
        12345.00
       123456.00
      1234567.00

    and actually to my understanding all the options given is wrong. Please tell me.

    Send the results to Text

    - Damian

  • Manie Verster - Sunday, January 22, 2017 11:24 PM

    Am I completely bonkers and am I the only one that read "right align"? I got this:


           0.00
            0.01
            0.12
            1.23
            1.40
          123.45
         1234.56
        12345.67
            1.00
           12.00
          123.00
         1234.00
        12345.00
       123456.00
      1234567.00

    and actually to my understanding all the options given is wrong. Please tell me.

    I agree!
    I guess since character data is right aligned in MY grid displace it is misleading, but the STR command did not align.
    Oh, well.

  • Thanks for this question, Steve. Interesting is that str () takes input numeric data as a float data type.
    In explanation it would probably be better to state that the STR () returns character data converted from numeric data.

  • djj - Monday, January 23, 2017 5:35 AM

    Manie Verster - Sunday, January 22, 2017 11:24 PM

    Am I completely bonkers and am I the only one that read "right align"? I got this:


           0.00
            0.01
            0.12
            1.23
            1.40
          123.45
         1234.56
        12345.67
            1.00
           12.00
          123.00
         1234.00
        12345.00
       123456.00
      1234567.00

    and actually to my understanding all the options given is wrong. Please tell me.

    I agree!
    I guess since character data is right aligned in MY grid displace it is misleading, but the STR command did not align.
    Oh, well.

    As previously stated, you need a fixed width font to get aligned results. Text results are usually configured to use a fixed width font, or you can change the one on the grid results.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • I am surprised that nobody yet has stated that formatting belongs in the front end instead of directly from your query. 😉 Good question Steve!!!

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange - Monday, January 23, 2017 7:08 AM

    I am surprised that nobody yet has stated that formatting belongs in the front end instead of directly from your query. 😉 Good question Steve!!!

    I can't speak for anyone else but I never format for the front-end.  That, notwithstanding, there are a whole lot of places that require such formatting that don't involve any front-end and every one of them is one of the dozens of columns in hundreds of "Fixed Field Format" files that I used to have to generate.  I also don't use a front-end or Excel to generate my very nicely formatted "Morning Reports" for Overnight Job History, Enterprise Disk Report, or other reports.  I do all of the HTML formatting using some really simple stuff in T-SQL because I'd rather not use SSIS, SSRS, or most of the other SQL Server 4 letter words. 😉

    --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)

  • Luis Cazares - Monday, January 23, 2017 6:34 AM

    djj - Monday, January 23, 2017 5:35 AM

    Manie Verster - Sunday, January 22, 2017 11:24 PM

    Am I completely bonkers and am I the only one that read "right align"? I got this:


           0.00
            0.01
            0.12
            1.23
            1.40
          123.45
         1234.56
        12345.67
            1.00
           12.00
          123.00
         1234.00
        12345.00
       123456.00
      1234567.00

    and actually to my understanding all the options given is wrong. Please tell me.

    I agree!
    I guess since character data is right aligned in MY grid displace it is misleading, but the STR command did not align.
    Oh, well.

    As previously stated, you need a fixed width font to get aligned results. Text results are usually configured to use a fixed width font, or you can change the one on the grid results.

    Works fine when results are sent to text

  • BillLudlow - Tuesday, January 24, 2017 12:45 AM

    Luis Cazares - Monday, January 23, 2017 6:34 AM

    djj - Monday, January 23, 2017 5:35 AM

    Manie Verster - Sunday, January 22, 2017 11:24 PM

    Am I completely bonkers and am I the only one that read "right align"? I got this:


           0.00
            0.01
            0.12
            1.23
            1.40
          123.45
         1234.56
        12345.67
            1.00
           12.00
          123.00
         1234.00
        12345.00
       123456.00
      1234567.00

    and actually to my understanding all the options given is wrong. Please tell me.

    I agree!
    I guess since character data is right aligned in MY grid displace it is misleading, but the STR command did not align.
    Oh, well.

    As previously stated, you need a fixed width font to get aligned results. Text results are usually configured to use a fixed width font, or you can change the one on the grid results.

    Works fine when results are sent to textTo be fair , the question stated right alignment and as demonstrated the answer doesn't achieve this. Relying on Font used isn't acceptable.

  • David Conn - Tuesday, January 24, 2017 3:04 AM

    BillLudlow - Tuesday, January 24, 2017 12:45 AM

    Luis Cazares - Monday, January 23, 2017 6:34 AM

    djj - Monday, January 23, 2017 5:35 AM

    Manie Verster - Sunday, January 22, 2017 11:24 PM

    Am I completely bonkers and am I the only one that read "right align"? I got this:


           0.00
            0.01
            0.12
            1.23
            1.40
          123.45
         1234.56
        12345.67
            1.00
           12.00
          123.00
         1234.00
        12345.00
       123456.00
      1234567.00

    and actually to my understanding all the options given is wrong. Please tell me.

    I agree!
    I guess since character data is right aligned in MY grid displace it is misleading, but the STR command did not align.
    Oh, well.

    As previously stated, you need a fixed width font to get aligned results. Text results are usually configured to use a fixed width font, or you can change the one on the grid results.

    Works fine when results are sent to text

    To be fair , the question stated right alignment and as demonstrated the answer doesn't achieve this. Relying on Font used isn't acceptable.

    Relying on font is the only way to format strings without modifying settings from the front end. It's like saying ASCII art shouldn't rely on font.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 15 posts - 1 through 14 (of 14 total)

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