Add white space

  • Hello,

    Can anyone please let me know, how to add as many white spaces as the length of text in select statement. Please see the following example:

    Col1 Col2

    ---- ----

    ABC (I want to count Col1 value ABC , which is 3 and then append it to the Col1 text)

    Thanks.

  • Sounds like a use for the replicate function.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • SPACE can do it for you

    SELECT SPACE( LEN( Col1)) + Col1 from <your table>

    And as Wayne suggested, u can use REPLICATE as well.. like:

    SELECT REPLICATE(' ' , LEN(col1)) + Col1 from <your table>

    ~Edit : Initially suggested STUFF ,which, of course is a dumb-*** suggestion.. sorry folks.. now changed it to SPACE function..

  • Thank you guys. It worked fine. 😉

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

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