Question

divide one field into two!

Asked by: khushi2003

How can i divide one field into as shown in sample data.
I don;t think substr and instr function only can handle this problem. instr function only checks what ever string we ask to check and returns that. in my address case i don;t hv any comma and symbols, but in this case :
Cnr Warf Rd & Hollywood Rd                                       Warf Rd & Hollywood Rd

i hv to delete delete Cnr from original data.
Is there any function any Oracle that splits into two?


Original Data                                                 Conversion
FULLADDRESS                                       ROADNUMBER      ROADNAME
1 Ada Street                                          1                      Ada Street
1 A Kalinda Street                                  1A                     Kalinda Street
1/13 Burrawang Street                           1/13                  Burrawang Street
1/21-23 Chelmsford Road                        1/21-23            Chelmsford Road
10/1a Frederick Street                           10/1a                 Frederick Street
161? Lang Street                                    161                   Lang Street
368 Cnr Barker & Anzac Pde                                            Barker & Anzac Pde
5/70-74/ Wilson Street                           5/70-74              Wilson Street
Shop 2 4-14 Claudia Road                      Shop 2 4-14        Claudia Road
PO Box 558                                                                     PO Box 558
Lot 4 Mungerie Road                               Lot 4                  Mungerie Road
Level 4/65 Albert Avenue                         Level 4/65          Albert Avenue
Cnr Warf Rd & Hollywood Rd                                            Warf Rd & Hollywood Rd
Unit 85 51 Little Willandra Rd                 House 85 51         Little Willandra Rd

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-03-30 at 03:36:30ID20937138
Tags

oracle

,

divide

,

field

,

one

Topic

Oracle Database

Participating Experts
2
Points
420
Comments
75

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Shopping cart woes
    I have a Web site that uses a script called "S-Mart" shopping cart. Recently, about two months ago I moved to a new provider and enhanced the script to handle SSL when the user goes to check out.... now I have a problem. About 1 in 10 times (1 in 10 orders) I wil...
  2. Street Abbreviation Revisited
    I will like to revise the code found at this like: http://www.experts-exchange.com/Databases/MS_Access/Q_21298450.html I adopted the suggestion provided by "nico5038" on 02/02/05 The work has worked for some time, but I have fine tuned the codes in my DB several t...
  3. Multiple transactions POS software
    Hey Experts I am making POS software for a company. The company have asked for the software to be able to handle multiple transactions The problem is i dont know how to Implement it in visual basic i was thinking about multiple arrays I am using a Microsoft Access Databa...
  4. Format text box with PO BOX
    Sorry, I'm a newbie and not a programmer, just a great hacker. I am working on an address field in access 2003. The issue is this field is mixxed with street addresses and post office boxes. We want to standardize formatting of "PO BOX" in this field if possible. It...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: khushi2003Posted on 2004-03-30 at 03:40:24ID: 10712622

In my most of the address i hv numeric value at front and character followed by it. What function can divide numeric and string into two so that i can insert my address data from original table to new table without working as manually.

 

by: mayankeaglePosted on 2004-03-30 at 05:27:34ID: 10713216

Well, you can try some coding with the substring () function.

 

by: pratikroyPosted on 2004-03-30 at 06:20:59ID: 10713648

The data that you have provided has a lot of problems, which you want to be fixed in the output. So, I dont think, its as simple as just dividing the column into two.

To start with, you can use the following SQL to get the result, which is similar to result that you are looking for. But it is not exactly same. I will need answers to a few more questions to give you a precise answer. :)

Try this ...

SQL> select fld1 from tab_ex;

FLD1
--------------------------------------------------------------------------------
1 Ada Street
1 A Kalinda Street
1/13 Burrawang Street
1/21-23 Chelmsford Road
10/1a Frederick Street
161? Lang Street
368 Cnr Barker & Anzac Pde
5/70-74/ Wilson Street
Shop 2 4-14 Claudia Road
PO Box 558
Lot 4 Mungerie Road
Level 4/65 Albert Avenue
Cnr Warf Rd & Hollywood Rd
Unit 85 51 Little Willandra Rd

14 rows selected.

SQL> select substr(fld1, 1, instr(fld1, ' ')) roadnum, substr(fld1, instr(fld1, ' ')) roadname from tab_ex;

ROADNUM                      ROADNAME
---------------------------  ---------------------------
1                            Ada Street
1                            A Kalinda Street
1/13                         Burrawang Street
1/21-23                      Chelmsford Road
10/1a                        Frederick Street
161?                         Lang Street
368                          Cnr Barker & Anzac Pde
5/70-74/                     Wilson Street
Shop                         2 4-14 Claudia Road
PO                           Box 558
Lot                          4 Mungerie Road
Level                        4/65 Albert Avenue
Cnr                          Warf Rd & Hollywood Rd
Unit                         85 51 Little Willandra Rd

14 rows selected.

Now, note the following :
1. 2nd address, "1 A Kalinda Street" has become "1","A Kalinda Street"
2. 6th address, "161? Lang Street" has become "161?","Lang Street"
3. 7th address, "368 Cnr Barker & Anzac Pde" has become "368","Cnr Barker & Anzac Pde"
4. 8th address, "5/70-74/ Wilson Street" has become "5/70-74/","Wilson Street"
5. 9th address, "Shop 2 4-14 Claudia Road" has become "Shop", "2 4-14 Claudia Road"
6. 10th address, "PO Box 558" has become "PO", "Box 558"
7. 11th address, "Lot 4 Mungerie Road" has become "Lot", "4 Mungerie Road"
8. 12th address, "Level 4/65 Albert Avenue" has become "Level", "4/65 Albert Avenue"
9. 13th address, "Cnr Warf Rd & Hollywood Rd" has become "Cnr", "Warf Rd & Hollywood Rd"
10. 14th address, "Unit 85 51 Little Willandra Rd" has become "Unit", "85 51 Little Willandra Rd"

In all these cases you wanted some different values like :
1. "1A","Kalinda Street" instead of "1","A Kalinda Street"
2. "161","Lang Street" instead of "161?","Lang Street"
3. "","Barker & Anzac Pde" instead of "368","Cnr Barker & Anzac Pde"
4. "5/70-74","Wilson Street" instead of "5/70-74/","Wilson Street"
5. "Shop 2 4-14", "Claudia Road" instead of "Shop", "2 4-14 Claudia Road"
6. "", "PO Box 558" instead of "PO", "Box 558"
7. "Lot 4", "Mungerie Road" instead of "Lot", "4 Mungerie Road"
8. "Level 4/65", "Albert Avenue" instead of "Level", "4/65 Albert Avenue"
9. "", "Warf Rd & Hollywood Rd" instead of "Cnr", "Warf Rd & Hollywood Rd"
10. "House House 85 51", "Little Willandra Rd" instead of "Unit", "85 51 Little Willandra Rd"

Almost all these cases are different, and have to be dealt separately. Do you have a set of rules, by which you can actually divide the road number and road name. You will not be able to have the desired results, unless these rules are clearly stated.

some questions like :
1. When do you concatenate the number and character (string) to formulate the road number.
2. Which characters do you need to strip. (like '/' and '?'), and when and where from do you strip it.
3. What is the rule of Cnr ? If there is a Cnr in the text, do you strip the Cnr and any number before it ?
4. How do you arrive at the shop number ? In case of "Shop 2 4-14 Claudia Road" ? In which cases do you keep the characters before the actual number (intact with the number) to arrive at road number ? for example - Shop/Level etc.
5. Is there a separate rule for Post Boxes (PO Box) ?
6. In which cases do you replace the character strings ? In this case - from Unit to House.

If you answer all these questions, you might find the answer in your questions. :)

Do let me know in case you need any more help on this.

 

by: khushi2003Posted on 2004-03-31 at 03:09:29ID: 10722683

Thanx Mr. Roy

I have clear view regarding my problem on this topic u can check this:
http://www.experts-exchange.com/Databases/Oracle/Q_20935009.html

but still having problem to put datas on 2nd table and exception table, what i am trying to do those unformatted address i want to throw into exception table and amend the it manually after looking at exception table.  Will u please look that link to get more idea
and ur question really helped to think what should i do with this unforammted adress, and now only one option i have is to throw into exception table.
For eg:
i hv employee table
emp_no
emp_adress
emp_tel_no

and table employee_2 table (Data inserted from employee table)
emp_no
emp_road_no
emp_road_name
emp_tel_no

and employee_exception (unformatted data thrown here)
emp_no
emp_adress
emp_tel_no
comments (to know what problem occur while splitting)

Ur question:
some questions like :
1. When do you concatenate the number and character (string) to formulate the road number.
>>number with road number into employee_2 and only with road name or street name into exception with comment exception
2. Which characters do you need to strip. (like '/' and '?'), and when and where from do you strip it.
>>"/" is fine but i am looking to unwanted character like "?" to change with null.
I may use INSTR function can ? INSTR(empaddress,'?') and can i use replace funtion to replcae sucj unwanted character with null;

3. What is the rule of Cnr ? If there is a Cnr in the text, do you strip the Cnr and any number before it ?
Cnr=corner reprents corner so i don;t need Cnr and its number
4. How do you arrive at the shop number ? In case of "Shop 2 4-14 Claudia Road" ? In which cases do you keep the characters before the actual number (intact with the number) to arrive at road number ? for example - Shop/Level etc.
I just like to put such doubtful data into exception table and rest other will do manually
5. Is there a separate rule for Post Boxes (PO Box) ?
>>PO box should be in emp_road_name col or can be put into exception table
6. In which cases do you replace the character strings ? In this case - from Unit to House.
>>unit and house number should be on road_number col and road_name in emp_road_name col

Regards!

 

by: pratikroyPosted on 2004-03-31 at 16:05:29ID: 10728418

Hello Khushi,

I have looked at the link that you have provided. It looks like that you have asked same/similar questions in both :) But you have accepted Raj's answer there, but you still are looking for answers :)

Now in the Q_20935009, everyone involved in the discussion had similar questions, that I had put up. You will have to define the rules clearly for identifying the House/Road No. and House/Road Name.

Since the data is coming from free form Excel, you really would have to find out the most common format of address that is present in the Excel sheet. Define the rule for that common format (Whether space/comma/colon or any other character separated fields, or fixed width fields etc). Any address that does not match the common format, can be thrown into the exception table (as you want). Remember, that you can define more than one rule for this as well. These rules can then be enforced in the procedure that you have written in your other question.

Did you notice the values that have come out from the first SQL that I had provided. Which of them are acceptable to you, and which are'nt. We will try and define some rules below.

My SQL had returned :

1. "1","A Kalinda Street" whereas you wanted "1A","Kalinda Street"
>>On which basis would you know if the Street name is "Kalinda Street", and not "A Kalinda Street". "A Kalinda Street" looks like a very valid name to me. Street name can be more than 2 words, like "Barker & Anzac Pde".

2. "161?","Lang Street" whereas you wanted "161","Lang Street"
>>Getting rid of "?" is not a problem, and can be handled easily. See the usage of REPLACE function that I have described below. But I need to know if I can safely take the first string as the house/road number, and rest of it as the house/road name.

3. "368","Cnr Barker & Anzac Pde" whereas you wanted "","Barker & Anzac Pde"
>>Can we safely say that wherever a string "Cnr" occurs in the address, ignore everything before "Cnr" and put the rest in the house/road name ? I would like to have this as a rule.

4. "5/70-74/","Wilson Street" whereas you wanted "5/70-74","Wilson Street"
>>Same comment as for #2. We can get rid of last character from the road number if it is either '/' or '?'. We can take this as a rule.

5. "Shop 2 4-14", "Claudia Road" instead of "Shop", "2 4-14 Claudia Road"
>>By looking at this any intelligent person can guess that "Shop 2 4-14" is the number, and "Claudia Road" is the street name. Unfortunately computer would not be intelligent enough to make this decision. We will have to tell it how to deal with a string that has "Shop" in it. Just like we did for "Cnr" (corner). A Rule can be, if there is a string "Shop" in it, extract the road number as - 1st character of the string to the character which is the last number (in this case character no. 11, i.e. 4) in the string. So, it will extract "Shop 2 4-14" as road number, and return the rest - "Claudia Road" as the name.

6. "PO", "Box 558" whereas you wanted "", "PO Box 558"
>>Similar to comment for #3. We can have a rule - wherever there is a string "PO Box" in the address, put it all in the road name.
 
7. "Lot", "4 Mungerie Road" whereas you wanted "Lot 4", "Mungerie Road"
>>Similar to comment for #5. We can have a rule - wherever there is a string "Lot" in the address, extract the road number as - 1st character of the string to the character which is the last number (in this case character no. 5, i.e. 4) in the string. So, it will extract "Lot 4" as road number, and return the rest - "Mungerie Road" as the name.

8. "Level", "4/65 Albert Avenue" whereas you wanted "Level 4/65", "Albert Avenue"
>>Similar to comment #5 and #7. Deal with the case where you have "Level" in the address.

9. "", "Warf Rd & Hollywood Rd" instead of "Cnr", "Warf Rd & Hollywood Rd"
>>same as comment #3

10. "House 85 51", "Little Willandra Rd" instead of "Unit", "85 51 Little Willandra Rd"
>>May be you need to things to be done, if you find a string "Unit" in the address. First replace "Unit" with "House". Then extract the House Number and Name using the same method as in #5,#7 and #8

If you find an address which does not belong to any of the above handled cases, send the record into the exception table.

Now you would realise the importance of the 6 questions that I had asked. It was because you will have to take care of 6 different cases (atleast), based on 6 different rules.

Coming on to the question that you asked regarding the INSTR function. "NO" INSTR function will not replace the value as you are expecting. "INSTR" function is used to find out the occurance of a string in another string. It returns the position where it finds the matching string.

The following SQL might give you a better idea of INSTR function, and its usage :
SQL> select instr('PRATIK ROY','A') from dual;

INSTR('PRATIKROY','A')
----------------------
                     3

SQL> select instr('PRATIK ROY','R') from dual;

INSTR('PRATIKROY','R')
----------------------
                     2

SQL> select instr('PRATIK ROY','TIK') from dual;

INSTR('PRATIKROY','TIK')
------------------------
                       4

But you can achieve it using the REPLACE function.

Try this :
SQL> select replace('abcdef','c','x') from dual;

REPLAC
------
abxdef

SQL> select replace ('161?','?','') from dual;

REP
---
161

Does this help now ?

 

by: pratikroyPosted on 2004-03-31 at 16:09:13ID: 10728440

Just for your info, I would be very-very busy with my work for the next 2 days, so would be able to see your answer, and provide further help on this only on Saturday. I Hope it would be OK.

 

by: khushi2003Posted on 2004-03-31 at 18:19:02ID: 10729089

Actually on last question i was waiting for solution but no one come up after that i had to close the question.
So i hv accepted answer and close that question!
Now i hv problem to write procedures atleast i hv something to throw in exception table, still working !
regards

 

by: pratikroyPosted on 2004-04-01 at 03:57:35ID: 10731302

OK. I could take out some time, and have created an anonymous PL/SQL block for you, which wud do exactly what you want.

I had created a table tab_ex with just one field fld1. In your case it is emp_adress of employee table.

I created another table tab_ex1 with two fields ROADNUM and ROADNAME. Just like you have emp_road_no and emp_road_name in employee_2 table. You can now use it as per your convenience. You might want to use the UPPER or LOWER function, so that it would work even if you get "SHOP" instead of "Shop" or "CnR" instead of "Cnr" etc. You will notice that I have hardcoded all the cases that I had previously discussed.

Here is the code ...

declare
    v_roadno   varchar2(50);
    v_roadnm   varchar2(200);
    v_lastnum  varchar(100);
    v_tempnm   varchar2(200);

    cursor c1 is
    select fld1 from tab_ex;

begin
    for c1_rec in c1
    loop
        -- Check the format of the address
        -- Case 1. "Cnr"
        --         ignore everything before "Cnr" and put the rest in the house/road name
        IF INSTR(c1_rec.fld1, 'Cnr') > 0 THEN
            v_roadno := '';
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1, 'Cnr')+ 4);
            goto loop_next;
        END IF;

        -- Case 2. "Shop"
        -- if there is a string "Shop" in it, extract the road number as - 1st character
        -- of the string to the character which is the last number
        IF INSTR(c1_rec.fld1, 'Shop') > 0 THEN
            -- Find the last number in the string
            v_lastnum := substr(trim(translate(upper(c1_rec.fld1),
                         '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);
            v_roadno  := substr(c1_rec.fld1,1,INSTR(c1_rec.fld1,v_lastnum,-1));
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1,v_lastnum,-1)+1);
            goto loop_next;
        END IF;

        -- Case 3. "PO Box"
        --         Put everything in road name
        IF INSTR(c1_rec.fld1, 'PO Box') > 0 THEN
            v_roadno := '';
            v_roadnm := c1_rec.fld1;
            goto loop_next;
        END IF;

        -- Case 4. "Lot"
        -- if there is a string "Lot" in it, extract the road number as - 1st character
        -- of the string to the character which is the last number
        IF INSTR(c1_rec.fld1, 'Lot') > 0 THEN
            -- Find the last number in the string
            v_lastnum := substr(trim(translate(upper(c1_rec.fld1),
                         '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);

            v_roadno  := substr(c1_rec.fld1,1,INSTR(c1_rec.fld1,v_lastnum,-1));
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1,v_lastnum,-1)+1);
            goto loop_next;
        END IF;

        -- Case 5. "Level"
        -- if there is a string "Level" in it, extract the road number as - 1st character
        -- of the string to the character which is the last number
        IF INSTR(c1_rec.fld1, 'Level') > 0 THEN
            -- Find the last number in the string
            v_lastnum := substr(trim(translate(upper(c1_rec.fld1),
                         '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);

            v_roadno  := substr(c1_rec.fld1,1,INSTR(c1_rec.fld1,v_lastnum,-1));
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1,v_lastnum,-1)+1);
            goto loop_next;
        END IF;

        -- Case 6. "Unit"
        -- First replace "Unit" with "House". Then extract the House Number using the same method as above
        IF INSTR(c1_rec.fld1, 'Unit') > 0 THEN
            -- Find the last number in the string
            v_tempnm := REPLACE(c1_rec.fld1,'Unit','House');
            v_lastnum := substr(trim(translate(upper(v_tempnm),
                         '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);

            v_roadno  := substr(v_tempnm,1,INSTR(v_tempnm,v_lastnum,-1));
            v_roadnm := substr(v_tempnm, INSTR(v_tempnm,v_lastnum,-1)+1);
            goto loop_next;
        END IF;

        -- If none of the above cases, then its the normal address.
        v_roadno := substr(c1_rec.fld1, 1, instr(c1_rec.fld1, ' '));
        v_roadnm := substr(c1_rec.fld1, instr(c1_rec.fld1, ' '));

        <<loop_next>>
        v_roadno := trim(v_roadno);
        v_roadnm := trim(v_roadnm);
        -- last thing, check if the last character of road number is '/' or '?'. Strip if this is the case
        IF substr(v_roadno,-1) = '/' OR substr(v_roadno,-1) = '?' THEN
            v_roadno := substr(v_roadno,1,length(v_roadno)-1);
        END IF;
        dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm);
        INSERT INTO tab_ex1 VALUES(v_roadno, v_roadnm);
    end loop;
end;
/

This should help.

 

by: pratikroyPosted on 2004-04-01 at 04:02:08ID: 10731317

The only problem would be for address like "1 A Kalinda Street" where you will get "1","A Kalinda Street" whereas you wanted "1A","Kalinda Street".
I Repeat myself ...
>>On which basis would you know if the Street name is "Kalinda Street", and not "A Kalinda Street". "A Kalinda Street" looks like a very valid name to me. Street name can be more than 2 words, like "Barker & Anzac Pde".

All other cases have been taken care of. This is the output from tab_ex1 table after executing the above code ...

SQL> Select roadnum, roadname from tab_ex1;

ROADNUM              ROADNAME
-------------------- --------------------------------------------------
1                    Ada Street
1                    A Kalinda Street
1/13                 Burrawang Street
1/21-23              Chelmsford Road
10/1a                Frederick Street
161                  Lang Street
                     Barker & Anzac Pde
5/70-74              Wilson Street
Shop 2 4-14          Claudia Road
                     PO Box 558
Lot 4                Mungerie Road
Level 4/65           Albert Avenue
                     Warf Rd & Hollywood Rd
House 85 51          Little Willandra Rd

14 rows selected.

Give me higher points if possible ... ;) just kidding ...

 

by: khushi2003Posted on 2004-04-01 at 09:30:45ID: 10733862

Thanx for the answer...but still i hvn;t run ur code.
Does this code really handles if i hv huge data records.

Original Data                                                 Conversion
FULLADDRESS                                       ROADNUMBER      ROADNAME
1 Ada Street                                          1                      Ada Street
1 A Kalinda Street                                  1A                     Kalinda Street
1/13 Burrawang Street                           1/13                  Burrawang Street
1/21-23 Chelmsford Road                        1/21-23            Chelmsford Road
10/1a Frederick Street                           10/1a                 Frederick Street
161? Lang Street                                    161                   Lang Street
368 Cnr Barker & Anzac Pde                                            Barker & Anzac Pde
5/70-74/ Wilson Street                           5/70-74              Wilson Street
Shop 2 4-14 Claudia Road                      Shop 2 4-14        Claudia Road
PO Box 558                                                                     PO Box 558
Lot 4 Mungerie Road                               Lot 4                  Mungerie Road
Level 4/65 Albert Avenue                         Level 4/65          Albert Avenue
Cnr Warf Rd & Hollywood Rd                                            Warf Rd & Hollywood Rd
Unit 85 51 Little Willandra Rd                 House 85 51         Little Willandra Rd
Suite 2 Ground Floor                                                        EXCEPTION
Street                                                                             EXCEPTION

i hvn;t yet run ur code, but how to deal with exception like address with only "street", suite 2 ground floor, i want to throw it into exception table and all data same as original table with comments on comment row.

tbl sample

customer

custnum
custaddress
phone


customer1

custnum
cust_road_num
cust_road_name
phone

customer_exception
custnum
custaddress
phone
comment

sorry its my mistake house and unit is same so i don;t need replace function to replce unit by house.
can i insert by doing this into exception table just example: (field name may not be same just concept please look at it)

procedure test123    as  
  begin  
  for x in (select * from customer)  
 loop  
  insert into customer1(id,name,street_no,street_name,phone)  
 values(x.id,x.name,substr(x.address,1,instr(x.address,' ',1)-1),substr(x.address,instr(x.address,' ',1)+1),x.phone);  
  end loop;  
 commit;  

...can i add exception or else statement to throw data into exception table

coz i like to do by using procedure or function...

and sure i will raise the points coz it getting huge and huge!
50 to 110 heheh

 

by: pratikroyPosted on 2004-04-02 at 08:30:31ID: 10741976

What is your definition of huge data ? How many rows are you expecting ? I dont think this procedure will have problem.

As for the exception handling,
1. How can I stop "Ground Floor" from being recognised as a street name ? Can you define a rule saying that the road name can be treated as valid, only if it has one of the following :
Rd, Road, Avenue, Street, Pde or PO BOX
2. As for the "street", you might want to add a rule saying that the road name should contain 2 or more words, like 'abc street' or 'xyz & abc Pde' etc.

You can add these two checks at the end, just before inserting the record in the main table. In case the above two checks fail, then insert the record in the exception table, else insert into the main table.

Hope this helps.

 

by: pratikroyPosted on 2004-04-02 at 09:28:32ID: 10742529

You should execute my script, to see how it is working. Things might become more clearer, and you may be able to solve the new issues yourself. The anonymous PL/SQL can be coverted into a procedure (as you like creating procedures :)). Let me know if you need help in doing that.

In the meanwhile I have done the exception handling in my code as well. To see my codes working, I believe you should create the tables, as I have created.
TAB_EX : fld1 varchar2(250)
TAB_EX1 : roadnum varchar2(50), roadname varchar2(200)
TAB_EX_ERR : fld1 varchar2(250), errmsg varchar2(200)

Insert the addresses in TAB_EX table, and then run the following code (Note the changes after <<loop_next>>:

declare
    v_roadno   varchar2(50);
    v_roadnm   varchar2(200);
    v_lastnum  varchar(100);
    v_tempnm   varchar2(200);

    cursor c1 is
    select fld1 from tab_ex;

--10* create table tab_ex1 (roadnum varchar2(50), roadname varchar2(200))
--SQL> create table tab_ex_err (fld1 varchar2(1000), errmsg varchar2(200));
begin
    for c1_rec in c1
    loop
        -- Check the format of the address
        -- Case 1. "Cnr"
        --         ignore everything before "Cnr" and put the rest in the house/road name
        IF INSTR(c1_rec.fld1, 'Cnr') > 0 THEN
            v_roadno := '';
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1, 'Cnr')+ 4);
            goto loop_next;
        END IF;

        -- Case 2. "Shop"
        -- if there is a string "Shop" in it, extract the road number as - 1st character
        -- of the string to the character which is the last number
        IF INSTR(c1_rec.fld1, 'Shop') > 0 THEN
            -- Find the last number in the string
            v_lastnum := substr(trim(translate(upper(c1_rec.fld1),
                         '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);
            v_roadno  := substr(c1_rec.fld1,1,INSTR(c1_rec.fld1,v_lastnum,-1));
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1,v_lastnum,-1)+1);
            goto loop_next;
        END IF;

        -- Case 3. "PO Box"
        --         Put everything in road name
        IF INSTR(c1_rec.fld1, 'PO Box') > 0 THEN
            v_roadno := '';
            v_roadnm := c1_rec.fld1;
            goto loop_next;
        END IF;

        -- Case 4. "Lot"
        -- if there is a string "Lot" in it, extract the road number as - 1st character
        -- of the string to the character which is the last number
        IF INSTR(c1_rec.fld1, 'Lot') > 0 THEN
            -- Find the last number in the string
            v_lastnum := substr(trim(translate(upper(c1_rec.fld1),
                         '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);

            v_roadno  := substr(c1_rec.fld1,1,INSTR(c1_rec.fld1,v_lastnum,-1));
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1,v_lastnum,-1)+1);
            goto loop_next;
        END IF;

        -- Case 5. "Level"
        -- if there is a string "Level" in it, extract the road number as - 1st character
        -- of the string to the character which is the last number
        IF INSTR(c1_rec.fld1, 'Level') > 0 THEN
            -- Find the last number in the string
            v_lastnum := substr(trim(translate(upper(c1_rec.fld1),
                         '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);

            v_roadno  := substr(c1_rec.fld1,1,INSTR(c1_rec.fld1,v_lastnum,-1));
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1,v_lastnum,-1)+1);
            goto loop_next;
        END IF;

        -- Case 6. "Unit"
        -- First replace "Unit" with "House". Then extract the House Number using the same method as above
        IF INSTR(c1_rec.fld1, 'Unit') > 0 THEN
            -- Find the last number in the string
            v_tempnm := REPLACE(c1_rec.fld1,'Unit','House');
            v_lastnum := substr(trim(translate(upper(v_tempnm),
                         '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);

            v_roadno  := substr(v_tempnm,1,INSTR(v_tempnm,v_lastnum,-1));
            v_roadnm := substr(v_tempnm, INSTR(v_tempnm,v_lastnum,-1)+1);
            goto loop_next;
        END IF;

        -- If none of the above cases, then its the normal address.
        v_roadno := substr(c1_rec.fld1, 1, instr(c1_rec.fld1, ' '));
        v_roadnm := substr(c1_rec.fld1, instr(c1_rec.fld1, ' '));

        <<loop_next>>
        v_roadno := trim(v_roadno);
        v_roadnm := trim(v_roadnm);
        -- last thing, check if the last character of road number is '/' or '?'. Strip if this is the case
        IF substr(v_roadno,-1) = '/' OR substr(v_roadno,-1) = '?' THEN
            v_roadno := substr(v_roadno,1,length(v_roadno)-1);
        END IF;
        IF instr(v_roadnm,' ') > 0 THEN
            -- If road name has more than one word then check if it has
            -- one of the valid values - Rd, Road, Avenue, Street, Pde or PO BOX
            IF instr(v_roadnm, 'Rd') > 0
            OR instr(v_roadnm, 'Road') > 0
            OR instr(v_roadnm, 'Avenue') > 0
            OR instr(v_roadnm, 'Ave') > 0
            OR instr(v_roadnm, 'Street') > 0
            OR instr(v_roadnm, 'Pde') > 0
            OR instr(v_roadnm, 'PO Box') > 0
            THEN
                dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm);
                INSERT INTO tab_ex1 VALUES(v_roadno, v_roadnm);
            ELSE
                dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm || '****Problem');
                INSERT INTO tab_ex_err VALUES(c1_rec.fld1,'Invalid Road Name');
            END IF;
        ELSE
            dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm || '****Problem');
            INSERT INTO tab_ex_err VALUES(c1_rec.fld1,'Invalid address format');
        END IF;

    end loop;
end;
/



 

by: khushi2003Posted on 2004-04-02 at 21:12:51ID: 10746415

I will try pratik, first i hv to familiar with ur code and if there is any problem i will let u know. Thanx pratik

 

by: khushi2003Posted on 2004-04-02 at 21:14:14ID: 10746420

Have u tested ur code?

 

by: khushi2003Posted on 2004-04-02 at 21:17:01ID: 10746427

isn't it better to modularise the code and call from one or two procedure? please advice me

 

by: khushi2003Posted on 2004-04-03 at 07:02:42ID: 10747775

1. "1","A Kalinda Street" whereas you wanted "1A","Kalinda Street"
>>On which basis would you know if the Street name is "Kalinda Street", and not "A Kalinda Street". "A Kalinda Street" looks like a very valid name to me. Street name can be more than 2 words, like "Barker & Anzac Pde".

2. "161?","Lang Street" whereas you wanted "161","Lang Street"
>>Getting rid of "?" is not a problem, and can be handled easily. See the usage of REPLACE function that I have described below. But I need to know if I can safely take the first string as the house/road number, and rest of it as the house/road name.

3. "368","Cnr Barker & Anzac Pde" whereas you wanted "","Barker & Anzac Pde"
>>Can we safely say that wherever a string "Cnr" occurs in the address, ignore everything before "Cnr" and put the rest in the house/road name ? I would like to have this as a rule.

4. "5/70-74/","Wilson Street" whereas you wanted "5/70-74","Wilson Street"
>>Same comment as for #2. We can get rid of last character from the road number if it is either '/' or '?'. We can take this as a rule.

5. "Shop 2 4-14", "Claudia Road" instead of "Shop", "2 4-14 Claudia Road"
>>By looking at this any intelligent person can guess that "Shop 2 4-14" is the number, and "Claudia Road" is the street name. Unfortunately computer would not be intelligent enough to make this decision. We will have to tell it how to deal with a string that has "Shop" in it. Just like we did for "Cnr" (corner). A Rule can be, if there is a string "Shop" in it, extract the road number as - 1st character of the string to the character which is the last number (in this case character no. 11, i.e. 4) in the string. So, it will extract "Shop 2 4-14" as road number, and return the rest - "Claudia Road" as the name.

6. "PO", "Box 558" whereas you wanted "", "PO Box 558"
>>Similar to comment for #3. We can have a rule - wherever there is a string "PO Box" in the address, put it all in the road name.
 
7. "Lot", "4 Mungerie Road" whereas you wanted "Lot 4", "Mungerie Road"
>>Similar to comment for #5. We can have a rule - wherever there is a string "Lot" in the address, extract the road number as - 1st character of the string to the character which is the last number (in this case character no. 5, i.e. 4) in the string. So, it will extract "Lot 4" as road number, and return the rest - "Mungerie Road" as the name.

8. "Level", "4/65 Albert Avenue" whereas you wanted "Level 4/65", "Albert Avenue"
>>Similar to comment #5 and #7. Deal with the case where you have "Level" in the address.

9. "", "Warf Rd & Hollywood Rd" instead of "Cnr", "Warf Rd & Hollywood Rd"
>>same as comment #3

10. "House 85 51", "Little Willandra Rd" instead of "Unit", "85 51 Little Willandra Rd"
>>May be you need to things to be done, if you find a string "Unit" in the address. First replace "Unit" with "House". Then extract the House Number and Name using the same method as in #5,#7 and #8

Yha all the rules specified are correct, only i hv made mistake is #10, correct data is:
Unit 85 51 Little willandra rd, conversion should be like "Unit 85 51", "Little Willandra rd".

I am trying ur code but geting confuse with whole code, so trying to break it and call by procedure can i do like that?

 

by: khushi2003Posted on 2004-04-03 at 07:14:07ID: 10747802

I understand pratik you are busy, take ur time, have fun!

 

by: pratikroyPosted on 2004-04-03 at 15:18:10ID: 10749731

Khushi, I have tested my code, and its working as per your expectations. What is the confusion in my code ? I had put comments at appropriate places to make the code more understandable.

I had put the solution for point number 10 a well, where I am replace Unit by House. You can remove that replace if you dont want it now.

You can definitely break this code in smaller functions/procedures if you are comfortable with that approach. I felt that since we are just doing one activity (i.e., breaking addresses into road name/road number), and handling various cases, we should do it in one procedure(PL/SQL Block) only.

Have you created the tables that I had asked you to ? Once the tables are created, you can insert the test addresses in the tab_ex, and just run the script that I have given. You can test it to your satisfaction.

Take your time and understand the code. I dont think you can categorise this code as a complicated one. Feel free to ask any more questions.

 

by: khushi2003Posted on 2004-04-04 at 06:28:34ID: 10751806

Thanx Pratik for ur support through out my task! muchly appreiciated

 

by: khushi2003Posted on 2004-04-04 at 07:38:33ID: 10752057

I am now creating all the tables u hv told me to, and running codes, i will let u know how i will go!

 

by: khushi2003Posted on 2004-04-04 at 08:07:19ID: 10752149

Yha i hv created tables and tested it works perfectly fine with out error. WOW!
But this whole code is too big actually i want to modularise it (into small block and call from one main procedure)
I may need to ask more question so i am raising points, coz i dont; want to start another question i would like to continue here!
In this case if i hv something to match from another table can i do on this block!

like if i add one filed in tab_ex table which is name of place (district), that identifies where that person pin code belongs. like i hv pin code table : for eg:
pin_code:
pincode_no number,
district
state

now my tab_ex table looks like:
fld1 varchar2(50),
district varchar2(250),
..........................no pincode field in this table
......

and
tab_ex1
roadnum varchar2(50),
roadname varchar2(200),
district
pincode_no...............................pincode field on this table (those rows that can be successfully return on this table shoudl have pincode value, retrieve from pincode table after matching with district name )

i know how SQL works on this case:
for eg:
select roadnum,roadname,tab_ex.district,pincode.pincode_no
from tab_ex,pincode
where tab_ex.district=pincode.district

so this will match the district on each other table is that rite i am reckoning.

So i should be able to insert into tab_ex1 with pincode value of respective district  from tab_ex. but tab_ex_err remains same as tab_ex don;t contain pincode field only error messgae field that u hv already. coz when we can;t split the address i think its not worth to get pin. right?

Please advice regarding this problem!
Thanx.

 

by: khushi2003Posted on 2004-04-04 at 09:17:25ID: 10752356

with that 14 data it works perfectly alrite. but i hv some more data that needs to be analyse like u did before, like u hv set rules for such datas.

one more rule what i hv found is, differnce between personal address and company address,
I am putting how my original table looks like:
for eg:
TAB_EX
id,
name
company_name
fld1 (fulladdress but as u hv called it fld1 so it will be easier to know u)
district

Like in my some rows name field's value is null, and only company name is there, on that case company address can only contain roadname.
road address does not have to have street number if it is company address Only privatre address needs its street number and name to be accepted?
This case applies when there is only name of street not number, ur code exaclty does what i need like throws data into exception table those only have street name, but when it is not personal address (its company address) then its valid should be in tab_ex1 in roadname col.
We know this if name field is null and name of company in company field is available!

and this part of ur code

IF instr(v_roadnm, 'Rd') > 0
            OR instr(v_roadnm, 'Road') > 0
            OR instr(v_roadnm, 'Avenue') > 0
            OR instr(v_roadnm, 'Ave') > 0
            OR instr(v_roadnm, 'Street') > 0
            OR instr(v_roadnm, 'Pde') > 0
            OR instr(v_roadnm, 'PO Box') > 0

i hv added few more words to check like Hwy, Drive, is that fine? does it checks similarly as it does with Road, Avenue etc.


 

by: khushi2003Posted on 2004-04-04 at 09:38:57ID: 10752418

and...
in this part of code
 THEN
                dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm);
                INSERT INTO tab_ex1 VALUES(v_roadno, v_roadnm);
            ELSE
                dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm || '****Problem');
                INSERT INTO tab_ex_err VALUES(c1_rec.fld1,'Invalid Road Name');
            END IF;
        ELSE
            dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm || '****Problem');
            INSERT INTO tab_ex_err VALUES(c1_rec.fld1,'Invalid address format');
        END IF;

how can i insert whole data beside two splitted data, like
insert into tab_ex1(id,name,company,roadnum(v_roadno),roadname(v_roadnm),postcode) select id,name,..................from tab_ex but i am confused how should i do ?

 

by: pratikroyPosted on 2004-04-05 at 02:47:01ID: 10755936

Let me tell you, this code is not so big, as you think it is. There are a lot of comments in it, which makes it look like a big code. The comments were added for your benefit, and you can get rid of them if you want your code to look shorter. Apart from that, I have indented the code, and have spaces in between logically different cases, to make it more readable.

You have added a lot more stuff to your question, and I am thoroughly confused :) To avoid confusions, let me restate the additional stuff that you want. Correct me if I am wrong, or let me know if this is what you want :
1. Additional Column District in Tab_Ex : This District would be existing in PINCODES table.
2. Additional Column Company Name in Tab_Ex : Rule->If the company name exists, then allow roadname (ignore the check that throws it in exception table if only roadname is provided).
3. A table PINCODES, which will have district, Pin code, and state name. : Master table, which would be referred to fetch the value of PINCODE based on District.
4. Additional Columns Distict and Pin Code in Tab_Ex1. District would come from Tab_Ex1, whereas Pin Code would come from PINCODES table, based on District.
5. Few additional valid Street Names like Drive, Hwy etc. : Yes you can add them to that check, and it would work fine.

This is how the final structures of the three tables would look like :
SQL> desc tab_ex
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER
 NAME                                               VARCHAR2(50)
 COMP_NAME                                          VARCHAR2(50)
 FLD1                                               VARCHAR2(250)
 DISTRICT                                           VARCHAR2(50)

SQL> desc tab_ex1
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER
 NAME                                               VARCHAR2(50)
 COMP_NAME                                          VARCHAR2(50)
 ROADNUM                                            VARCHAR2(50)
 ROADNAME                                           VARCHAR2(200)
 POSTCODE                                           VARCHAR2(30)

SQL> desc tab_ex_err
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER
 NAME                                               VARCHAR2(50)
 COMP_NAME                                          VARCHAR2(50)
 FLD1                                               VARCHAR2(250)
 DISTRICT                                           VARCHAR2(50)
 ERRMSG                                             VARCHAR2(200)

SQL>

Let me know if I am right in understanding your additional questions, so that I could provide the answers.

 

by: pratikroyPosted on 2004-04-05 at 02:56:40ID: 10755974

Also the PINCODEs table :
SQL> desc tab_pins
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 POSTCODE                                           VARCHAR2(30)
 DISTRICT                                           VARCHAR2(50)
 STATE                                              VARCHAR2(30)

 

by: pratikroyPosted on 2004-04-05 at 03:04:53ID: 10756001

A few more questions :
1. What happens in case the name and comp_name both are populated ?
2. Address like "Barker & Anzac Pde" and "Warf Rd & Hollywood Rd" would be valid in case a company name exists, else they would be treated as invalid ?
3. Address like "Street" would be treated as invalid even if there is a company name attached to the address, as "Street" is not a valid street name.
4. Addresses that have "PO Box" would be valid for personal addresses as well ?

 

by: pratikroyPosted on 2004-04-05 at 03:26:39ID: 10756072

some more questions :)
1. What if there is a road/street number entered for the company address, like "2 greenwood street" I think it would be valid.
2. What if the person's name as well as company's name is missing from the address. I think you might want to ignore such records.
3. What if the district is missing from the record ? No PIN would be allocated to the address.
4. What if the district has two reords in the PINCODES table ? I think it would be enforced in the table, and case like this should never occur.
5. What if the District's record is not found in the PINCODES table ?

 

