Re: Random Number SKU Generator

From: "Peter Dabrowski" <meritage(at)mail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Random Number SKU Generator
Date: 2015-02-15 03:22:55
Message-ID: trinity-16649c8a-9767-40d2-b838-837271170810-1423970575413@3capp-mailcom-lxa08
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>
<div>Thank you Roxane,</div>

<div>&nbsp;</div>

<div>Tested it with db and returnig as expected.</div>

<div>&nbsp;</div>

<div>Best Regards</div>

<div>Peter</div>

<div>&nbsp;
<div name="quote" style="margin:10px 5px 5px 10px; padding: 10px 0 10px 10px; border-left:2px solid #C3D9E5; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="margin:0 0 10px 0;"><b>Sent:</b>&nbsp;Sunday, February 15, 2015 at 1:50 PM<br/>
<b>From:</b>&nbsp;&quot;Roxanne Reid-Bennett&quot; &lt;rox(at)tara-lu(dot)com&gt;<br/>
<b>To:</b>&nbsp;pgsql-novice(at)postgresql(dot)org<br/>
<b>Subject:</b>&nbsp;Re: [NOVICE] Random Number SKU Generator</div>

<div name="quoted-content">
<div style="background-color: rgb(255,255,255);">
<div class="moz-cite-prefix">On 2/14/2015 6:34 PM, Peter Dabrowski wrote:</div>

<blockquote>
<div style="font-family: Verdana;font-size: 12.0px;">
<div>
<div>The purpose is to have 5 digit unique random SKU number&nbsp; &quot;00000&quot; which could be &quot;56713&quot;&nbsp; or &quot;00001&quot;</div>

<div>&nbsp;initially i got this inserted into sku as default:</div>

<div>
<pre>ltrim(to_char(luhn_generate(round(random()*10000)::int), &#39;00000&#39;))</pre>
</div>

<div>and the luhn_generate function is creating&nbsp; number that was 5 digit in length &quot;00000&quot; so if a number was smaller than 5 digits always was returning with leading zero&#39;s &quot;00042&quot;</div>
</div>
</div>
</blockquote>
<br/>
change the &quot;skuGen :=&quot;&nbsp; line to:<br/>
<br/>
skuGen := to_char(luhn_generate(round(random()*10000)::int), &#39;00000&#39;);<br/>
<br/>
You don&#39;t need the ltrim, but you do need the to_char.<br/>
<br/>
Roxanne
<blockquote>
<div style="font-family: Verdana;font-size: 12.0px;">
<div>
<div>&nbsp;
<div style="margin: 10.0px 5.0px 5.0px 10.0px;padding: 10.0px 0 10.0px 10.0px;border-left: 2.0px solid rgb(195,217,229);">
<div style="margin: 0 0 10.0px 0;"><b>Sent:</b>&nbsp;Sunday, February 15, 2015 at 3:10 AM<br/>
<b>From:</b>&nbsp;&quot;Roxanne Reid-Bennett&quot; <a class="moz-txt-link-rfc2396E" href="rox(at)tara-lu(dot)com" target="_parent">&lt;rox(at)tara-lu(dot)com&gt;</a><br/>
<b>To:</b>&nbsp;<a class="moz-txt-link-abbreviated" href="pgsql-novice(at)postgresql(dot)org" target="_parent">pgsql-novice(at)postgresql(dot)org</a><br/>
<b>Subject:</b>&nbsp;Re: [NOVICE] Random Number SKU Generator</div>

<div>
<div style="background-color: rgb(255,255,255);">
<div class="moz-cite-prefix">On 2/14/2015 10:12 AM, Roxanne Reid-Bennett wrote:</div>

<blockquote>
<div class="moz-cite-prefix">On 2/14/2015 6:48 AM, Peter Dabrowski wrote:</div>

<blockquote>
<pre>I found one issue,
The leading zeros are removed. expected number &quot;00042&quot; printed &quot;42&quot;

Regards
Peter </pre>
</blockquote>
</blockquote>
hmm.. belay that.<br/>
I stripped the to_char&nbsp; out - which was using the &#39;00000&#39;.&nbsp; My draft code to develop the routine was generating a numeric that was 9 digits in length.&nbsp; to_char(x,&#39;00000&#39;) was returning &#39;#####&#39;<br/>
<br/>
But my question still stands - what is the purpose of the ltrim?&nbsp;<br/>
What is the maximum number of digits that your luhn_generate function is creating?<br/>
<br/>
Roxanne<br/>
&nbsp;
<blockquote>That would be the ltrim(<tt class="PARAMETER">string</tt>,<tt class="PARAMETER">characters</tt>) function.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.g. &quot;Remove the longest string containing <b>only </b>characters from <tt class="PARAMETER">characters</tt> from the start of <tt class="PARAMETER">string&quot;</tt><br/>
&nbsp;For example, taking the example from the docs:<br/>
<br/>
select ltrim(&#39;zzzytrim&#39;, &#39;xyz&#39;)<br/>
<br/>
returns trim<br/>
<br/>
&nbsp;- so the ltrim(x,&#39;00000&#39;) from your original ALTER will remove all leading zeros.&nbsp;<br/>
<br/>
What was the intended purpose of ltrim in your pseudo code?<br/>
<br/>
Roxanne
<blockquote>
<pre>&nbsp;
&nbsp;

Sent:&nbsp;Saturday, February 14, 2015 at 9:23 PM
From:&nbsp;&quot;Peter Dabrowski&quot; <a class="moz-txt-link-rfc2396E">&lt;meritage(at)mail(dot)com&gt;</a>
To:&nbsp;<a class="moz-txt-link-abbreviated">pgsql-novice(at)postgresql(dot)org</a>
Subject:&nbsp;Re: [NOVICE] Random Number SKU Generator

Thank you Roxane,
Applied the code against db, it is generating Random SKU and I did not to have any errors to report.
So far i&#39;ts very good.
Best Regards
Peter
&nbsp;

Sent:&nbsp;Saturday, February 14, 2015 at 2:20 AM
From:&nbsp;&quot;Roxanne Reid-Bennett&quot; <a class="moz-txt-link-rfc2396E">&lt;rox(at)tara-lu(dot)com&gt;</a>
To:&nbsp;<a class="moz-txt-link-abbreviated">pgsql-novice(at)postgresql(dot)org</a>
Subject:&nbsp;Re: [NOVICE] Random Number SKU Generator

On 2/13/2015 4:46 AM, Peter Dabrowski wrote:

I use a Luhn algorithm to generete random number, inserted below sql code to column &#39;SKU&quot;
&nbsp;
ALTER TABLE m_product ALTER sku SET DEFAULT ltrim(to_char(luhn_generate(round(random()*10000)::int), &#39;00000&#39;))
&nbsp;
as a result on record saving random number is generated, but my problem is the numbers are not unique.
&nbsp;
Somone sugest that I&nbsp; should write a function to encapsulate &quot;ltrim(to_char(luhn_generate(
round(random()*10000)::int), &#39;00000&#39;))&quot; &nbsp;and at the same time, check if the value is already used in the table.
&nbsp;
in pseudo code it would look something like this:
&nbsp;
generateMProductSKU(){
&nbsp;&nbsp;&nbsp; skuGen=&quot;&quot;
&nbsp;&nbsp;&nbsp; needToGenerate = true
&nbsp;&nbsp;&nbsp; while(needToGenerate){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; skuGen = ltrim(to_char(luhn_generate(round(random()*10000)::int), &#39;00000&#39;))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if((Select count(*) from M_Product where sku =skuGen) = 0
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; needToGenerate =&nbsp; false
&nbsp;&nbsp; }
&nbsp;&nbsp; return skuGen
}
&nbsp;
Culd somebody help me tu structure code into right sql format so it could be inserted into database.
&nbsp;
Thank you very much.
Peterperhaps this?

create or replace function generateMProductSKU()
RETURNS text AS
&#36;BODY&#36;

DECLARE
&nbsp;&nbsp;&nbsp; skuGen text;
&nbsp;&nbsp;&nbsp; needToGenerate boolean;
BEGIN
&nbsp;&nbsp;&nbsp; skuGen := &#39;&#39;;
&nbsp;&nbsp;&nbsp; needToGenerate := true;

&nbsp;&nbsp;&nbsp; WHILE needToGenerate LOOP
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; skuGen := ltrim(luhn_generate(round(random()*10000)::int)::text, &#39;00000&#39;);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SELECT count(*) != 0 into needToGenerate from m_product where sku = skuGen;
&nbsp;&nbsp;&nbsp; END LOOP;

&nbsp;&nbsp;&nbsp; return skuGen;
END
&#36;BODY&#36;
&nbsp; LANGUAGE &#39;plpgsql&#39; STABLE;

ALTER TABLE m_product ALTER sku SET DEFAULT generateMProductSKU();
&nbsp;
</pre>
</blockquote>
&nbsp;

<pre class="moz-signature">--
[At other schools] I think the most common fault in general is to teach students how to pass exams instead of teaching them the science.
Donald Knuth</pre>
</blockquote>
&nbsp;

<pre class="moz-signature">--
[At other schools] I think the most common fault in general is to teach students how to pass exams instead of teaching them the science.
Donald Knuth</pre>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
&nbsp;

<pre class="moz-signature">--
[At other schools] I think the most common fault in general is to teach students how to pass exams instead of teaching them the science.
Donald Knuth</pre>
</div>
</div>
</div>
</div>
</div></div></body></html>

Attachment Content-Type Size
unknown_filename text/html 7.4 KB

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message David G Johnston 2015-02-15 09:22:58 Re: Random Number SKU Generator
Previous Message Roxanne Reid-Bennett 2015-02-15 02:50:44 Re: Random Number SKU Generator