Home > Domino Tips > Developer > Other > Create fixed length strings without using @if
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

OTHER

Create fixed length strings without using @if


Rob Porter
12.17.2001
Rating: -3.87- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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



Code

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!


Rate this Tip
To rate tips, you must be a member of SearchDomino.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Other
Create a dynamic user-driven navigator for a Notes/Domino application
How to apply XSL style sheets to XML views
Comparing replicas on clustered Lotus Domino servers
Creating a Lotus Notes view column categorized by month
Using the XMLHTTP object for integration with Domino or any RDBMS back end
Hiding field properties/data from DocProperties box
Export a view to Excel without coding
Prevent document deletion if there are response documents
Switching between test IDs quickly
AddParameter to a NotesXSLTransFormer

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts