I have a table
TableName: MACAddresses
Columns:
- Computer
- MACAddress
I would like to create a sql script that creates a new column and correctly formats the mac address with the colon (ie with Substring) – To create a new column called CorrectMAC
How would I do this with Oracle?
Here is some test data:
Adding the new column is quite straightforward:
Note that you cannot make it NOT NULL at this point, because you already have some records in the table. So if you want to apply such a constraint, you need to populate it first.
This is the simplest way of updating the new column.
Now, if you had a more complicated pattern, or if you wanted to perform this operation on a regukar basis I suggest you expend the effort to turn it into a function, and perhaps remove that repetition at the same time.
Finally, if you wanted to enforce a mandatory constraint you can: