What you could do to make this eaeier and not need IIF or ICASE is prefix "10" and take the right 4 characters:
[pre]"25" -> "1025" -> Right(x,4): "1025"
"125" -> "10125" -> Right(x,4): "0125"
"5150" -> "105150" -> Right(x,4): "5150"[/pre]
If that works for you the expression is [tt]Right(Alltrim(employee),4) as employee[/tt].
And by the way, I wouldn't ever trust assertions about data like "no employee has a single digit number". Even when there is such a field rule it could have been added after such records already exist.
You have SQL to find out: [tt]SELECT Distinct Len(alltrim(employee)) as length, count(*) as frequency from empname[/tt]. You might even have NULL vaues.
Now why didnd't IF work: Your IIF does this:
[pre]"25" -> Len("25")=2 [highlight #FCE94F]is <3[/highlight], so prefix "10": "1025"
"125" -> Len("25")=3 [highlight #FCE94F]is
not <3[/highlight], so prefix "0": "0125"
"5150" -> Len("5150")=4 [highlight #FCE94F]is
not <3[/highlight], so prefix "0": "05150"[/pre]
It's simple to see when you strictly act as the code you write. 0-2 digits get the "10" prefix, the case for 0-1 don't exist, so that case is only for 2 digit employees. Every other with 3 [highlight #FCE94F]or 4[/highlight] digits gets prefix "0", both 3 and 4 are not <3. You have no case for
no prefix. As you want 4 digit numbers to get no prefix, you have another case to distinguish.
And let me guess: The SQL returns "0515" in the last case instead of "05150". Look into the width of the result field:
Code:
? Len(junksalary.employee)
-> 4
It's 4, isn't it?
As your first result record likely comes from one of the first employees with a short employee number, that get's correctly prefixed and ends with 4 char long value. And that means the last digit of most any employee with 4-digit number get's cut off as the intermediate IIF result is 5 digits long. Your employee "5150" does neither stay "5150" nor gets "05150" but turns into "0515". Because you don't know you shouldn't use any expressions of varying lengths or you don't realize that it's that way.
Some recommendations:
The first step before programming anything: Browse data, do some value checks with things like a GROUP BY or DISTINCT query.
Second step: When there are cases with less than 10 records: Perhaps even cover these cases manually before you write code 10 records are corrected manually easier and faster. Plus that'll correct them permanently, not just your query result. And then, always cover the most frequent case first, because of step 2, what's left over might simply be manually covered again.
You could also do 2 updates, in the first prefix "0" for every number shorter than 4, afterward prefix "1" for those still not having length 4. If thinking about 3 instead of just 2 cases is already an abstraction too much - I have an understanding for this at the moment, the heat here in Germany is draining concentration and focus, I can, fortunately, go into a cooled server room. And ice helps, though I still needed 3 or 4 edits before this finally is as I like.
Bye, Olaf.
Olaf Doschke Software Engineering