I Implemented a small ERP system for our kitchen. The system is used to track our groceries, so it contains the goods (referred as food) with its name and UPC code. Over the time the names and upcs started to overlap and creating a data problem. In a first attempt we started to add the manufacturer into the name of the food leafing us with data which looks like this.
| Id | Foodname |
|---|---|
| 1 | Acidophilus;Joghurt |
| 2 | Aibler;Rapsöl |
| 3 | Allos;Choco Confiserie |
| 4 | Alnatura; Dinkel Butterkeks |
| 5 | Alnatura; Salatkerne Mix |
The names are German, sorry for that. The first part of the name is the manufacturer and the second part is the name of the food. I now introduced a new table called 'Manufacturer' which holds an Id and a Name. The table for the foods was extended by the ManufacturerId field. I want to have the following tables
Manufacturer
| Id | Name |
|---|---|
| 1 | Acidophilus |
| 2 | Aibler |
| 3 | Allos |
| 4 | Alnatura |
| 5 | Alnatura |
Food
| Id | Foodname | ManufacturerId |
|---|---|---|
| 1 | Joghurt | 1 |
| 2 | Rapsöl | 2 |
| 3 | Choco Confiserie | 3 |
| 4 | Dinkel Butterkeks | 4 |
| 5 | Salatkerne Mix | 5 |
I tried warping my head around a SQL statement which will solve this for me but I couldn't find any solution.
Requiremnts
- Not all entries in the food table already have a manufactuerer assigened. So the query has to filter for item which contain the a ';' in the name. Like
select Name from Foods
where Foods.Name like '%;%'
order by [Name]
- As seen in the sample data there could be whitespaces after the ';' so the entries for manufactuerer name and food name have to be trimmed.
- A manufacturer has to be created (INSERT) if it isn't already in the table
insert into Manufacturer(Name) values (left part of the string split) - The name of the food has to be updated to just the name, meaning remove the manufacturer and the ';'
- The ID of the manufactuerer has to be set on the food entry (ManufacturerId)
update foods set Name= left part of the string split set ManufacturerId=id of the inserted or found manufacturer
I tried with the build in functions STRING_SPLIT, LEFT(), RIGHT() but couldn't solve it. According to the docu string split also does not garantie that the result is always in the same order. I hope someone has an idea for me.
Thank you
SELECT @@VERSIONit is Microsoft SQL Azure (RTM) - 12.0.2000.8 Apr 19 2021 17:05:18 Copyright (C) 2019 Microsoft Corporation