how to add leading zero to a text field in crystal reports - crystal-reports

I'm trying to think of a way to add a leading zero to a string field value.
For example I have 12345 but need a formula that would convert it to 012345. I'm new to Crystal as I know that this is probably a simple formula but cant seem to get it to work.
12345 => 012345 (add leading zero to make it 6 chars)
Thanks in advance.

Try this
totext(your_number, "000000");
1st arg.: Well, it's the input.
2nd arg.: Number of digits you need in the output.
For ex,
num = 1234;
totext(num, "000000");
Output:
001234
And, if you want to add a fixed number of zeroes, then you can just use (to add 3 leading zeroes):
"000" + totext(your_number, 0); // to add 3 leading zeroes
Note: The final output is a string not a number.

To pad a numeric string value with zeroes to a certain length:
local numbervar yournum := tonumber({table.your_string}); //convert to number
totext(yournumnum, '000000') //convert back to padded string of length 6
OR for a universal string
local stringvar yourstring:= {table.your_string};
local numbervar LENGTH := 10; //The desired padded string length
if length(yourstring) >= LENGTH then yourstring else
replicatestring('0',LENGTH-length(yourstring)) + yourstring

how to use the formula in your crystal report,
to put below mention formula in ( open (field explorer)-->then right click(Formula field)-->select new--->create formula name(xyz)-->then put
** ToText({DataTable1.Sr_No},"0000") **
copy/past only bold text between ** and then save and close
after that formula you add to your crystal report and run, your required answer is there,
Sr_No.
0001
0002
0003

just put on your field '0'+yourstringchar

Above the logic works and its a generic formula to left padding
if you want right padding use this
local stringvar yourstring:= {table.your_string};
local numbervar LENGTH := 10; //The desired padded string length
if length(yourstring) >= LENGTH
then yourstring
else
yourstring + replicatestring('0',LENGTH-length(yourstring))
--kanthi

Right("00000000"&ToText({Table.correla}),8)

I use this:
replace((space((20)-len({Table.Field}))+({Table.Field}))," ","0")
Basically it makes leading spaces, but then replaces those spaces with zeros. By subtracting the length of the field from the added spaces, the field will always be the number of characters specified in space((XX)...
It looks overly-complex, but it actually simplifies reports that require numerous fixed-length fields. As I develop the report, I can copy-paste this code to a new formula, change the number of spaces to match the required field length, then change my field name.

Related

mask a field with no fixed length in crystal reports 2008

I have a field in crystal reports that displays customer's names. the requirement however is to just display the first 5 characters, which includes spacing as well. whatever comes after the 5th character will be converted into * and the spacing shall be displayed as a space still. as the name field varies, how do i accomplish this?
currently i only have this, which displays the first 5 characters and replaces only the 6th character with an *
Replace({Command.CUST_NAME},MID({Command.CUST_NAME},6),"*")
Unfortunately, it seems Crystal does not suport regex.
So, i would do a function. Try this:
stringvar input := {Command.CUST_NAME};
stringvar output := MID(input, 1, 5);
numbervar i;
for i:=5 to Length (input) step 1 do (
stringvar aChar := MID(input, i, 1);
if aChar <> " " then aChar := "*";
output := output + aChar;
);
output
Maybe you will have to handle if {Command.CUST_NAME} is smaller than 5 chars.

How to add zeros from left side to reight side for a given number

can you please convert the below left hand side zeros to right hand.
example:
Number output
0012345 1234500
00008794 87940000
Please help me which function will give the above result.
I am assuming that Number is of type text, otherwise it wouldin't retain the leading zeros. Then, the following should do it:
SELECT regexp_replace(Number,'^(0*)([0-9]*)','\2\1') AS output;
If want the output to be an integer, you can simply typecast it:
SELECT regexp_replace(Number,'^(0*)([0-9]*)','\2\1')::integer AS output;
Do this is python.
eg:
a = "0012345"
str(int(a)) + (len(a) - len(str(int(a))))*"0"
Here is another option:
SELECT CAST(Number AS int) * 10 ^ (LENGTH(Number) - LENGTH(REPLACE(Number, '0', '')))
FROM yourTabl
Explanation:
The CAST would remove any leading zeroes leaving behind an integer, e.g. 12345. This number is then multiplied by ten raised to the number of zeroes which padded the original text the left.

Mask address in Crystal Reports

I am creating a report by Crystal Reports which will mask address field depending upon parameter set to the report. I know this can be done using formula. I can successfully mask with hard coded value. However I need something like password masking. For example, if address is D/302 ABC apartment, it should be displayed as X/XXX XXX XXXXXXXXX. Only characters and numbers to be masked space and special characters not be masked. In addition length of masked data should match actual data.
I think you can use a formula like this:
Local StringVar str := "";
Local NumberVar strLen := Length({User.Address});
Local NumberVar i;
For i := 1 To strLen Do
(
if (ChrW({User.Address}[i]) in (AscW("A") to AscW("Z"))) or (ChrW({User.Address}[i]) in (AscW("a") to AscW("z"))) or (ChrW({User.Address}[i]) in (AscW("0") to AscW("9"))) Then
str := str + "X"
else
str := str + {User.Address}[i];
);
str
Logic is correct.. I have done the same thing by using Mid function instead of chrW (). No need to use asw () either. Simply use range operator.
Logic is correct.. I have done the same thing by using Mid function instead of chrW (). Additional you can use formula as ReplicateString ("X",len(address));
Only problem is space will be also masked

Crystal report "a number is required here"

I am writing some formulas in crystal reports. I have a field that may contain a numeric value, in which case I am doing some calulation on it, or it may contain a string value, in which case it should be returned verbatim. So I thought this should work: If Result.entry contains a number, then put that numeric value into Result, else just return Result.entry:
Local NumberVar Result := 0;
if NumericText({RESULT.ENTRY}) // may be N.D. or B.L.D.
then
( Result:=val({RESULT.ENTRY});)
else (
{RESULT.ENTRY} );
);
// something more going on here that at last returns some value
But no..."A number is required here" says CR and highlights the block following else... Any clue at all to what Crazy, sorry Crystal Reports wants here and why this is not acceptable? I've even tried to set a value and report that afterwards, (as per Crystal report if then help, how to return a string or variable with formula) but I still get the same error.
else ( 0 );
works - but that is definately not what I want. (the other return value from this function is also a string)
you are trying to assign if part as number and else part as String which won't work, you either return both numbers or both strings.
So you save the numeric value as string and while using in calculation convert to number
Local StringVar Result;
if NumericText({RESULT.ENTRY}) // may be N.D. or B.L.D.
then
Result:={RESULT.ENTRY};
else (
{RESULT.ENTRY} );
);
Now when using in calculation you can use ToNumber(Result)
Almost important to note here is that if declaring something as NumberVar then the field being added has to be a number. If dealing with decimals use CurrencyVar as you cannot set NumberVar to 0 and then add it to a decimal value, which will result in getting a number is expected here result.

Crystal Reports: append spaces to string

I have a field in my report that needs to have a length of 40. If the string is not of that length I need to append some space.
Thanks.
try something along the lines of
if length({yourfieldname here}) < 40
then
{yourfieldnamehere} + space(40 - length({yourfieldnamehere}))
else
{yourfieldnamehere}
Append 40 blanks to the variable and take the first 40 characters of the resulting string.
Something like
mid( (yourfield + space(40)), 1, 40)
should work.