| Author |
Topic |
|
learzbu
Starting Member
27 Posts |
Posted - 2011-11-12 : 08:38:56
|
| I wanted to know if it would be possible to join two tables based on the following:Table 1 Column:Order_no040221Table 2 Column:Comment040221/Customer 1the column in table 1 is a varchar and table 2 is a string, is it possible to truncate the end of the column in table 2 just leaving the digits which i could then convert in to varchar to join on the first table? |
|
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2011-11-12 : 09:07:35
|
It is possible but there is no need if all you want is to join those tables. Just try something like this:Table1 AS T1INNER JOINTable2 AS T2ON T2.Comment LIKE T1.Order_no + '/%' For us, there is only the trying. The rest is not our business. ~T.S. EliotMuhammad Al Pasha |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-12 : 11:52:15
|
| In using it like above it will also use an index if present on Order_no column whereas if you apply functions over field for splitting number part the index will be ignored------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
learzbu
Starting Member
27 Posts |
Posted - 2011-11-13 : 08:06:30
|
| Thanks Alot! |
 |
|
|
learzbu
Starting Member
27 Posts |
Posted - 2011-11-13 : 11:00:43
|
| select scheme.opheadm.order_no, scheme.porecpm.order_no, delivery_no, invoice_no, customer, qty_received from scheme.opheadm join scheme.porecpm on (scheme.porecpm.commnt like (scheme.opheadm.order_no + '/%')) where effective_date between '2011-10-01 00:00:00.000' and '2011-10-08 00:00:00.000' I tried this but I get blank set of results.... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-13 : 11:49:01
|
quote: Originally posted by learzbu select scheme.opheadm.order_no, scheme.porecpm.order_no, delivery_no, invoice_no, customer, qty_received from scheme.opheadm join scheme.porecpm on (scheme.porecpm.commnt like (scheme.opheadm.order_no + '/%')) where effective_date between '2011-10-01 00:00:00.000' and '2011-10-08 00:00:00.000' I tried this but I get blank set of results....
first check if you've any data for given period in the tablesselect count(*) from scheme.opheadm where effective_date between '2011-10-01 00:00:00.000' and '2011-10-08 00:00:00.000' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
learzbu
Starting Member
27 Posts |
Posted - 2011-11-13 : 12:16:09
|
| Ya there's data for that period, I have 4 rows. But the join won't find them, A left join will give me the 4 rows but null data for the results from scheme.porecpm |
 |
|
|
learzbu
Starting Member
27 Posts |
Posted - 2011-11-13 : 12:19:12
|
| order_no order_no delivery_no invoice_no customer qty_received004218 NULL NULL OP/I010359 8051 NULL004213 NULL NULL OP/I010358 8050 NULL004220 NULL NULL OP/I010361 8024 NULL004221 NULL NULL OP/I010362 8038 NULL |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-13 : 12:20:17
|
quote: Originally posted by learzbu order_no order_no delivery_no invoice_no customer qty_received004218 NULL NULL OP/I010359 8051 NULL004213 NULL NULL OP/I010358 8050 NULL004220 NULL NULL OP/I010361 8024 NULL004221 NULL NULL OP/I010362 8038 NULL
and how are corresponding records in other table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
learzbu
Starting Member
27 Posts |
Posted - 2011-11-13 : 12:26:03
|
| order_no commnt delivery_no qty_received070218 004218/ACCESS RENEWA 730649 18This is the corresponding table, it's the comment that i'm trying to match to the first table based on the digits in the comment |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-13 : 12:30:37
|
is this any better?select scheme.opheadm.order_no, scheme.porecpm.order_no, delivery_no, invoice_no,customer, qty_receivedfrom scheme.opheadm join scheme.porecpm on (ltrim(rtrim(scheme.porecpm.commnt)) like (ltrim(rtrim(scheme.opheadm.order_no)) + '/%'))whereeffective_date between '2011-10-01 00:00:00.000' and '2011-10-08 00:00:00.000' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
learzbu
Starting Member
27 Posts |
Posted - 2011-11-13 : 12:36:16
|
| Yes!! thanks!!! that's gotten it. Was it reading blank spaces? Was that the issue? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-13 : 12:37:46
|
| yep..it had some hidden space------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|