HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

Select and Update MAC Addresses to Same Format

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sameformatupdateaddressesandselectmac

Problem

I have a mysql database with a macaddress column, this contains thousands of mac addresses in various formats; uppercase, lowercase and with colons. See below for an example;

+-------------------+
| macaddress        |
+-------------------+
| 90CCAADD3341      |
+-------------------+
| 90:3f:ff:11:22:33 |
+-------------------+
| 33:44:aa:bb:34:6a |
+-------------------+
| 8801abcd3231      |
+-------------------+


Is there any way I can bulk update all rows in this column so they are in the same format?

I'd like them all to be uppercase with no colons, see below;

+--------------+
| macaddress   |
+--------------+
| 90CCAADD3341 |
+--------------+
| 903FFF112233 |
+--------------+
| 3344AABB346A |
+--------------+
| 8801ABCD3231 |
+--------------+


How can I achieve this?

Solution

Test select:

select macaddress, upper(replace(macaddress,':','')) as new_macaddress
from Macs;


Update:

update macs
set macaddress = upper(replace(macaddress,':',''));


Fiddle

Test data:

create table macs
(
macaddress varchar(20)
);

insert into macs values('90CCAADD3341');

insert into macs values('90:3f:ff:11:22:33');

insert into macs values('33:44:aa:bb:34:6a');

insert into macs values('8801abcd3231');


Documentation links:

UPPER()

REPLACE()

Code Snippets

select macaddress, upper(replace(macaddress,':','')) as new_macaddress
from Macs;
update macs
set macaddress = upper(replace(macaddress,':',''));
create table macs
(
macaddress varchar(20)
);

insert into macs values('90CCAADD3341');

insert into macs values('90:3f:ff:11:22:33');

insert into macs values('33:44:aa:bb:34:6a');

insert into macs values('8801abcd3231');

Context

StackExchange Database Administrators Q#252289, answer score: 2

Revisions (0)

No revisions yet.