<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.developerdotstar.com/community" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>developer.* Blogs - SQL Server T-SQL LPAD &amp;amp; RPAD Functions (String Padding Equivalent to PadLeft &amp;amp; PadRight) - Comments</title>
 <link>http://www.developerdotstar.com/community/node/319</link>
 <description>Comments for &quot;SQL Server T-SQL LPAD &amp; RPAD Functions (String Padding Equivalent to PadLeft &amp; PadRight)&quot;</description>
 <language>en</language>
<item>
 <title>Also check out a related</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-3842</link>
 <description>&lt;blockquote&gt;&lt;p&gt;Also check out a related post, &quot;Hard Coded Limits Considered Harmful,&quot; on my blog.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Because there is an alternative to the hard coded values present in the functions? I don&#039;t see one...&lt;/p&gt;
</description>
 <pubDate>Wed, 24 Jan 2007 23:09:10 -0800</pubDate>
 <dc:creator>Guest</dc:creator>
 <guid isPermaLink="false">comment 3842 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>Hard Coded Limits</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-1399</link>
 <description>&lt;p&gt;To clarify Dan&#039;s comment, SQL Server ints can be up to 10 characters.&lt;/p&gt;
&lt;p&gt;Also check out a related post, &lt;a href=&quot;http://www.developerdotstar.com/community/node/551&quot;&gt;&quot;Hard Coded Limits Considered Harmful&lt;/a&gt;,&quot; on my blog.&lt;/p&gt;
&lt;p&gt;Edward&lt;/p&gt;
</description>
 <pubDate>Wed, 16 Aug 2006 21:13:19 -0700</pubDate>
 <dc:creator>Edward G Nilges</dc:creator>
 <guid isPermaLink="false">comment 1399 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>Cool!</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-1395</link>
 <description>&lt;p&gt;Nice function. Thanks.&lt;/p&gt;
</description>
 <pubDate>Wed, 16 Aug 2006 06:34:56 -0700</pubDate>
 <dc:creator>Guest</dc:creator>
 <guid isPermaLink="false">comment 1395 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>Length not issue for us</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-1345</link>
 <description>&lt;p&gt;Our member numbers are all 6 characters, so I don&#039;t need to worry about anything larger.  Infact our lowest numbers are still 3 digits so I could simply append 3 zeros to the leading edge.&lt;/p&gt;
&lt;p&gt;I never considered creating a function, this is just a quick fix from our new database system to feed the web SQL box until we get the new system completely up and running.&lt;/p&gt;
</description>
 <pubDate>Fri, 04 Aug 2006 13:46:45 -0700</pubDate>
 <dc:creator>Mike</dc:creator>
 <guid isPermaLink="false">comment 1345 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>Looks Like a Viable Solution</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-1344</link>
 <description>&lt;p&gt;I can&#039;t say whether or not this is particularly inefficient, but it looks like it should  work fine as long as your company ID stays bellow 1,000,000. I believe a SQL Server int (&amp;gt; 0) can be up to 9 characters (?). Beyond that, though, I can&#039;t see anything wrong with it on the surface, especially if it were generalized into it&#039;s own function that took an int argument and returned a string. If you use this technique inline, it&#039;s less communicative than a function call would be.&lt;/p&gt;
&lt;p&gt;Assuming you removed the limit on the 6 characters and generalized it into a function, it would be interesting to compare the two solutions for performance. Thanks for contributing this alternate solution.&lt;/p&gt;
&lt;p&gt;Dan&lt;/p&gt;
</description>
 <pubDate>Fri, 04 Aug 2006 13:43:11 -0700</pubDate>
 <dc:creator>Daniel Read</dc:creator>
 <guid isPermaLink="false">comment 1344 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>Anything wrong with my method</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-1343</link>
 <description>&lt;p&gt;I needed an int converted into a varchar of 6 characters with leading zeros.  Add 6 zeros to the left of the converted int, then just take the 6 characters on the right.&lt;/p&gt;
