Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Variable field length join


Variable field length join

Author
Message
DerbyNeal
DerbyNeal
SSC-Enthusiastic
SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)

Group: General Forum Members
Points: 156 Visits: 909
Hi,

I have a join consisting of two tables (VCTMovements and VCTHaulageMileageLog).

SELECT * FROM VCTMovements vm JOIN VCTHaulageMileageLog vhml ON
vm.VCTCollectZipcodeId = vhml.ZipcodeFrom
AND vm.VCTDeliveryZipcodeId = vhml.ZipcodeTo
AND vm.DataAreaId = vhml.DataAreaId
WHERE vm.VCTCollectZipcodeId IS NOT NULL
AND vm.VCTDeliveryZipcodeId IS NOT NULL
AND vm.VCTCollectZipcodeId <> ''
AND vm.VCTDeliveryZipcodeId <> ''
AND vhml.MatrixType = 2



Ordinarily the above code would be fine. However, there are some records in the VCTHaulageMileageLog table where partial data was entred in the ZipcodeFrom and ZipcodeTo fields.
These fields are both NVARCHAR(10).

I could exeucte a lengthy statement to catch each permutation (i.e. where length of ZipcodeFrom = 1 and length of ZipcodeTo = 1), but is there a better approach than the following example?

SELECT * FROM VCTMovements vm JOIN VCTHaulageMileageLog vhml ON
(
SUBSTRING(vm.VCTCollectZipcodeId, 1, 1) = vhml.ZipcodeFrom
AND
SUBSTRING(vm.VCTDeliveryZipcodeId, 1, 1) = vhml.ZipcodeTo
)
OR
(
SUBSTRING(vm.VCTCollectZipcodeId, 1, 2) = vhml.ZipcodeFrom
AND
SUBSTRING(vm.VCTDeliveryZipcodeId, 1, 1) = vhml.ZipcodeTo
)
... etc
... etc
OR
(
SUBSTRING(vm.VCTCollectZipcodeId, 1, 10) = vhml.ZipcodeFrom
AND
SUBSTRING(vm.VCTDeliveryZipcodeId, 1, 10) = vhml.ZipcodeTo
)
WHERE vm.VCTCollectZipcodeId IS NOT NULL
AND vm.VCTDeliveryZipcodeId IS NOT NULL
AND vm.VCTCollectZipcodeId <> ''
AND vm.VCTDeliveryZipcodeId <> ''
AND vhml.MatrixType = 2



Any ideas, please?

Thanks in advance,

Neal
Ken McKelvey
Ken McKelvey
SSC Eights!
SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)

Group: General Forum Members
Points: 958 Visits: 7267

-- either (LEN ignores trailing spaces)
LEFT(vm.VCTCollectZipcodeId, LEN(vhml.ZipcodeFrom)) = vhml.ZipcodeFrom
-- or
vm.VCTCollectZipcodeId LIKE vhml.ZipcodeFrom + '%'


DerbyNeal
DerbyNeal
SSC-Enthusiastic
SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)

Group: General Forum Members
Points: 156 Visits: 909
Many thanks Ken
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search