{"id":50,"date":"2008-04-28T15:22:23","date_gmt":"2008-04-28T21:22:23","guid":{"rendered":"https:\/\/jackdonnell.com\/?p=50"},"modified":"2008-04-30T01:38:48","modified_gmt":"2008-04-30T07:38:48","slug":"scalar-function-to-add-leading-zeros-to-text","status":"publish","type":"post","link":"https:\/\/jackdonnell.com\/?p=50","title":{"rendered":"T-SQL: Adding Leading Zeros with a Simple Scalar Function"},"content":{"rendered":"<p>We were creating a extract at work from T-SQL and new that the we would need to export data with leading zeros. The data was not stored in the database with those leading zero values. The solution, create a simple scalar function to dynamically add the zeros that could accept two parameters ( text , and total length).<\/p>\n<p>The Function is rather simple takes the total length value passed by the user and then subtracts the Length of the the text value.<\/p>\n<p>What would make this better?<\/p>\n<ul>\n<li>Would have a default value for total length<\/li>\n<li>Error checking to make sure the text supplied is not longer than the total length<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p><code><br \/>\nSET ANSI_NULLS ON<br \/>\nGO<br \/>\nSET QUOTED_IDENTIFIER ON<br \/>\nGO<br \/>\n-- =============================================<br \/>\n-- Author: Jack C Donnell<br \/>\n-- Create date: 2008-04-28<br \/>\n-- Description: Adds leading Zeros to CHAR<br \/>\n-- =============================================<br \/>\nALTER FUNCTION dbo.AddLeadingZeros<br \/>\n(<br \/>\n@textString NVARCHAR(100) ,@columnLEN int<br \/>\n)<br \/>\nRETURNS NVARCHAR(100)<br \/>\nAS<br \/>\nBEGIN<br \/>\n-- Declare the return variable here<br \/>\nDECLARE @ResultValue NVARCHAR(100)<br \/>\n,@i as INT<br \/>\n,@findLen as INT<br \/>\n-- Add the T-SQL statements to compute the return value here<br \/>\nSET @i = 1<br \/>\nSELECT @findLen = @columnLEN - LEN(@textString)<br \/>\nWHILE @i &lt;= @findLen<br \/>\nBEGIN<br \/>\nSET @textString = '0' + @textString<br \/>\nSET @i = @i + 1<br \/>\nEND<br \/>\nSET @ResultValue = @textstring<br \/>\n-- Return the result of the function<br \/>\nRETURN RTRIM(@ResultValue)<br \/>\nEND<br \/>\nGO<br \/>\n<\/code><\/p>\n<div><code>How Used :<\/code><\/div>\n<p>\u00a0<\/p>\n<div><code>--Submit Character Value and Length<\/code><\/div>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>Select dbo.AddLeadingZeros(&#8216;CVND&#8217;,15) as<\/p>\n<p>[4 Alph Chars with 11 Zeros=15]<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 4 Alph Chars with 11 Zeros=15<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00000000000CVND<\/p>\n<p>&#8211;Submit Numeric Value and Length<\/p>\n<p>SELECT dbo.AddLeadingZeros(874598,12) as [6 Num with 6 Zeros=12]<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 6 Num with 6 Zeros=12<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 000000874598<\/p>\n<p>\u00a0<\/p>\n<p>\ufffd<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We were creating a extract at work from T-SQL and new that the we would need to export data with leading zeros. The data was not stored in the database with those leading zero values. The solution, create a simple &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/jackdonnell.com\/?p=50\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[138,113,5],"tags":[199,200,291,198],"class_list":["post-50","post","type-post","status-publish","format-standard","hentry","category-programming","category-reportingbi","category-t-sql","tag-function","tag-scalar","tag-t-sql","tag-zero"],"_links":{"self":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=50"}],"version-history":[{"count":0,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/50\/revisions"}],"wp:attachment":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}