I have a table with the following example data:
ID | Name | lat | long |
---|---|---|---|
1 | 100 | 123 | -123 |
2 | 101 | 234 | -234 |
4 | 100-L | NULL | NULL |
5 | 100-R | NULL | NULL |
6 | 101-L | NULL | NULL |
7 | 101-R | NULL | NULL |
What I need to do is update the lat and long fields for IDs 4-7 to match the lat/long of the ones without the L/R at the end. So basically XX100-L will have the same lat/long as XX100, YY101-L will have the same as YY100 and so on, the desired output would be:
ID | Name | lat | long |
---|---|---|---|
1 | XX100 | 123 | -123 |
2 | YY101 | 234 | -234 |
4 | XX100-L | 123 | -123 |
5 | XX100-R | 123 | -123 |
6 | YY101-L | 234 | -234 |
7 | YY101-R | 234 | -234 |
Any advice on how to achieve this would be greatly appreciated!
2
Answers
Inside your
UPDATE
statement, you can apply a self join, that matches on the partial common part of the "Name" value, obtainable withSUBSTRING_INDEX
.Output:
Check the demo here.
you can do it by left joining with a list of names that have lat or long not null :
The update statement can be :
Demo here