Custom Auto Generated ID in SQL

Hi,

Can someone give me the simplest logic when it comes to generating custom id in sql?

I wanted to have an id like CTM201001-0001… The first 4 numbers from the left represents the year which is 2010, the next 2 numbers 01 represents the month of january then the last numbers after the dash sign is the number of record. It’s like for example :

CTM201001-0453: For the year of 2010, month of january, there are 453 records inputted…

My users are kinda requesting me an auto-generated customized id.

Now i believed that i’ll be using SUBSTRING… And another concern about the number of records is: What if the records will exceed
greater than the 4 digit?

I’m only having some ideas in mind and I’ll be needing some help about this as well.

Thanks in advance.
ishey

Re: Custom Auto Generated ID in SQL

The question that presents itself to me here is the first, is: Do you want this in PHP so solve Scriptcase or in the database? Furthermore, the question arises whether this ID as PrimaryKex the table should be saved or whether it should be spent only as information for the user?

Re: Custom Auto Generated ID in SQL

Are you bound to this format? or can it be any custom id that is unique?

For your format, you can use date(), substr(), str_pad() to format the values.
For the record you can use mysql_insert_id() to get the last value.

I would not limit yourself to 4 digits, unless you start over when you max, but then what is the use if you do.

Regards,
Scott.

Re: Custom Auto Generated ID in SQL

Yes, I’m bound for this format. Thanks for the suggestions.

This ID should be saved in the database as well. Yes, I’ll be using PHP to create an algorithm that will generate the said id.

I also have this one as a reference: http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server

Thanks
ishey

Re: Custom Auto Generated ID in SQL

Auto gen values are a dime a dozen, so that should not be any problem. As mentioned, the 4 digit limitation they gave you for record entries was not very forward thinking on their part. Try to talk them into a another digit or 2. Large Hard drives are standard now. What could be the reason not to? Unless they have an old system in place that can only read that format.

Perhaps this database will only have a few new records a week <bg>

Regards,
Scott.

Re: Custom Auto Generated ID in SQL

Hi

I am trying to create Serial No for each insert in the Editable Grid. Is there any way to do it…? or any logic…?

Regards
dhana

Re: Custom Auto Generated ID in SQL

Do they have to be sequential, or just unique, or …

Here are a few functions I have used before:

function random_code($len=8) {
return substr(strtoupper(base_convert(microtime(),10,16)),0,$len);
}

function random_ticket($len=6, $start=false, $end=false) {
mt_srand((double) microtime() * 1000000);
$start=(!$len && $start)?$start:str_pad(1,$len,“0”,STR_PAD_RIGHT);
$end=(!$len && $end)?$end:str_pad(9,$len,“9”,STR_PAD_RIGHT);

return mt_rand($start,$end);
}

$ticket = ‘R’.random_ticket(5).’-’.random_code(8);

Mix and match however you want.

Regards,
Scott.

Re: Custom Auto Generated ID in SQL

Scott,
Great…! works fine…! For Sequential…! I may have to write a function and increament the values

Thanks for your help

Regards
Dhana

Re: Custom Auto Generated ID in SQL

(This may or may not apply here, but I couldn’t find another place to post it and I thought it was good info for the topic)

I needed somthing similar, but I needed it to work in a 2-form process where Form1 was a “To Be Assigned” value shown to the user (noted that it was a ‘temporary’ number), and the actual number needed to relate to the actual primary_key ID number in the database.

Method Used:

Main Form1 in Single Record mode
Table with numeric “id” field (INT(11))
set to AUTOINCREMENT at the database level, primary_key

Form1 or Form2 do not actually edit any data for the ID field (Form1 Attribute Values for ‘id’ set to “Calculated by the database”)
2nd field used in this example is “idtxt” (a customized text version of the ID number)
Form1 does dynamically ‘insert’ the final value into the ‘idtxt’ field at the last moment, later read again by Form2

Form 1, onLoad event: (get the last order ID, increment it by +1, create a fixed-length text string)

sc_lookup(getidnew, "SELECT CONCAT('[s_dept]-',LEFT('0000000',8 - CHAR_LENGTH(id+1)),id+1) idtxt FROM orders ORDER BY id DESC LIMIT 1");

{idtxt} = {getidnew[0][0]};

(the concat statement above is for MySQL, not MSSQL)

(the ‘[s_dept]’ in the concat statement adds a pre-stored variable containing a 4-char sales department ID to the beginning of the auto-generated text id)

Then, to make sure another user hasn’t entered a form with the same ID (such as if you have 100 users entering data at the same time, AND you are NOT running Transactions, you can still re-query a new ID before inserting the final data) :

Form 1, onBeforeInsert event:
(run the same query as above!)

The resulting text should be:
If ID = 25, and [s_dept] = “DEPT”
idtxt = “DEPT-00000025”

A crude way to get there, but easy and effective, and very customizable for your text result! I’m sure it would be adaptable to a editable grid scenerio

Re: Custom Auto Generated ID in SQL

on events before insert :

$tgl = {tanggal} ;
$tahun = substr($tgl,2,2);
$bulan = substr($tgl,5,2);

$jenis = “HM”;

$tahunbulan = $jenis . $tahun . $bulan ;

sc_lookup(dataset,“select max((substr(noref,8,5))) from trhimpunan where substr(noref,1,6) = '” . $tahunbulan . “’”);

$proses1 = {dataset}[0][0];

$jml = $proses1 + 1 ;

$hasil = $tahunbulan . “/” . str_pad($jml,5, “0”, STR_PAD_LEFT);

{noref} = $hasil;