Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what data-type to use to store MAC addresses in an SQL Server database table?

Tags:

sql

sql-server

I want to store MAC addresses in one of my database tables, what data-type should I use? Reading articles on google, I have seen Binary(8) mentioned a few times. Is this the correct way?

Also, this does not make sense to me, as MAC addresses are six groups of two hexadecimal digits, wouldn't you use Binary(6)?

like image 930
Mausimo Avatar asked Dec 26 '12 20:12

Mausimo


3 Answers

I wouldn't use Binary at all.

I would use CHAR(12).

Though this really depends on what you use the data for - if this is for display only, you can simply use the textual representation.

like image 199
Oded Avatar answered Oct 08 '22 01:10

Oded


For easier performaing binary operations you can store them into Binary(6) You can use the following built in function to view the Hex readable value of the binary data:

select top 10 master.sys.fn_varbintohexstr(mac) from macaddresses

and to convert the hexadecimal text into binary:

select CONVERT(binary(6), 'AABBCCDDEEFF', 2);
like image 43
Atheer Mostafa Avatar answered Oct 08 '22 02:10

Atheer Mostafa


MAC address is a sequence of 6 hexadecimal numbers. That's why it would be efficient to store it as a number. Use BIGINT.

like image 28
Daniels Šatcs Avatar answered Oct 08 '22 02:10

Daniels Šatcs