Professional Web Applications Themes

str function adds a space - Microsoft SQL / MS SQL Server

Hello there, I can't figure out why there is a space added in this query result: SELECT Isbn + '-' + STR(Specimen) AS IsbnX suppose Isbn (varchar 20) is "1234567890" and Specimen (bigint) is 25 the expected result is "1234567890-25" instead I get "1234567890- 25" what gives?...

  1. #1

    Default str function adds a space

    Hello there,

    I can't figure out why there is a space added in this query result:

    SELECT Isbn + '-' + STR(Specimen) AS IsbnX

    suppose Isbn (varchar 20) is "1234567890"
    and Specimen (bigint) is 25

    the expected result is "1234567890-25"
    instead I get "1234567890- 25"

    what gives?



    Browser Guest

  2. #2

    Default Re: str function adds a space

    According the Books Online, the length of the value returned by STR is
    10 so you should get 8 leading spaces as illustrated by the following:

    DECLARE Isbn varchar (20)
    DECLARE Specimen bigint
    SET Isbn = '1234567890'
    SET Specimen = 25
    SELECT Isbn + '-' + STR(Specimen) AS IsbnX

    Results:

    IsbnX
    -------------------------------
    1234567890- 25

    You might consider using CAST or CONVERT instead so you'll get the
    results you expect::

    SELECT Isbn + '-' + CAST(Specimen AS varchar(20)) AS IsbnX

    --
    Hope this helps.

    Dan Guzman
    SQL Server MVP

    -----------------------
    SQL FAQ links (courtesy Neil Pike):

    http://www.ntfaq.com/Articles/Index.cfm?DepartmentID=800
    http://www.sqlserverfaq.com
    http://www.mssqlserver.com/faq
    -----------------------

    "Browser" <com> wrote in message
    news:xGe2b.2490$bellglobal.com... 


    Dan Guest

  3. #3

    Default Re: str function adds a space

    Browser,

    It is doented in BOL
    ( http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp )

    STR ( float_expression [ , length [ , decimal ] ] )
    ".. length Is the total length, including decimal point, sign, digits, and spaces. The default is 10"

    Easy enough to work around - either supply the optional length and decimal arguments to str(),
    and/or use ltrim()

    Regards
    AJ

    "Browser" <com> wrote in message news:xGe2b.2490$bellglobal.com... 


    Andrew Guest

Similar Threads

  1. Replies: 2
    Last Post: February 2nd, 09:09 PM
  2. Losing my option key / space bar function in AI 10
    By Matt_Lorenzi@adobeforums.com in forum Adobe Illustrator Macintosh
    Replies: 0
    Last Post: April 15th, 09:18 PM
  3. SFW adds hyphen instead of space
    By Robert in forum Adobe Photoshop 7, CS, CS2 & CS3
    Replies: 11
    Last Post: September 24th, 12:03 PM
  4. Query yzer adds extra space/newlines
    By Mark in forum Microsoft SQL / MS SQL Server
    Replies: 1
    Last Post: July 15th, 08:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139