How do I convert string to time in postgresql? - 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);

Related

PostgreSQL. How to convert varchar format dates in an existing column to date format dates

I'm quite new to PostgreSQL. I've looked at other existing threads, the documentation and various google searches but still can't fully understand if it is possible to convert existing date values that are actually strings in a varchar column into an actual date format.
The column is named datelaid and has a varchar datatype. The table is named init_sa_mains_1
datelaid:
" "
"01-12-2011"
"01-12-2011"
" "
"01-12-2011"
" "
I have tried to change the data type of the column as follows, with no success:
ALTER TABLE init_sa_mains_1
ALTER COLUMN datelaid TYPE date USING datelaid::date;
ALTER TABLE init_sa_mains_1
CAST(init_sa_mains_1.datelaid as date);
ALTER TABLE init_sa_mains_1
ALTER COLUMN datelaid USING datelaid::date;
ALTER TABLE init_sa_mains_1
ALTER COLUMN datelaid TYPE date;
USING to_date(datelaid, 'DD-MM-YYYY');
Could anyone please advise on a possible solution?
You will need to deal with empty strings as they can't be converted like that. But you can turn them into NULL values:
ALTER TABLE init_sa_mains_1
ALTER COLUMN datelaid TYPE date;
USING to_date(nullif(trim(datelaid),''), 'DD-MM-YYYY');

Syntax for adding a timestamp column in Postgres

After research on here I wanted to use "timestamp with time zone" but cannot figure out the proper syntax based on the postgres docs.
ALTER TABLE microwaves ADD COLUMN scanned_in DATA_TYPE timestamp with time zone;
ALTER TABLE microwaves ADD COLUMN scanned_in TYPE timestamp with time zone;
Both throw errors.
Any help appreciated, thanks.
You just had the syntax wrong. You don't need the [DATA] TYPE part here (that's only needed when you want to change the type) :
CREATE TABLE barf
( id serial PRIMARY KEY);
ALTER TABLE barf ADD COLUMN scanned_in timestamp with time zone;
BTW (just a hint): most of the ALTER syntax just mimics the syntax for CREATE TABLE (...): the sub-syntax is mostly the same.
Follow the simple solution as below:
ALTER TABLE microwaves ADD COLUMN scanned_in timestamp with time zone;
For more details check PostgreSQL - ADD COLUMN

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.

PostgreSQL 9.3: Migrate text column to macaddr type

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)

Alter a Column Data Type Postgres

Hey I have just started working on PostgreSQL, and I am wondering how can we change a column's data type, I tried the following command:
alter table tableName alter column columnName type timestamp with time zone;
However I got the following message:
column "columnName" cannot be cast to type timestamp with time zone
The current column's data type is int, and i would like to change it to timestamp
Postgres doesn't know how to translate int to timestamp. There are several cases and usually they have different starting date.
Create temporary column with timestamp
Update table and copy data from old column to temporary column using your own translation
Drop old column
Rename temporary column.
If you look into documentation, you will find one line syntax with example how to convert unix time integer type:
ALTER [ COLUMN ] column [ SET DATA ] TYPE type [ USING expression ]
Postgres does't allow int type column to change directly into timezone. To achive this, you have to first change column type to varchar and then change it to timezone.
alter table tableName alter column columnName type varchar(64);
alter table tableName alter column columnName type timestamp with time zone;
There is a better way to do this, with the USING clause. Like so:
ALTER TABLE tableName
ALTER columnName type TIMESTAMP WITH TIME ZONE
USING to_timestamp(columnName) AT TIME ZONE 'America/New_York';
I achieved it from timestamp to timestamp with time zone by:
ALTER TABLE tableName ALTER COLUMN columnName SET DATA TYPE timestamp with time zone;
but if it is from timestamp to int or bigint you may need to do this:
ALTER TABLE tableName ALTER COLUMN columnName SET DATA TYPE int8 USING columnName::bigint