by: khushi2003Posted on 2004-04-05 at 03:32:25ID: 10756092

Yha..happy to see ur posting
Yha ur code is very well formatted and with comment and indented, what i am looking for. But i am talking about can i break this code into smaller procedure and call from main procedure or function, so that i fulfill the definition of reusable, modularise code.

Yha the table created is exactly what i am looking but problem is that my pincodes table data is too huge so i am dealing with specific data sorry i hv missed one field in PINCODES table that is category
now it looks like:
SQL> desc tab_pins
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 POSTCODE                                           VARCHAR2(30)
 DISTRICT                                           VARCHAR2(50)
 STATE                                              VARCHAR2(30)
CATEGORY                                        VARCHAR2(40)

Coz i can't match tab_ex and pincode table becoz of large in size so most of the customer they are in tab_ex are for suppose "West Bengal" state and their category is "Courier service". did u check my above SQL statement?
i think i need to add thier:
state='West Bengal'
and category like '%Courier service%'
right?

Now the question u hv put upon me-
1. What happens in case the name and comp_name both are populated ?
>>Its fine exactly what u hv did before if its incase of both., only for company, nto for private address (only private address needs road number)
2. Address like "Barker & Anzac Pde" and "Warf Rd & Hollywood Rd" would be valid in case a company name exists, else they would be treated as invalid ?
>>Yes!
3. Address like "Street" would be treated as invalid even if there is a company name attached to the address, as "Street" is not a valid street name.
>>yah only street is not valid name, even if it is company coz street doesn't make sense.
4. Addresses that have "PO Box" would be valid for personal addresses as well ?
Yha, just go like null and POBOX for roadnum and roadname

Thanx for taking ur time to read my postings!
Regards

 

by: khushi2003Posted on 2004-04-05 at 03:40:48ID: 10756118

some more questions :)
1. What if there is a road/street number entered for the company address, like "2 greenwood street" I think it would be valid.
>>yha! i think so for its valid
2. What if the person's name as well as company's name is missing from the address. I think you might want to ignore such records.
>>Yha throw into the tab_ex_err with some missing errmsg
3. What if the district is missing from the record ? No PIN would be allocated to the address.
>>yha thanx for finding out that problem i just faced that problem, if that is the case then simply i wanna throw it into tab_ex_err table and if there are 2 PIN for same district that will also goes to tab_ex_err table.
4. What if the district has two reords in the PINCODES table ? I think it would be enforced in the table, and case like this should never occur.
>>Wow, u have found every problem, sorry i didn't read whole question at a time so this answer is included in Q3 hehe.
5. What if the District's record is not found in the PINCODES table ?
>>Throw it into tab_ex_err table.

Regards!

 

by: pratikroyPosted on 2004-04-05 at 03:59:48ID: 10756191

What is the significance of Category ? Will the PIN code be dependent on category ? Is'nt District and Pin Code having 1-to-1 relation ?
Can you give a sample data for Pin_codes table ?
Will it be ....

POSTCODE   DISTRICT      STATE              CATEGORY
=======   ========   ======          =====
1234455       Wellington     W.Bengal          Courier Service
1234456       Wellington     W.Bengal          IPS

Will there be separate post code for Courier Service and Indian Postal Service (IPS) ??

I thought it might be :
POSTCODE   DISTRICT      STATE              CATEGORY
=======   ========   ======          =====
1234455       Wellington     W.Bengal          Courier Service
1234455       Wellington     W.Bengal          IPS

same for the two categories, as they belong to the same District.

Please Clarify.

 

by: khushi2003Posted on 2004-04-05 at 04:19:25ID: 10756296

Yha u r right, coz my tab_ex table (actually it has more than thousands rows) is only dealing with 'W.bengal' state and Courier Service category, so when ever you will match to get post code from PINCODE table u have to care only that state and category, so it will check few rows from thousands and thousands of POSTCODES.
yha they should have 1-1 relation but one district can hv more than 1 PINCODE that is case, but its fine if one districts retrieves more than 1 PIN then should be in tab_ex_err table.

from ur example postcode 1234455 can have more than 1 district. can various category so we hv to match with that category and state so most of the tab_ex table falls under that category.

 

by: khushi2003Posted on 2004-04-05 at 04:26:37ID: 10756332

is it good idea to use min(district) or where rownum < 2

 

by: pratikroyPosted on 2004-04-05 at 04:43:31ID: 10756436

What if the address has any other state apart from West Bengal ? Do you want me to put it in Exceptions table ?

 

by: khushi2003Posted on 2004-04-05 at 04:43:47ID: 10756440

In the PINCODES table...For State = 'W.Bengal' (suppose) and Category = 'Courier Service', 2-3 postcodes are repeated for different districts

even after filtering, i think we should better throw it into tab_ex_err table.
Regards

 

by: khushi2003Posted on 2004-04-05 at 04:45:26ID: 10756446

What if the address has any other state apart from West Bengal ? Do you want me to put it in Exceptions table ?
>>as we are only dealing with w.bengal but if there is by chance then should be throw into exception table.

Regards

 

by: pratikroyPosted on 2004-04-05 at 04:48:39ID: 10756462

I did'nt get your last point. Please re-phrase.

 

by: khushi2003Posted on 2004-04-05 at 04:58:27ID: 10756524

As we are only dealing with "W.bengal" state, so i think when we filter using state and category tab_ex's address falls under that w.bengal state. Yes as u hv mentioned it goes to exception table coz we can;t have other state name only w.bengal.

Is that clear if not please ask me!

 

by: pratikroyPosted on 2004-04-05 at 05:01:48ID: 10756548

I was talking about your comment "In the PINCODES table...For State = 'W.Bengal' (suppose) and Category = 'Courier Service', 2-3 postcodes are repeated for different districts
even after filtering, i think we should better throw it into tab_ex_err table."
Please re-phrase this one for me.

 

by: khushi2003Posted on 2004-04-05 at 05:06:23ID: 10756571

oh! sorry

I mean to say that i hv matched the suburb from tab_ex and PINCODES table (using state and category as my filter) there are only 2 or 3 repeated postcodes for same district.
So if that is the case i would like to throw that into tab_ex_err table
Regards

 

by: khushi2003Posted on 2004-04-05 at 05:07:08ID: 10756578

Am i disturbing ur cricket? heheh..........

 

by: pratikroyPosted on 2004-04-05 at 05:12:52ID: 10756623

Yes, cricket is going on superb :) I am just watching the scores on the cricinfo.com :)

As for the comments, I think what you mean is :
1. The district in address will definitely be of West Bengal and of "Courier Service". If its anything else, it should go to Exception table.
2. In case there are more than one records found, after filtering for West Bengal and Courier Service, I still have to throw that record in the Exception table.

I am re-writing the code, with some functions procedures as you wanted :) Would present the code after I am through with the coding and testing :)

 

by: khushi2003Posted on 2004-04-05 at 05:23:11ID: 10756680

As for the comments, I think what you mean is :
1. The district in address will definitely be of West Bengal and of "Courier Service". If its anything else, it should go to Exception table.
>>Yes, with comment like "postcode problem" somethinbg like that!
2. In case there are more than one records found, after filtering for West Bengal and Courier Service, I still have to throw that record in the Exception table.
>>Yes, absolutely
regards!

 

by: khushi2003Posted on 2004-04-05 at 06:23:44ID: 10757082

Pratik,
Can you break that long code into few small procedures or function and call and do my work from one main procedure/function.
Cause my requirement is modular code, as much as possible!
Regards!

 

by: pratikroyPosted on 2004-04-05 at 07:53:00ID: 10757796

OK, the latest table structures are :

SQL> desc tab_ex
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- -------------------------------- ID                                                                         NUMBER
 NAME                                                                       VARCHAR2(50)
 COMP_NAME                                                                  VARCHAR2(50)
 FLD1                                                                       VARCHAR2(250)
 DISTRICT                                                                   VARCHAR2(50)

SQL> desc tab_ex1
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- -------------------------------- ID                                                                         NUMBER
 NAME                                                                       VARCHAR2(50)
 COMP_NAME                                                                  VARCHAR2(50)
 ROADNUM                                                                    VARCHAR2(50)
 ROADNAME                                                                   VARCHAR2(200)
 POSTCODE                                                                   VARCHAR2(30)

SQL> desc tab_ex_err
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- -------------------------------- ID                                                                         NUMBER
 NAME                                                                       VARCHAR2(50)
 COMP_NAME                                                                  VARCHAR2(50)
 FLD1                                                                       VARCHAR2(250)
 DISTRICT                                                                   VARCHAR2(50)
 ERRMSG                                                                     VARCHAR2(200)

SQL> desc tab_pins
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- -------------------------------- POSTCODE                                                                   VARCHAR2(30)
 DISTRICT                                                                   VARCHAR2(50)
 STATE                                                                      VARCHAR2(30)
 CATEGORY                                                                   VARCHAR2(30)

To test my latest code Create the above tables
Insert the following records in TAB_PINS :
POSTCODE   DISTRICT             STATE           CATEGORY
---------- -------------------- --------------- ---------------
123345     Salt Lake            West Bengal     Courier Service
123346     Wellington           West Bengal     IPS
123347     Bhubneshwar          Orissa          Courier Service
123348     Kolkata              West Bengal     Courier Service
123349     Kolkata              West Bengal     IPS
123350     Darjeeling           West Bengal     Courier Service
123350     Darjeeling           West Bengal     IPS
123350     Darjeeling           West Bengal     Courier Service

Insert the following records in TAB_EX :
        ID NAME                 COMP_NAME                 FLD1                           DISTRICT
---------- -------------------- ------------------------- ------------------------------ ---------------
         1 Alan Border                                    1 Ada Street                   Salt Lake
         2 Steve Bucknor                                  1 A Kalinda Street             Salt Lake
         3 Tony Day                                       1/13 Burrawang Street          Salt Lake
         4 Phil Hampson                                   1/21-23 Chelmsford Road        Salt Lake
         5 John Reid                                      10/1a Frederick Street         Salt Lake
         6 Steve Wright                                   161? Lang Street               Salt Lake
         7 Jason Gillespie                                368 Cnr Barker & Anzac Pde     Salt Lake
         8 Greg Chapman                                   5/70-74/ Wilson Street         Salt Lake
         9 Paul Radcliffe                                 Shop 2 4-14 Claudia Road       Salt Lake
        10 George Hemmingway                              PO Box 558                     Salt Lake
        11 Dave Mathews                                   Lot 4 Mungerie Road            Salt Lake
        12 Jonty Rhodes                                   Level 4/65 Albert Avenue       Salt Lake
        13 Clive Llyode                                   Cnr Warf Rd & Hollywood Rd     Salt Lake
        14 John Davies                                    Unit 85 51 Little Willandra Rd Salt Lake
        15 Stephen Matthews                               Suite 2 Ground Floor           Salt Lake
        16 Chris Harris                                   Street                         Salt Lake
        17                      Anzac Solicitors          Burwood Road                   Salt Lake
        18                      Joe & Joyce Travels       Paramatta Street               Salt Lake
        19 Ruth Hughes                                    Harmondsworth Street           Salt Lake
        20                      Greenwood Technologies    Cranston Drive                 Salt Lake
        21                      Haywire News Ltd          Martindale Road                Salt Lake
        22 Mark Jones                                     12 Ada Street                  Wellington
        23 Peter Radcliffe                                14 Ada Street                  Bhubneshwar
        24 Michael Chang                                  15 Ada Street                  Kolkata
        25 Chris Gayle                                    18 Ada Street                  Darjeeling

Once the table and appropriate data is created, then run the following code :

DECLARE
    v_roadno   varchar2(50);
    v_roadnm   varchar2(200);
    v_lastnum  varchar(100);
    v_tempnm   varchar2(200);
    v_addrtyp  varchar2(30);
    v_postcode varchar2(30);
    v_errmsg   varchar2(250);

    cursor c1 is
    select * from tab_ex;

    FUNCTION f_addr_type ( fulladdr varchar2)
    RETURN VARCHAR2 IS
     v_space VARCHAR2(30);
    BEGIN
        IF INSTR(fulladdr, 'Cnr') > 0 THEN
            v_space := 'Cnr';
        END IF;
        IF INSTR(fulladdr, 'Shop') > 0 THEN
            v_space := 'Shop';
        END IF;
        IF INSTR(fulladdr, 'PO Box') > 0 THEN
            v_space := 'PO Box';
        END IF;
        IF INSTR(fulladdr, 'Lot') > 0 THEN
            v_space := 'Lot';
        END IF;
        IF INSTR(fulladdr, 'Level') > 0 THEN
            v_space := 'Level';
        END IF;
        IF INSTR(fulladdr, 'Unit') > 0 THEN
            v_space := 'Unit';
        END IF;
        RETURN v_space;
    END f_addr_type;

    PROCEDURE p_pincode (i_district IN varchar2, o_postcode OUT varchar2, o_errmsg OUT varchar2) IS
     v_msg VARCHAR2(250);
    BEGIN
        BEGIN
            SELECT POSTCODE
            INTO   o_postcode
            FROM   tab_pins
            WHERE  DISTRICT = i_district
            AND STATE       = 'West Bengal'
            AND CATEGORY    = 'Courier Service'
            AND ROWNUM      = 1;
        EXCEPTION
        WHEN NO_DATA_FOUND THEN
            o_errmsg:='No Data Found for District in PINCODES table';
        WHEN OTHERS THEN
            o_errmsg:='Others Exception raised for the district';
        END;
    END p_pincode;

    FUNCTION f_logerr (tabexrec tab_ex%rowtype, i_errmsg varchar2)
    RETURN VARCHAR2
    IS
    v_space VARCHAR2(30);
    BEGIN
        insert into tab_ex_err
        (ID, NAME, COMP_NAME, FLD1, DISTRICT, ERRMSG)
        values
        (tabexrec.id, tabexrec.NAME, tabexrec.COMP_NAME, tabexrec.FLD1, tabexrec.DISTRICT, i_errmsg);
        RETURN v_space;
    END f_logerr;


BEGIN -- Main Block
    for c1_rec in c1
    loop
        -- First check whether the District is valid (the one that we are looking for)
        IF c1_rec.DISTRICT IS NULL THEN
            v_errmsg := 'District is not provided';
            v_errmsg := f_logerr(c1_rec,v_errmsg);
            goto loop_next;
        ELSE
            p_pincode(c1_rec.district, v_postcode, v_errmsg);
            IF v_errmsg IS NOT NULL THEN
                v_errmsg := f_logerr(c1_rec,v_errmsg);
                goto loop_next;
            END IF;
        END IF;

        -- Check the format of the address
        v_addrtyp := f_addr_type (c1_rec.fld1);

        -- Case 1. "Cnr"
        --         ignore everything before "Cnr" and put the rest in the house/road name
        IF v_addrtyp = 'Cnr' THEN
            v_roadno := '';
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1, 'Cnr')+ 4);
            goto proc_next;
        END IF;

        -- Case 2. "Shop" -- Case 4. "Lot" -- Case 5. "Level" -- Case 6. "Unit"
        IF v_addrtyp IN ('Shop','Lot','Level','Unit','House') THEN
            -- Find the last number in the string
            v_lastnum := substr(trim(translate(upper(c1_rec.fld1),
                         '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);
            v_roadno  := substr(c1_rec.fld1,1,INSTR(c1_rec.fld1,v_lastnum,-1));
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1,v_lastnum,-1)+1);
            goto proc_next;
        END IF;

        -- Case 3. "PO Box"
        --         Put everything in road name
        IF INSTR(c1_rec.fld1, 'PO Box') > 0 THEN
            v_roadno := '';
            v_roadnm := c1_rec.fld1;
            goto proc_next;
        END IF;

        -- If none of the above cases, then its the normal address.
        -- Find the last number in the string
        v_lastnum := substr(trim(translate(upper(c1_rec.fld1),
                     '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);
        v_roadno  := substr(c1_rec.fld1,1,INSTR(c1_rec.fld1,v_lastnum,-1));
        v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1,v_lastnum,-1)+1);

        IF v_roadno IS NULL AND v_roadnm IS NULL THEN
            v_roadnm := c1_rec.fld1;
        END IF;

        <<proc_next>>
        v_roadno := trim(v_roadno);
        v_roadnm := trim(v_roadnm);
        -- last thing, check if the last character of road number is '/' or '?'. Strip if this is the case
        IF substr(v_roadno,-1) = '/' OR substr(v_roadno,-1) = '?' THEN
            v_roadno := substr(v_roadno,1,length(v_roadno)-1);
        END IF;

        IF instr(v_roadnm,' ') > 0 THEN
            -- If road name has more than one word then check if it has
            -- one of the valid values - Rd, Road, Avenue, Street, Pde or PO BOX
            IF instr(v_roadnm, 'Rd') > 0
            OR instr(v_roadnm, 'Road') > 0
            OR instr(v_roadnm, 'Avenue') > 0
            OR instr(v_roadnm, 'Ave') > 0
            OR instr(v_roadnm, 'Street') > 0
            OR instr(v_roadnm, 'Pde') > 0
            OR instr(v_roadnm, 'PO Box') > 0
            OR instr(v_roadnm, 'Drive') > 0
            OR instr(v_roadnm, 'Hwy') > 0
            THEN -- THIS IS A VALID RECORD. Insert into TAB_EX1

                -- If Road Number is null and company name does not exist (i.e., a personal address) then its invalid
                IF v_roadno IS NULL and c1_rec.comp_name IS NULL AND INSTR(v_roadnm, 'PO Box') <= 0 THEN
                    dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm || '****Problem');
                    v_errmsg := 'Road Number not provided for personal address';
                    v_errmsg := f_logerr(c1_rec,v_errmsg);
                    goto loop_next;
                END IF;

                dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm);

                INSERT INTO tab_ex1
                (ID, NAME, COMP_NAME, ROADNUM, ROADNAME, POSTCODE)
                VALUES
                (c1_rec.ID, c1_rec.NAME, c1_rec.COMP_NAME, v_roadno, v_roadnm, v_postcode);

            ELSE
                dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm || '****Problem');
                v_errmsg := 'Invalid Road Name';
                v_errmsg := f_logerr(c1_rec,v_errmsg);
            END IF;
        ELSE
            dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm || '****Problem');
            v_errmsg := 'Invalid address format';
            v_errmsg := f_logerr(c1_rec,v_errmsg);
        END IF;

        <<err_rec>>
        NULL;

        <<loop_next>>
        NULL;
    end loop;
end;
/

 

by: pratikroyPosted on 2004-04-05 at 07:56:21ID: 10757836

After you run the code, you can expect the following in TAB_EX1 :

        ID NAME                 COMP_NAME                 ROADNUM    ROADNAME                       POSTCODE
---------- -------------------- ------------------------- ---------- ------------------------------ ----------
         1 Alan Border                                    1          Ada Street                     123345
         2 Steve Bucknor                                  1          A Kalinda Street               123345
         3 Tony Day                                       1/13       Burrawang Street               123345
         4 Phil Hampson                                   1/21-23    Chelmsford Road                123345
         5 John Reid                                      10/1       a Frederick Street             123345
         6 Steve Wright                                   161        Lang Street                    123345
         8 Greg Chapman                                   5/70-74    Wilson Street                  123345
         9 Paul Radcliffe                                 Shop 2 4-1 Claudia Road                   123345
        10 George Hemmingway                                         PO Box 558                     123345
        11 Dave Mathews                                   Lot 4      Mungerie Road                  123345
        12 Jonty Rhodes                                   Level 4/65 Albert Avenue                  123345
        14 John Davies                                    Unit 85 51 Little Willandra Rd            123345
        17                      Anzac Solicitors                     Burwood Road                   123345
        18                      Joe & Joyce Travels                  Paramatta Street               123345
        20                      Greenwood Technologies               Cranston Drive                 123345
        21                      Haywire News Ltd                     Martindale Road                123345
        24 Michael Chang                                  15         Ada Street                     123348
        25 Chris Gayle                                    18         Ada Street                     123350

18 rows selected.

And following records would be present in TAB_EX_ERR :

        ID NAME             COMP_NAME  FLD1                           DISTRICT     ERRMSG
---------- ---------------- ---------- ------------------------------ ------------ ----------------------------
         7 Jason Gillespie             368 Cnr Barker & Anzac Pde     Salt Lake    Road Number not provided for personal address
        13 Clive Llyode                Cnr Warf Rd & Hollywood Rd     Salt Lake    Road Number not provided for personal address
        15 Stephen Matthews            Suite 2 Ground Floor           Salt Lake    Invalid Road Name
        16 Chris Harris                Street                         Salt Lake    Invalid address format
        19 Ruth Hughes                 Harmondsworth Street           Salt Lake   Road Number not provided for personal address
        22 Mark Jones                  12 Ada Street                  Wellington   No Data Found for District in PINCODES table
        23 Peter Radcliffe             14 Ada Street                  Bhubneshwar  No Data Found for District in PINCODES table

7 rows selected.

 

by: pratikroyPosted on 2004-04-05 at 08:00:48ID: 10757887

Hope this is what you wanted. I suggest that you test this with your set of data also, after  you have tested it with the data that I have suggested.

Let me know how you feel about it.

All the best !!

 

by: khushi2003Posted on 2004-04-05 at 08:34:50ID: 10758194

Thanx Pratik i will test it! and let u know the feedback once again thanx a lot!

 

by: pratikroyPosted on 2004-04-05 at 08:41:16ID: 10758255

You are welcome :)

 

by: khushi2003Posted on 2004-04-05 at 09:04:34ID: 10758463

I am bit confused with ur code coz it starts with .......

anonymous block....

and then i found procedure followed by cursor
can u explain me what's going there in the code please

 

by: khushi2003Posted on 2004-04-05 at 09:12:49ID: 10758547

Can u specify me which functin/procedure does what stuff?

coz ur first line of code seems to start from declare is that anonymous block?
regards!

 

by: khushi2003Posted on 2004-04-05 at 09:31:07ID: 10758709

did u missed to insert exception data into tab_ex_err table?

 

by: khushi2003Posted on 2004-04-05 at 09:34:31ID: 10758753

Oh sorry got it in p_pincode procedure, but ia m still confuse with your procedure and function coz it is anonymous, so can;t store in DB.
Can you explain me regarding how it works if it is possible can u change into named procedure and function, sorry to take ur time!
regards!

 

by: khushi2003Posted on 2004-04-05 at 09:46:30ID: 10758856

Actually i hv to store all the procedure and function in DB, so it test whether my function/procedure works or not, i hv to delete table such as tab_ex1, and tab_ex_err. and then run my stored procedure th fill those tables.
I am trying to understand ur code it takes some time for me!

 

by: pratikroyPosted on 2004-04-05 at 14:47:12ID: 10761374

An Anonymous Block can have functions and procedures, just like any other PL/SQL Block (Package/Procedure/Function).

If want you can make it a named block :)

 

by: pratikroyPosted on 2004-04-05 at 14:53:38ID: 10761419

You have asked new questions again in this string of questions :)

The only difference between anonymous block and named block is that you can store the named block in the DB (after compiling it). Whereas anonymous block like I have written will be compiled and executed at run time (each time you run the script).

 

by: pratikroyPosted on 2004-04-05 at 14:58:29ID: 10761448

You wanted me to break my code into functions and procedures, so I did that :)

You probably might have figured it out by now, but here is what the functions and procedure are doing :
1. FUNCTION f_addr_type ( fulladdr varchar2) : Checks which type of address it is. It will basically return "Shop", "Cnr" etc.
2. PROCEDURE p_pincode (i_district IN varchar2, o_postcode OUT varchar2, o_errmsg OUT varchar2) : Is fetching the postcode, and populating errmsg, in case there is some problem with the postcode.
3. FUNCTION f_logerr (tabexrec tab_ex%rowtype, i_errmsg varchar2) : Logs the error in the exceptions table.

Let me know if you have any more questions.

 

by: khushi2003Posted on 2004-04-05 at 18:39:28ID: 10762422

Yha ! got it
function f_addr_type
and procedure p_pincode is named.
but the first line of code starts from
declare
..............

and again later starts with

Begin --main block, is that same block?

so can i turn it into named procedure. I am just confused coz i saw between anonymous blocks there are procedures/functions. Is that the main block to call procedure/function?

Okay i will try to convert it into procedure, do that anonymous bock need any parameters when i turned into named one. or simply i can only give name.?
regards!


 

by: khushi2003Posted on 2004-04-05 at 23:36:16ID: 10763441

Oh now i got it what u did!
ur code starts from anonymous block
declare...

and there are two functions and one procedures are in between them.

i have stored that functions and procedures in my DB using commad CREATE OR REPLACE function <function name>

but if i changed that anonymous main block into named procedure do i need parameter?
 how i like to test is i will delete rows from both table tab_ex1 and tab_ex_err and when i run my main procedure (right now it is anonymous) then it performs the work!

can i create like this:

CREATE OR REPLACE CALL_ALL IS

    v_roadno   varchar2(50);
    v_roadnm   varchar2(200);
    v_lastnum  varchar(100);
    v_tempnm   varchar2(200);
    v_addrtyp  varchar2(30);
    v_postcode varchar2(30);
    v_errmsg   varchar2(250);

    cursor c1 is
    select * from tab_ex;

....
...
end CALL_ALL;

regards!

 

by: pratikroyPosted on 2004-04-06 at 01:11:14ID: 10763835

Just replace the "DECLARE" on the first line to "CREATE OR REPLACE PROCEDURE PROC_TAB_EX AS"

Once this is done, run the SQL script that you have created (with the code that I had given). This would compile the named stored procedure PROC_TAB_EX.

You can then run the procedure by giving command :
exec proc_tab_ex;

You dont need any parameters, because we do everything within the procedure itself.

 

by: khushi2003Posted on 2004-04-06 at 01:49:41ID: 10763985

Okay Got It!
I don;t think now i can ask u more question, coz i am sure its irritating you.
and making bore to you!
regards

 

by: pratikroyPosted on 2004-04-06 at 03:05:40ID: 10764306

Ask more questions if you still are unclear about the above solution. Don't worry. Its not irritating ... :) I enjoy chatting anyways ... and chatting about a solution is great :)

 

by: pratikroyPosted on 2004-04-06 at 03:07:39ID: 10764314

Once you are through with your testing, accept the answer that I gave in post at "Date: 04/05/2004 07:53AM PDT", so that anyone else who wants to benefit from this solution, can read the detailed answer that I gave in that post.

All the best !!

 

by: khushi2003Posted on 2004-04-06 at 03:15:45ID: 10764352

Sure !

but why i am having problem when retrieving data:

like for example i have 300 rows in tab_ex
and after running all procedure/function
i sould only get total rows 300 including on both tables

but when i add rows from two tables its exceeds (more than 300) 300 rows. I think same data are inserted into both tables.

regards

 

by: khushi2003Posted on 2004-04-06 at 03:55:17ID: 10764553

Now it is retriving correct rows, got problem in joins thanx!

 

by: pratikroyPosted on 2004-04-06 at 09:23:39ID: 10767337

Can you please accept the answer and PAQ this question if you are through with your testing.

Thanks!

 

by: khushi2003Posted on 2004-04-07 at 04:09:25ID: 10773520

I hv got some prob!
1. ur previous code used parse this address " 1  A Kalinda Street" as "1A", "kalainda Street" but now it does same at code used to do. It produce  "1" , "A Kalinda Street"
2.same case for "10/1a Frederick Street"
3.Why does it splits this "tom &  jerry Street",  as  "tom &", "jerry Street";
4."10a M'cac street" produce " 10a M' ", "cac Street"