&lt;p&gt;RIGHT(&#039;000000&#039; + CAST(dbo.Company.ID AS VARCHAR), 6) AS ID&lt;/p&gt;
&lt;p&gt;Is this terribly ineffecient?&lt;/p&gt;
</description>
 <pubDate>Fri, 04 Aug 2006 13:05:36 -0700</pubDate>
 <dc:creator>Mike</dc:creator>
 <guid isPermaLink="false">comment 1343 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>LPAD and RPAD</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-1242</link>
 <description>&lt;p&gt;the RPAD and LPAD functions in SQL Server are very clever and appear to work perfectly. This is for non-commercial work, but I have given you credit in the code. Thanks!&lt;/p&gt;
&lt;p&gt;scott&lt;br /&gt;
Prairie Heart Institute&lt;/p&gt;
</description>
 <pubDate>Mon, 03 Jul 2006 11:58:22 -0700</pubDate>
 <dc:creator>Guest</dc:creator>
 <guid isPermaLink="false">comment 1242 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>thanks</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-1196</link>
 <description>&lt;p&gt;Excellent function.  Worked like a charm.&lt;/p&gt;
</description>
 <pubDate>Fri, 16 Jun 2006 10:58:17 -0700</pubDate>
 <dc:creator>Patrick</dc:creator>
 <guid isPermaLink="false">comment 1196 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>Spaces - varchar issue?</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-1087</link>
 <description>&lt;p&gt;Hmmmm...could your right-padded spaces be disappearing because you&#039;re storing the string in a varchar variable? I believe right-padded spaces in a varchar are automatically removed. If this is the case, you might try storing your values in char variables/columns while processing your file export. Or have I misunderstood your issue?&lt;/p&gt;
&lt;p&gt;Hope that helps,&lt;br /&gt;
Dan&lt;/p&gt;
</description>
 <pubDate>Fri, 12 May 2006 08:03:38 -0700</pubDate>
 <dc:creator>Daniel Read</dc:creator>
 <guid isPermaLink="false">comment 1087 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>RPAD with spaces is assumed as null</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-1085</link>
 <description>&lt;p&gt;I appreciate the effort in generating the function above. However, when trying to generate a fixed width file, one has to right pad alpha fields with spaces. Unfortunately, the spaces are treated like  and are truncated when inserting into a field.  Do you have a method to rpad spaces to an alpha field?  I look forward to your feedback.&lt;/p&gt;
&lt;p&gt;Caesar&lt;/p&gt;
</description>
 <pubDate>Fri, 12 May 2006 07:21:24 -0700</pubDate>
 <dc:creator>Caesar</dc:creator>
 <guid isPermaLink="false">comment 1085 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>Oh, sorry, there they are and they are kewl</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-752</link>
 <description>&lt;p&gt;I do like the formality of saying, here is the function and it merits a change record.&lt;/p&gt;
&lt;p&gt;Centering is another alignment task where you divide the difference between the length of a string and the (usually larger) length of a container by 2 and offset the string in the area by the quotient. You use the integer floor of the quotient when there is an odd number because n monospace spaces to the left and n+1 look more natural.&lt;/p&gt;
&lt;p&gt;I coded an align routine which depending on a parameter will left, right or center justify a string within another string early in my VB addiction and this legacy code survives in the utilities.DLL for Build Your Own .Net Language and Compiler.&lt;/p&gt;
&lt;p&gt;In Rexx I added the ability to justify code with even boundaries on either side. Unfortunately this, and to a degree centering, is at best workable &quot;typesetting&quot; only in the wonderful world of monospace fonts like Courier New, where all characters are of identical width, and you can make documents that are &quot;beautiful&quot; only in typewriter terms.&lt;/p&gt;
&lt;p&gt;Such a low skill is useful still in programming when writing error reports, logs, and ransom notes while REAL computerised typesetting is vastly more complex.&lt;/p&gt;
&lt;p&gt;Indeed, during my early addiction to VB I contemplated writing a typesetting package and little language exclusively for monospace but soon enough realized that this would be a Plan Nine from Outer Space.&lt;/p&gt;
&lt;p&gt;However, I have written code to place messages in asterisk boxes.&lt;/p&gt;
</description>
 <pubDate>Sat, 07 Jan 2006 00:56:46 -0800</pubDate>
 <dc:creator>Edward G Nilges</dc:creator>
 <guid isPermaLink="false">comment 752 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>Dig the New UDFs</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-748</link>
 <description>&lt;p&gt;Your point about the &quot;magic number&quot; literal value of 10 is correct, Edward, but it was really just an example. My example was assuming a single SQL statement and I wanted to keep it simple, so I used the magic number. Unfortunately T-SQL does not support named constants explicitly, though you can approximate them with variables. Often, though, I solve the magic number problem in SQL with a single line comment above the line that uses the magic number to explain its meaning. If the code is within a T-SQL stored procedure, though, I&#039;ll create a pseudo-named-constant variable in place of the literal.&lt;/p&gt;
&lt;p&gt;Also, in my post I suggested using branching logic rather than ABS() to handle the negative value possibility because after thinking about it I decided ABS() around the formula would assume too much about the intent of the caller by returning a string that is fewer characters than requested. My example does not contain the ABS() function because I wanted to leave that decision up to the coder to solve. I&#039;ve added two user defined functions, and you can see there how I&#039;ve decided to handle the negative problem. Someone else might prefer to use ABS() instead of the branching logic that I&#039;ve implemented. Someone else might prefer to raise an error instead of just returning back the base string, as I have done in my UDFs.&lt;/p&gt;
&lt;p&gt;Thanks for your interest,&lt;br /&gt;
Dan&lt;/p&gt;
</description>
 <pubDate>Fri, 06 Jan 2006 09:44:51 -0800</pubDate>
 <dc:creator>Daniel Read</dc:creator>
 <guid isPermaLink="false">comment 748 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>Thanks, Donna</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-747</link>
 <description>&lt;p&gt;I&#039;ll be doing a lot of SQL Server 2005 and SSIS work in the coming weeks, so I plan to continue blogging about it as I come up with things that might be helpful to other people. Have I recommended &lt;a href=&quot;http://www.amazon.com/exec/obidos/ASIN/0672327783/developerdots-20&quot;&gt;this excellent book yet for getting up to speed with SQL Server 2005&lt;/a&gt;? It&#039;s called &lt;a href=&quot;http://www.amazon.com/exec/obidos/ASIN/0672327783/developerdots-20&quot;&gt;&lt;i&gt;Microsoft SQL Server 2005: Changing the Paradigm&lt;/i&gt;&lt;/a&gt;. It covers things from both the DBA and development perspectives, and has a very nice balance between giving high level strategic and tactical information about all the new stuff and getting into the weeds a little with some how-to and reference.&lt;/p&gt;
&lt;p&gt;Dan&lt;/p&gt;
</description>
 <pubDate>Fri, 06 Jan 2006 09:35:52 -0800</pubDate>
 <dc:creator>Daniel Read</dc:creator>
 <guid isPermaLink="false">comment 747 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>OK, but...</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-746</link>
 <description>&lt;p&gt;The 10 is hard coded. If it is a parameter or even defined as a symbolic constant PAD_MAX which is probably good practice in the surrounding programming language, it becomes a teensy hard to &quot;stuff&quot; into the SQL and the SQL&#039;s intent is obscured thereby. Which makes it a shame that T-SQL doesn&#039;t implement a pad() function. Failing this, it would be nice, I think, if T-SQL had the general ability to define user functions.&lt;/p&gt;
&lt;p&gt;If quantity is negative, the code doesn&#039;t work. Here you need an absolute value function (is this provided in your SQL?) AND a SQL version of the VB Iif (or the C ?: operator, a fun little bit of strangeness) if you are going to stay in one SQL call. If the call goes to the server, you need to.&lt;/p&gt;
&lt;p&gt;As it is you are going to get 00000-12345 and other strange outputs if this is an order application and there are &quot;returns&quot;.&lt;/p&gt;
&lt;p&gt;It&#039;s enough to drive a man either to drink, or else to writing his Own Goddamn SQL (OGSQL). That&#039;s been done, of course, but by somebunny else.&lt;/p&gt;
</description>
 <pubDate>Thu, 05 Jan 2006 23:16:06 -0800</pubDate>
 <dc:creator>Edward G Nilges</dc:creator>
 <guid isPermaLink="false">comment 746 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>Keep serving that SQL</title>
 <link>http://www.developerdotstar.com/community/node/319#comment-742</link>
 <description>&lt;p&gt;Dan, I (for one) am appreciating your recent posts involving SQL Server. There&#039;s not a day that I don&#039;t go into Enterprise Manager (in the 2000 realm) and really need to wade into the 2005 waters.&lt;/p&gt;
</description>
 <pubDate>Thu, 05 Jan 2006 18:16:01 -0800</pubDate>
 <dc:creator>Donna L Davis</dc:creator>
 <guid isPermaLink="false">comment 742 at http://www.developerdotstar.com/community</guid>
</item>
<item>
 <title>SQL Server T-SQL LPAD &amp; RPAD Functions (String Padding Equivalent to PadLeft &amp; PadRight)</title>
 <link>http://www.developerdotstar.com/community/node/319</link>
 <description>&lt;p&gt;Here is my method for achieving string padding in the Microsoft SQL Server T-SQL language. Unfortunately T-SQL does not offer functions like Oracle PL/SQL&#039;s LPAD() and RPAD() and C#&#039;s PadLeft() and PadRight() functions. However, you can achieve the same thing using the T-SQL REPLICATE and LEN functions.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.developerdotstar.com/community/node/319&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://www.developerdotstar.com/community/node/319#comment</comments>
 <category domain="http://www.developerdotstar.com/community/taxonomy/term/30">Microsoft SQL Server</category>
 <category domain="http://www.developerdotstar.com/community/taxonomy/term/26">SQL</category>
 <pubDate>Thu, 05 Jan 2006 11:31:59 -0800</pubDate>
 <dc:creator>Daniel Read</dc:creator>
 <guid isPermaLink="false">319 at http://www.developerdotstar.com/community</guid>
</item>
</channel>
</rss>
