Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2020 : Technical Questions
Message Icon Topic: Need to drop leading zeros Post Reply Post New Topic
Author Message
GulfSniper91
Newbie
Newbie
Avatar

Joined: 22 Jun 2009
Location: United States
Online Status: Offline
Posts: 3
Quote GulfSniper91 Replybullet Topic: Need to drop leading zeros
    Posted: 22 Jun 2009 at 1:59pm
I have a label that pulls a fixed 5 digit store number from my SQL database that uses leading zeros (i.e store #4 = 00004, store # 222 = 00222).   With a different customer, the label had been setup to add preceding zeros, now I want to remove all zeros so that I only get 3 digits.  Here's my thinking, but something isn't working.

local numberVar v_length := 0;
local stringVar v_store := "";

// get the length of the store number field
v_length := Length({customer.strnum});

// evaluate the store number length, if less than three, append zeroes at the beginning
if (v_length = 3) then
    v_store :={customer.strnum}
else if (v_length = 2) then
    v_store :="0" + {customer.strnum}
else if (v_length = 1) then
    v_store :="00" + {customer.strnum}
else
    v_store := "000";

v_store;
IP IP Logged
JohnT
Groupie
Groupie
Avatar

Joined: 20 Jan 2008
Online Status: Offline
Posts: 92
Quote JohnT Replybullet Posted: 22 Jun 2009 at 2:07pm
What is it that isn't working ?   Your description of the problems says you want to remove zeros to only get 3 characters but your code looks at the length of strnum and appends zeros to make 3 characters. 
 
Would you give us some more information ?
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 22 Jun 2009 at 2:09pm
Why not use
tonumber({customer.strnum}) to remove all zeros
 
or to limit it to 3 right characters
right({customer.strnum},3)


Edited by DBlank - 22 Jun 2009 at 2:11pm
IP IP Logged
GulfSniper91
Newbie
Newbie
Avatar

Joined: 22 Jun 2009
Location: United States
Online Status: Offline
Posts: 3
Quote GulfSniper91 Replybullet Posted: 22 Jun 2009 at 2:17pm
Man you guys are fast with the replies. 

I have to append in some cases like store number 4 which would be 00004 in my database.  I'm thinking that any function I use that comes before the appending of zeros to make exactly 3 digits would need to strip all zeros (like when you make a number an int instead of varchar).
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 22 Jun 2009 at 2:27pm
Either approach below assumes you have no records that numerically would be >999...
You could do: Right("00" + {customer.strnum},3)
 
Or I think your last statement is what  is messing it up..
local numberVar v_length := 0;
local stringVar v_store := "";

// get the length of the store number field
v_length := Length({customer.strnum});

// evaluate the store number length, if less than three, append zeroes at the beginning
if (v_length = 3) then
    v_store :={customer.strnum}
else if (v_length = 2) then
    v_store :="0" + {customer.strnum}
else if (v_length = 1) then
    v_store :="00" + {customer.strnum}
else
    v_store := right({customer.strnum},3);

v_store;
IP IP Logged
GulfSniper91
Newbie
Newbie
Avatar

Joined: 22 Jun 2009
Location: United States
Online Status: Offline
Posts: 3
Quote GulfSniper91 Replybullet Posted: 22 Jun 2009 at 2:53pm
Your recommendation of

right({customer.strnum},3)

is what I used to make this work.  Thanks for such a quick answer.
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.016 seconds.