Manage Learn to apply best practices and optimize your operations.

Create fixed length strings without using @if

How to create fixed length dates, invoice number, order numbers etc. padded out to the fixed length with leading zeroes.

How to create fixed length dates, invoice number, order numbers etc. that are padded out to the required fixed length with leading zeroes.

From time to time the need arises to create a string of a fixed length, be it a date that always has two characters for the day and month part or, perhaps, an invoice number to use as a key to get data from an SQL database.

In the Sametime database, stconf.nsf, I came across this fragment of code in a column formula to format dates as fixed length strings, with the month and day each forced to be two characters long:

 cMonth := @If(@Month(StartDateTime)<10;"0"+@Text(@Month(StartDateTime)); @Text(@Month(StartDateTime))); cDay := @If(@Day(StartDateTime)<10;"0"+@Text(@Day(StartDateTime)); @Text(@Day(StartDateTime)));

It does similar things for the hours and minutes and then adds the whole lot together to get a fixed length value.

A much more readable (and probably less processor intensive) way is to replace the above with this:

 cMonth := @Right("0" + @Text(@Month(StartDateTime)) ; 2); cDay := @Right("0" + @Text(@Day(StartDateTime)) ; 2);

In other words, always add a zero to the front of the string then take the rightmost two characters.

For example:

If the month is "2" then the above formula adds a zero to the front, making "02" then takes the rightmost two characters which, since there are only two characters, remains "02".

If the day is "11" then the above formula adds a zero to the front, making "011" then takes the rightmost two characters, still giving "11"

"But what about things like invoice or order numbers where you don't know their length in the first place, but they have to be padded out with leading zeroes?" I hear you cry. Nothing could be easier. For example, if the number has to be ten characters long, Simply add ten zeroes to the front of the number then take the rightmost ten characters:

 tInvoiceNum := @Right("0000000000" + InvoiceNum ; 10);

It doesn't matter whether InvoiceNum is 1, 3 or 7 characters in the first place, it will always be padded out with the correct number of leading zeroes.

Doing the same thing in LotusScript is just as easy. Here's a fragment of LotusScript from an agent in the same SameTime database:

 If Month(ServerDate.DateOnly) < 10 Then sMonth = "0" + Cstr(Month(ServerDate.DateOnly)) Else sMonth = Cstr(Month(ServerDate.DateOnly)) End If If Day(ServerDate.DateOnly) < 10 Then sDay = "0" + Cstr(Day(ServerDate.DateOnly)) Else sDay = Cstr(Day(ServerDate.DateOnly)) End If

This can be replaced by:

 sMonth = Right$("0" + Cstr(Month(ServerDate.DateOnly)) , 2) sDay = Right$("0" + Cstr(Day(ServerDate.DateOnly)) , 2)

Much shorter and easier to read!

Dig Deeper on Domino Resources - Part 3



  • iSeries tutorials's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...