?
Solved

If field is <9, then pad with 0, else use both digits in field

Posted on 2011-05-10
15
Medium Priority
?
314 Views
Last Modified: 2012-05-11
Here is my query:
select dd.o_itemname||'-'||dd.o_projectno||'-'||dd.o_version||'-'||'0'||dd.o_version_seq SKETCH
        from dms_proj dp, dms_doc dd
        where dd.o_projguid = dp.o_projguid
        and dp.o_stateno = 21
        and dd.o_itemname like 'SK%';

What I need to do is use this condition before qeury can work.
For row in question...
If dd.o_version_seq > 9 then
   select dd.o_itemname||'-'||dd.o_projectno||'-'||dd.o_version||'-'||dd.o_version_seq SKETCH
else
   select dd.o_itemname||'-'||dd.o_projectno||'-'||dd.o_version||'-'||'0'||dd.o_version_seq SKETCH
end if

How do I make this work so that each row will be tested for condition, and appropriate query used?  This IS NOT a stored proc, but a query used in Lotus Notes.

Thanks in advance for your help.  
0
Comment
Question by:Jon Davidson
  • 9
  • 5
15 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 35729819
select dd.o_itemname||'-'||dd.o_projectno||'-'||dd.o_version||'-'||
to_char(dd.o_version_seq,'fm09')
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 1000 total points
ID: 35729855
fyi,

to_char(dd.o_version_seq,'fm09')  


will work regardless if the usage is in SQL or a stored procedure
0
 

Author Comment

by:Jon Davidson
ID: 35730386
sdstuber:
What does fm09 do?   Because one way, I need to pad with a zero, and the other (>9) would be not to use the 0, but just the number from the field.  Either way, I'll have 2 numbers in the field at all times.
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 74

Expert Comment

by:sdstuber
ID: 35730549
that's exactly what fm09  does


fm- removes extra space reserved for a sign character

otherwise  to_char('9','09')  would return ' 09'  instead of '09'

09 - left pad with 0 to fill two digits

you could use   'fm00'  instead of 'fm09'  for the same functionality
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35730643
0
 

Author Comment

by:Jon Davidson
ID: 35730777
If I use fm00, I get '000' as output.  If I use fm99, I get '00', or '01'.  I'll have to test a for number greater than 9 since it only happens on rare occasions.  Thank you.  I need to test before I close and award this action.  
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35730813
>>>  If I use fm00, I get '000'

no you don't

at least not from the to_char itself.  your third zero must be from the concatenation of the other fields

'0' || to_char(0,'fm00')  = '000'  but it's not the fm00  that is creating three zero's


in fact,  fm09 (or fm00)  is not just a an insurance to have at least 2 digits,  it's also assurance that you'll have at most 2 digits

to_char(123,'fm09')  = '###"


why?  because you can't fit 123 in 2 digits
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35730858
Here's a simple test to confirm the conversions for every number 0-109

The   ><  is simply to show there is no extra whitespace or other invisible characters surrounding the converted string


SELECT n, '>' || TO_CHAR(n, 'fm00') || '<'
  FROM (SELECT     LEVEL - 1 n
              FROM DUAL
        CONNECT BY LEVEL <= 110)
0
 

Author Comment

by:Jon Davidson
ID: 35730929
Ah! I found an error in my sql, so the fm00 works as you said.   fm00 should handle numbers from 1~99?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35730975
as shown in the test case, it will handle 0 to 99 ,  so yes, 1-99 will work too

of course, best advice,  run it yourself, test it yourself.

don't just trust me
0
 
LVL 32

Expert Comment

by:awking00
ID: 35731089
See attached.
comment.txt
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35731176
awking00

why would you force BOTH an implicit conversion from number to string  as well as padding?

using the implicit conversion  that way forces the db to do this for you

lpad(to_char(dd.o_version_seq),2,'0')

I agree it will work, but if you're need to do the to_char anyway, then more efficient and easier to just do it one step
0
 

Author Comment

by:Jon Davidson
ID: 35731296
The complete string that this forms will always remain constant

SK0001-12345-00-00 or SK1234-12345-01-99

Note the last two digits in the field.  The value could fall between 0, and 99 (I might've said 1~99 in an earlier note, but is 0~99).  It is a numerical value.  It would be very rare to see the version sequence (last two numbers) value greater than 9, but I have to code for it nonetheless.
Adding the code lpad(),2,'0')  does it all without the implicit conversion?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35731352
>>Adding the code lpad(),2,'0')  does it all without the implicit conversion?

NO

The lpad version forces the same conversion I suggested above AND requires the extra step of a call to lpad too

just use the

to_char(dd.o_version_seq,'fm09')  

as shown originally.  

It's one function call doing it all in one step and,
if you run it, you'll see it does exactly what you asked

0
 

Author Closing Comment

by:Jon Davidson
ID: 35732200
Thanks for the explanations, as I iterated through the tesitng.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question