Sorry pratik i definitely accept ur answer, but i dont; want to open new question on same topic previously i did as. So i will like to include points in every question as i did from start of question! (-:

Regards!

 

by: khushi2003Posted on 2004-04-07 at 04:12:06ID: 10773538

3.Why does it splits this "tom &  jerry Street",  as  "tom &", "jerry Street";

It does this parsing in tab_ex1 table in roadname field though it is valid address.

regards!  

 

by: pratikroyPosted on 2004-04-07 at 14:48:07ID: 10778993

Try this now :

--create or replace procedure proc_tab_ex as
declare
    v_roadno   varchar2(50);
    v_roadnm   varchar2(200);
    v_lastnum  varchar(100);
    v_tempnm   varchar2(200);
    v_addrtyp  varchar2(30);
    v_postcode varchar2(30);
    v_errmsg   varchar2(250);

    cursor c1 is
    select * from tab_ex;

    FUNCTION f_addr_type ( fulladdr varchar2)
    RETURN VARCHAR2 IS
     v_space VARCHAR2(30);
    BEGIN
        IF INSTR(fulladdr, 'Cnr') > 0 THEN
            v_space := 'Cnr';
        END IF;
        IF INSTR(fulladdr, 'Shop') > 0 THEN
            v_space := 'Shop';
        END IF;
        IF INSTR(fulladdr, 'PO Box') > 0 THEN
            v_space := 'PO Box';
        END IF;
        IF INSTR(fulladdr, 'Lot') > 0 THEN
            v_space := 'Lot';
        END IF;
        IF INSTR(fulladdr, 'Level') > 0 THEN
            v_space := 'Level';
        END IF;
        IF INSTR(fulladdr, 'Unit') > 0 THEN
            v_space := 'Unit';
        END IF;
        RETURN v_space;
    END f_addr_type;

    PROCEDURE p_pincode (i_district IN varchar2, o_postcode OUT varchar2, o_errmsg OUT varchar2) IS
     v_msg VARCHAR2(250);
    BEGIN
        BEGIN
            SELECT POSTCODE
            INTO   o_postcode
            FROM   tab_pins
            WHERE  DISTRICT = i_district
            AND STATE       = 'West Bengal'
            AND CATEGORY    = 'Courier Service'
            AND ROWNUM      = 1;
        EXCEPTION
        WHEN NO_DATA_FOUND THEN
            o_errmsg:='No Data Found for District in PINCODES table';
        WHEN OTHERS THEN
            o_errmsg:='Others Exception raised for the district';
        END;
    END p_pincode;

    FUNCTION f_logerr (tabexrec tab_ex%rowtype, i_errmsg varchar2)
    RETURN VARCHAR2
    IS
    v_space VARCHAR2(30);
    BEGIN
        insert into tab_ex_err
        (ID, NAME, COMP_NAME, FLD1, DISTRICT, ERRMSG)
        values
        (tabexrec.id, tabexrec.NAME, tabexrec.COMP_NAME, tabexrec.FLD1, tabexrec.DISTRICT, i_errmsg);
        RETURN v_space;
    END f_logerr;


BEGIN -- Main Block
    for c1_rec in c1
    loop
        -- First check whether the District is valid (the one that we are looking for)
        IF c1_rec.DISTRICT IS NULL THEN
            v_errmsg := 'District is not provided';
            v_errmsg := f_logerr(c1_rec,v_errmsg);
            goto loop_next;
        ELSE
            p_pincode(c1_rec.district, v_postcode, v_errmsg);
            IF v_errmsg IS NOT NULL THEN
                v_errmsg := f_logerr(c1_rec,v_errmsg);
                goto loop_next;
            END IF;
        END IF;

        -- Check the format of the address
        v_addrtyp := f_addr_type (c1_rec.fld1);

        -- Case 1. "Cnr"
        --         ignore everything before "Cnr" and put the rest in the house/road name
        IF v_addrtyp = 'Cnr' THEN
            v_roadno := '';
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1, 'Cnr')+ 4);
            goto proc_next;
        END IF;

        -- Case 2. "Shop" -- Case 4. "Lot" -- Case 5. "Level" -- Case 6. "Unit"
        IF v_addrtyp IN ('Shop','Lot','Level','Unit','House') THEN
            -- Find the last number in the string
            v_lastnum := substr(trim(translate(upper(c1_rec.fld1),
                         '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);
            v_roadno  := substr(c1_rec.fld1,1,INSTR(c1_rec.fld1,v_lastnum,-1));
            v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1,v_lastnum,-1)+1);
            goto proc_next;
        END IF;

        -- Case 3. "PO Box"
        --         Put everything in road name
        IF INSTR(c1_rec.fld1, 'PO Box') > 0 THEN
            v_roadno := '';
            v_roadnm := c1_rec.fld1;
            goto proc_next;
        END IF;

        -- If none of the above cases, then its the normal address.
        -- Find the last number in the string
        -- If none of the above cases, then its the normal address.
        v_roadno := substr(c1_rec.fld1, 1, instr(c1_rec.fld1, ' '));
        v_roadnm := substr(c1_rec.fld1, instr(c1_rec.fld1, ' '));

        v_lastnum := substr(trim(translate(upper(v_roadno),
                     '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);

        IF v_lastnum is null THEN
             v_roadno := NULL;
             v_roadnm := c1_rec.fld1;
        END IF;

/*
        v_lastnum := substr(trim(translate(upper(c1_rec.fld1),
                     '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')),-1);
        v_roadno  := substr(c1_rec.fld1,1,INSTR(c1_rec.fld1,v_lastnum,-1));
        v_roadnm := substr(c1_rec.fld1, INSTR(c1_rec.fld1,v_lastnum,-1)+1);
*/

        IF v_roadno IS NULL AND v_roadnm IS NULL THEN
            v_roadnm := c1_rec.fld1;
        END IF;

        <<proc_next>>
        v_roadno := trim(v_roadno);
        v_roadnm := trim(v_roadnm);
        -- last thing, check if the last character of road number is '/' or '?'. Strip if this is the case
        IF substr(v_roadno,-1) = '/' OR substr(v_roadno,-1) = '?' THEN
            v_roadno := substr(v_roadno,1,length(v_roadno)-1);
        END IF;

        IF instr(v_roadnm,' ') > 0 THEN
            -- If road name has more than one word then check if it has
            -- one of the valid values - Rd, Road, Avenue, Street, Pde or PO BOX
            IF instr(v_roadnm, 'Rd') > 0
            OR instr(v_roadnm, 'Road') > 0
            OR instr(v_roadnm, 'Avenue') > 0
            OR instr(v_roadnm, 'Ave') > 0
            OR instr(v_roadnm, 'Street') > 0
            OR instr(v_roadnm, 'Pde') > 0
            OR instr(v_roadnm, 'PO Box') > 0
            OR instr(v_roadnm, 'Drive') > 0
            OR instr(v_roadnm, 'Hwy') > 0
            THEN -- THIS IS A VALID RECORD. Insert into TAB_EX1

                -- If Road Number is null and company name does not exist (i.e., a personal address) then its invalid
                IF v_roadno IS NULL and c1_rec.comp_name IS NULL AND INSTR(v_roadnm, 'PO Box') <= 0 THEN
                    dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm || '****Problem');
                    v_errmsg := 'Road Number not provided for personal address';
                    v_errmsg := f_logerr(c1_rec,v_errmsg);
                    goto loop_next;
                END IF;

                dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm);

                INSERT INTO tab_ex1
                (ID, NAME, COMP_NAME, ROADNUM, ROADNAME, POSTCODE)
                VALUES
                (c1_rec.ID, c1_rec.NAME, c1_rec.COMP_NAME, v_roadno, v_roadnm, v_postcode);

            ELSE
                dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm || '****Problem');
                v_errmsg := 'Invalid Road Name';
                v_errmsg := f_logerr(c1_rec,v_errmsg);
            END IF;
        ELSE
            dbms_output.put_line(v_roadno || chr(9) || chr(9) || '|' || chr(9) || v_roadnm || '****Problem');
            v_errmsg := 'Invalid address format';
            v_errmsg := f_logerr(c1_rec,v_errmsg);
        END IF;

        <<err_rec>>
        NULL;

        <<loop_next>>
        NULL;
    end loop;
end;
/

 

by: khushi2003Posted on 2004-04-08 at 01:45:54ID: 10781819

Hi is it  possible to put exceptional handler in above four procedure and functions?

Yha in one procedure p_pincode u have but i would like to have in others too

 

by: pratikroyPosted on 2004-04-08 at 02:06:59ID: 10781909

This post have become real big now. It takes time to scroll down as well. Since your original question has been answered, and a lot of new questions have also been answered, I suggest you accept the answer in this question, and open a new question if you want.

As for the exception handling, you also have noticed that the exception is handled in the procedure p_pincode. I am not sure which exceptions you expect and wish to trap in others. I had put in the exception handling in procedure because it was required there. In the functions, you can put in exception handling as per own requirements.

It should be a fairly simple thing to do.

Thanks !!  

 

by: khushi2003Posted on 2004-04-08 at 03:32:40ID: 10782256

OK

 

by: khushi2003Posted on 2004-04-08 at 04:54:06ID: 10782595

But the code u have given still does not separate field correclty

1. ur previous code used parse this address " 1  A Kalinda Street" as "1A", "kalainda Street" but now it does same at code used to do. It produce  "1" , "A Kalinda Street"
2.same case for "10/1a Frederick Street"
3.Why does it splits this "tom &  jerry Street",  as  "tom &", "jerry Street";
4."10a M'cac street" produce " 10a M' ", "cac Street"

like this example!

 

by: pratikroyPosted on 2004-04-08 at 05:58:15ID: 10783002

I had mentioned that "1 A Kalinda Street" would always be treated as "1" "A Kalinda Street". Refer to my post dated "Date: 03/31/2004 04:05PM PST". In all the code solutions that I have provided, you would find that "1 A Kalinda Street" is treated as "1" "A Kalinda Street". In case it would have been "1A Kalinda Street", it would have been treated as "1A" "Kalinda Street"

Cases for 10/1a Fredeick Street, Tom & Jerry Street, and 10a M'cac street should now be working in the latest code that I have provided .. dated "Date: 04/07/2004 02:48PM PDT"

There was a glitch in my last-to-latest solution, where I had deleted the lines that used to take care of Normal Addresses like "10/1a Kalinda Street". This problem has been resolved in my latest code that I had put in my post dated "Date: 04/07/2004 02:48PM PDT"





 

by: pratikroyPosted on 2004-04-08 at 14:58:10ID: 10787585

Hi Khushi, Do you still need any help on this ? Is everything working as per your expectations now ?

Waiting to hear !!

Happy Easter to you!!

 

by: khushi2003Posted on 2004-04-09 at 06:26:12ID: 10791025

But still i got this:
"upper & lower Street"  separates as "upper &" and "lower Street" should be one field in roadname col
"Hill Road & downhill Road" same in this case "Hill road &" and "downhill Road"
"Dance Pde & mahal Street" in this case too "Dance Ave &" and "mahal Street",
In above case what i am finding is where ever "&" is there is the address it separates into two as roadnum and roadname in tab_ex1. It should not separate it should insert whole data as one in roadname col.
Please check ur coding.

one more prob may be this is new prob like every time adding!
11 Up-town Road should be "11" and "Up-town Road"  but now i can get as "11 Up-" and "town Road".
As u have said its fixed but i can still get same outcome as ."10a M'cac street" produce " 10a M' ", "cac Street", may be becoz of "  '  " in M'cac.

Regards, Yha its all ur answer under my expectation but just asking some more.

As u have said this question too large to scroll and open so i am moving to another Q. Hope if u hv time i expect solution.

http://www.experts-exchange.com/Databases/Oracle/Q_20948849.html

Happy Easters to u too!
regards

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...