Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

table join problem

Status
Not open for further replies.

priyanthan

Programmer
Jul 27, 2008
70
CA
I need to join two tables, but the issue is there is no similar columns to join. what i mean is;

table 1
name type notes
---- ---- ------------
12345678-22556 3601 sample notes
123123123-22555 3601 sample notes
12345678123456-22556 3602 sample notes

table 2

siteid location
------- --------
12345678 Toronto
123123123 Toronto

i need to join table 1 with table 2 on name and siteid.
In table 1 the characters upto "-" is the siteid.

Can someone help me with this.

Thanks in advance.
 
Suggest table1 be properly defined. . .

With the current definition, you could also do this inside a program (read table1, build the siteid into a variable, read table2).

 
Use combo of substr and posstr to purge the siteid from the name in table 1. In order to compare you will need to make sure that you have same datatype between join columns, so use the INT function to make an integer of the partial string from name from table 1.

Ties Blom

 
Hi blom0344,

I'm new to this, so can you please give an example how to use the substr and posstr in SQL.

Thanks a lot.
 
priyanthan,
The POSSTR function returns the position of a character (or string) in another string. For instance:
SELECT POSSTR('ABC-DEF','-')
would return the value 4.

The SUBSTR function returns a portion of a string dependant on the parameters given in the SUBSTR function. For instance:
SELECT SUBSTR('ABC-DEF',2,2)
would return the value 'BC' (position 2 for 2 characters)

What Ties is alluding to is a WHERE clause that uses POSSTR on table 1, combined with SUBSTR in order to give you the start and end of the key, dropping off everything after the '-', and then linking that with table 2.

If you need further help, let us know.

Marc
Ps - I haven't given you the complete answer as you said that you were new to this, and I believe that you will find a good deal of satisfaction when you work this slightly unusual problem out yourself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top