How can I break this string for licence plates v/s states?
I have unique VIN Numbers in my database for every vehicle.
VIN varchar2(100)
Make varchar2(100)
Model varchar2(100)
Year varchar2(4)
PlateInfo varchar2(1000)
The objective is to take the string in the PlateInfo field and split it to states and the license plates.
There are vehicles who have had more than 24 owner / plate changes.
The string in plate info typically looks like this.
MA~CT~DE~NJ~NJ~~~~~~VEG-1825~AX7547~117824~NEG-1012~BEG-1011~~
This needs to split into two columns StateId, PlateId
MA:VEG-1825
CT:AX7547
DE:117824
NJ:NEG-1012
NJ:BEG-1011
I was able to do this making an assumption that the number of occurrences for the tilde “~” character will always be an even number.
However when I ran this against the database, I found there are several vehicles where the information looks like this.
CT~DC~DE~MA~MD~NY~RI~VA~WA~WV~
My client wants me to put this in a state column with a null column for the plate. How can I achieve this? Would it be fair to make the assumption that each 2 character is a state, then validate it against the 50 states?
Assuming that your input string is a list of 10+ items where each item is terminated by a
~, e.g.you can remove the last
~and split the string by~:The states seem to be the first 10 non-empty elements (see @Reed Copsey‘s answer):
The plates seem to be the second 10 elements, which need to be padded with nulls if necessary:
Then zip the states and the plates as follows:
Example 1:
Example 2: