PostgreSQL 9.3: Migrate text column to macaddr type - postgresql

I have a table with a text column representing a MAC address, but I want to use the macaddr type instead. So, I tried:
alter table mytable alter column mac_column type macaddr;
And got this error
ERROR: column "mac_column" cannot be cast automatically to type macaddr
HINT: Specify a USING expression to perform the conversion.
But I don't know what to use as USING expression:
alter table mytable alter column mac_column type macaddr using(????????)
What should I use as USING expression?
Many thanks in advance

You can’t simply change the data type because data is already there in the column. Since the data is of type String PostgreSQL can't expect it as macaddr though you entered valid String representation of the macaddr. So now, as PostgreSQL suggested you can use the ‘USING’ expression to cast your data into macaddr.
ALTER TABLE mytable ALTER COLUMN mac_column TYPE macaddr USING(mac_column::macaddr)

Related

Postgres Altering column type using user-defined function

I have a column which is stored as TEXT, I would like to change it to UUID in postgres. But I need to run some function on the column first to change it to UUID. Is it possible to do smth like that?
ALTER TABLE am.product ALTER COLUMN p_merchant_id TYPE UUID USING myFunc;
Where myFunc returns UUID with the input merchant_id?
Yes, that's possible.
You just need to pass the old value to your function:
ALTER TABLE am.product
ALTER COLUMN p_merchant_id TYPE UUID USING myfunc(p_merchant_id);

How do I convert string to time in postgresql?

I have a column of type varchar with a time on it like 120217. I would like to convert this to a time format I can query. Would something like
alter table public.table alter column col_time type date using to_date(col_time, 'HHMMSS');
work?
this should do:
alter table public.table
alter column col_time
type time using (col_time::time);

Change data type varchar to timestamp along with null values in PostgreSQL

Change data type varchar to timestamp along with null values in PostgreSQL
I have a column with empty rows and few with timestamp rows. How to convert that into timestamp data type in PostgreSQL?
You need a USING clause to turn the empty strings into null.
ALTER TABLE ...
ALTER COLUMN mycol
TYPE timestamp
USING (...conversion expression...)
Without seeing the input data it's hard to say exactly what that expression must be, but it probably involves nullif or case expressions and the to_timestamp function and/or a CAST to timestamp.

PSQL - Modify data type of column

ALTER TABLE user_login ALTER COLUMN dob date;
Is this correct query in psql to modify the data type of the column?
No, you need to include TYPE. It should be
ALTER TABLE user_login ALTER COLUMN dob TYPE date
See the documentation. You're probably confused with ADD COLUMN where you can omit the TYPE.

How to change column datatype from character to numeric in PostgreSQL 8.4

I am using following query:
ALTER TABLE presales ALTER COLUMN code TYPE numeric(10,0);
to change the datatype of a column from character(20) to numeric(10,0) but I am getting the error:
column "code" cannot be cast to type numeric
You can try using USING:
The optional USING clause specifies how to compute the new column value from the old; if omitted, the default conversion is the same as an assignment cast from old data type to new. A USING clause must be provided if there is no implicit or assignment cast from old to new type.
So this might work (depending on your data):
alter table presales alter column code type numeric(10,0) using code::numeric;
-- Or if you prefer standard casting...
alter table presales alter column code type numeric(10,0) using cast(code as numeric);
This will fail if you have anything in code that cannot be cast to numeric; if the USING fails, you'll have to clean up the non-numeric data by hand before changing the column type.
If your VARCHAR column contains empty strings (which are not the same as NULL for PostgreSQL as you might recall) you will have to use something in the line of the following to set a default:
ALTER TABLE presales ALTER COLUMN code TYPE NUMERIC(10,0)
USING COALESCE(NULLIF(code, '')::NUMERIC, 0);
(found with the help of this answer)
Step 1: Add new column with integer or numeric as per your requirement
Step 2: Populate data from varchar column to numeric column
Step 3: drop varchar column
Step 4: change new numeric column name as per old varchar column