I want to create a function/tsql to get strings from the 1st table/data and compare and convert to the value of a second table if the strings match what is on table 2.
Business example.
When a cashier mistakenly encoded ABC1231 as company name instead of ABC the report will have an issue showing ABC1231 instead of ABC. We need to correct this by matching it against a master list of company names in another table.
The query logic Im trying to apply here is to match how many similar strings are in table 1 data to master list data in table 2. Then if the process saw that the strings from table 1 has a match or has the highest similar string it will output what data in table 2. This should be also case sensitive and allows int and special character. See example below.
Example:
Table 1 contains ->
A121B312C33,AB12312C,AB-C,ABC223,AsBCD21,D23EF,D2E2F2,DEF
Table 2 contains ->
ABC,DEF
Output should be:
ABC,ABC,ABC,ABC,ABC,DEF,DEF,DEF
It is not only capital letters. It should match what is on the 2nd table whether lower case or upper case with special characters or with integer as well.
Best Answer
Byron,
Following our small conversation in the comments, I would recommend making a table structure change instead of trying to make a code change to accommodate this.
I believe this is the solution for you because it will:
Macros
orQueries
to retrieve the data you are actually looking forThis will however require a couple of steps of initial effort from you:
1. Make Table Changes
One thing Relational Databases are really good at is creating was I have heard call either a
Master List
or anList Of Values (LOV)
table
. The benefit is this forces your data and your application to only store certain allowed value for data integrity purposes. It then also allows you to make a change in one place and have it update everything.Let's assume your two tables look something like this:
If we rebuild the tables to be something like this we get to create a
Foreign Key
relationship which will ensure that the data stays consistent over time. (Table_1.CompanyID
must be a value found inTable_2.CompanyID
)2. Make application changes to support both the new table structures and probably a slight change to data entry processes
When you wanted to create the original output of
Table_1
you would run a query like below. This kind of change would need to be made in anything that tries to retrieve this data (reports, applications, automated processes, etc...)When it comes to
Insert
andUpdate
processes, you will first need to know that whatever theCompanyName
value is you are trying to store. If that value does not exist already, than you have to create that value inTable_2
before writing your record toTable_1
. For the sake of simplicity lets assume that the valueABC
already exists andTable_1.ID
is anIdentity
/Auto-Incriminating
column (IE: I don't need to create a value for it on theInsert
). A simpleInsert
andUpdate
script would look something like:Your application will probably need to have some sort of separate screen that allows certain users to create new
Table_2
records when appropriate. Otherwise you may have a similar problem down the road with just a different implementation. You can do a check and when neededinsert
intoTable_2
when trying to runinsert
orupdate
operation forTable_1
. I can provide some examples for those if you would like but I would recommend against it.3. Data Cleanup Project
Depending on the amount of data in your database, this may be the biggest effort. You will need to take the data you have right now and transpose it into this new table structure.
Then correct the records in
Table_2
so that only the correct values you are looking for exist. You could do this directly in the originalTable_1
before running the above script or make changes onTable_2
directly after everything is said and done. There are a couple of different ways to make it happen and I can provide some guidance if needed.When This is All Said and Done
You should be in a position where your application self-regulates much better and your reporting process is as simple as a single
SELECT
with a simpleJOIN
and no need for any kind of conversion in eitherSQL
orExcel
.Hopefully this helps. Let me know if this solution won't work for some reason and we can see about creating a different solution.