locked
STR inserting extra space characters RRS feed

  • Question

  • Hi

    When I use this script, the output insert 8 space charaacters. Does anyone know why and how I can get around this.

    DECLARE

     

    @status int

    SET @status=1

    PRINT STR

    (@status)

    Typical output is :-

             1

    Thanks

    Matthew

     
    Wednesday, March 23, 2011 2:26 PM

Answers

  • BOL

    The STR function converts numbers to characters, with optional parameters for specifying the total length of the result, including the decimal point and the number of places after the decimal point.

    Length and decimal parameters to STR, if supplied, should be positive. The default length is 10. The number is rounded to an integer either by default or if the decimal parameter is 0.

    DECLARE @status int

    SET

    @status=1

    SELECT

    STR(@status,1,1

    )


    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    • Marked as answer by KJian_ Tuesday, March 29, 2011 6:19 AM
    Wednesday, March 23, 2011 2:34 PM
    Answerer
  • Please see BOL definition for STR function. By default the length of the result is 10, so it's padded with spaces on the left for numbers.
    For every expert, there is an equal and opposite expert. - Becker's Law

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    • Marked as answer by KJian_ Tuesday, March 29, 2011 6:19 AM
    Wednesday, March 23, 2011 2:34 PM

All replies

  • because the output of STR is string and is right justified.
    DVR
    Wednesday, March 23, 2011 2:30 PM
  • BOL

    The STR function converts numbers to characters, with optional parameters for specifying the total length of the result, including the decimal point and the number of places after the decimal point.

    Length and decimal parameters to STR, if supplied, should be positive. The default length is 10. The number is rounded to an integer either by default or if the decimal parameter is 0.

    DECLARE @status int

    SET

    @status=1

    SELECT

    STR(@status,1,1

    )


    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    • Marked as answer by KJian_ Tuesday, March 29, 2011 6:19 AM
    Wednesday, March 23, 2011 2:34 PM
    Answerer
  • Please see BOL definition for STR function. By default the length of the result is 10, so it's padded with spaces on the left for numbers.
    For every expert, there is an equal and opposite expert. - Becker's Law

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    • Marked as answer by KJian_ Tuesday, March 29, 2011 6:19 AM
    Wednesday, March 23, 2011 2:34 PM
  •  

    DECLARE @status int

    SET @status=1

    PRINT cast(@status as varchar(10))

    Wednesday, March 23, 2011 5:57 PM