I imported 2 spreadsheets to SQL Server tables. One sheet has data containing persons name & surname, the project they are working on as well as there banking details. The other spreadsheet has only the names & surnames of persons and banking details.
Sheet 2 data needs to be grouped to the right projects like in sheet 1. The only connection I found is that the Reference
column in Sheet 2 has numbers that match the ParentID
on sheet 1 but with a suffix appended.
Beneficiaries with the same ParentID
belong to the same Project. I need a way to extract the numeric values of Reference
and match them to the corresponding ParentID
in order to group the Beneficiaries in the right project.
sheet 1 :
BeneficiaryID float Checked
BeneficiaryName nvarchar(255) Checked
BeneficiarySurname nvarchar(255) Checked
IDNumber float Checked
DOB nvarchar(255) Checked
BranchnameID nvarchar(255) Checked
BranchCode float Checked
AccountType nvarchar(255) Checked
AccountNumber float Checked
TotalWages float Checked
[(No column name)] float Checked
ParentId float Checked
PPRDate datetime Checked
sheet 2 :
Reference nvarchar(255) Checked
[Account No] nvarchar(255) Checked
[Branch No] nvarchar(255) Checked
[Bank Name] nvarchar(255) Checked
[Acc Type] nvarchar(255) Checked
Amount nvarchar(255) Checked
[Action Date] nvarchar(255) Checked
Status nvarchar(255) Checked
Here is some sample
sheet 1
BeneficiaryID BeneficiaryName BeneficiarySurname IDNumber DOB Branchname ID BranchCode AccountNumber TotalWages ParentId PPRDate Caption
1202 Chris Manda 123456 4/6/1995 1 ABC 123 120 10125 June 2018 Agriculture
sheet 2
Reference Account_No Branch_No Bank_Name Amount Action_Date Status
10125_Motsomane 1234 151258865 CDF 1200 15/01/2016 ERROR
Best Answer
You need to match
float
value10125
(columnParentID
) onSheet1
withNVarchar(255)
value10125_Motsomane
(columnReference
) fromSheet2
.If data types would match you would simply do a direct
INNER JOIN
:But to extract the number before the underscore you can use
CHARINDEX
to find the position of the underscore and thenLEFT
to get the value up to that position:Result:
Applying this on the join (will do an implicit conversion from
NVARCHAR
toFLOAT
):Keep in mind that the
LEFT
function will fail if there is no underscore on a value forReference
, since it will return-1
as position.