I created a database for addresses, however, I made a mistake of concatenating the unit number with the road address which happened to include the unit number as well. So now, the unit number is repeated in some rows.
+----------------------------+---------------+
| physical_address | city |
+----------------------------+---------------+
| 1 AGATE COURT | New York City |
| 1 ANGELAS PLACE, 1A, 1A | New York City |
| 1 ARLINGTON COURT | New York City |
| 1 AVENUE J | New York City |
| 1 BAY CLUB DRIVE, 10M, 10M | New York City |
| 1 BAY CLUB DRIVE, 11B, 11B | New York City |
| 1 BAY CLUB DRIVE, 11V, 11V | New York City |
| 1 BAY CLUB DRIVE, 12H, 12H | New York City |
| 1 BAY CLUB DRIVE, 14S, 14S | New York City |
| 1 BAY CLUB DRIVE, 15B, 15B | New York City |
+----------------------------+---------------+
So if you look in the table above in the last 6 records, in the physical_address
column, the unit numbers like 10M
, 11B
, 11V
etc are repeated.
Is there any query I can run to remove everything after the last ,
in each row? The type is varchar
for the column if that helps. Also, keeping in mind that some addresses don't have any ,
in them.
EDIT what I have tried:
UPDATE sales
SET MyAddress = LEFT(MyAddress, CHARINDEX(',', MyAddress) - 1)
WHERE CHARINDEX(',', MyAddress) > 0
This unfortunately removes everything after the first comma, not the last.
This is on a database named Dolt