Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres CAST macaddr

Lets say I have two tables in Postgres:

Name: table_rad
Column    Type   
id        integer
username  character varying(64)

Name: table_mac
Column    Type
id        integer
mac       macaddr

I want to do to a join:

SELECT * FROM table_rad WHERE username = mac;

Postgres will complain:

ERROR: operator does no exist: character varying = macaddr
LINE 1: ...ELECT * from table_rad WHERE username = mac;
                                                 ^    
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

Sofar I have googled for a solution and I know I have to CAST. But how can I cast type macaddr as varhcar?

like image 390
wilcox Avatar asked Jul 22 '26 17:07

wilcox


2 Answers

Formal style: CAST(mac AS varchar)

PostgreSQL-style: mac::varchar

e.g.:

SELECT * FROM table_rad JOIN table_mac ON username = CAST(mac AS varchar)
like image 53
araqnid Avatar answered Jul 25 '26 08:07

araqnid


You can do it by ::text like below:

SELECT * FROM table_rad WHERE username = mac::text;
like image 41
Ali MasudianPour Avatar answered Jul 25 '26 10:07

Ali MasudianPour