Postgres split timestamp column to time column and date column - postgresql

Hi as the title says I have a column named healthtime in my table which is of type timestamp without timezone i.e.
Healthime
2012-02-02 08:15:00
I would like to split this into two new columns in the same table, one date column and one time column i.e.
Date Time
2012-02-02 08:15:00
Please can someone advise me how to do this (preferably in a single query),
Thanks,
James

Not one query, but one transaction... should be fine as well:
My table looks like:
CREATE TABLE d (t timestamp);
Code for changing the table:
BEGIN;
ALTER TABLE d ADD COLUMN dat date;
ALTER TABLE d ADD COLUMN tim time;
UPDATE d
SET
dat = t::date,
tim = t::time;
ALTER TABLE d DROP COLUMN t;
COMMIT;

SELECT Healthime::date from tbl_yourtable as date
SELECT Healthime::time(0) from tbl_yourtable as time
PostgreSQL Version: >= 10

select fake_table.healthtime
, cast(to_char(fake_table.healthtime, 'YYYY-MM-DD') as date) as "Date"
, cast(to_char(fake_table.healthtime, 'HH24:MI:SS') as time) as "Time"
from (select current_timestamp as "healthtime") as "fake_table"
output:
healthtime Date Time
timestamp with time zone date time without time zone
========================== ========== ======================
2022-04-22 14:20:25.678-04 2022-04-22 14:20:25
tested on PostgreSQL 9.3.25, compiled by Visual C++ build 1600, 64-bit running on Windows 8.1

Related

How to extract Year and Month in SQL Postgres by using Data_Part function

I am facing an issue extracting the month and year from the data (in Character varying type) >> InvoiceDate in SQL Postgres. I have seen the solution is relatively easy with MySQL function: DATEFROMPARTS as per the below Code which is not available in SQLpostgres. How can I get the same result DATA_PART function in Postgres SQL, but simultaneously I need to change the data type of the column "InvoiceDate" to the date
Select
CustomerID,
min(InvoiceDate) first_purchase_date,
DATEFROMPARTS(year(min(InvoiceDate)), month(min(InvoiceDate)), 1) Cohort_Date
into #cohort
from #online_retail_main
group by CustomerID
The output:
Customer ID| first_purchase_date |Cohort_Date|
-----------+-------------------------+-----------+
12345 | 2010-12-20 15:47:00:00 | 2010-12-01|
I am trying to make a date consits of Year and Month , while the day to be set as 1 for all
Assuming a valid Postgres timestamp:
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
12/01/2010
--or ISO format
set datestyle = 'ISO,MDY';
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
2010-12-01
Uses date_trunc to truncate the timestamp to a month which means the first of the month. Then cast(::date) to a date type. The DateStyle just deals with how the value is presented to the user. The value is not stored formatted.
To do something similar to what you did in MySQL:
select make_date(extract(year from '2010-12-20 15:47:00.00'::timestamp)::integer, extract(month from '2010-12-20 15:47:00.00'::timestamp)::integer, 1);
This uses make_date from here Date/time functions and extract to build a date.

Postgresql Newbie - Looking for insight

I am in an introduction to sql class (using postgresql) and struggling to take simple queries to the next step. I have a single table with two datetime columns (start_time & end_time) that I want to extract as two date only columns. I figured out how to extract just the date from datetime using the following:
Select start_time,
CAST(start_time as date) as Start_Date
from [table];
or
Select end_time,
CAST(end_time as date) as End_Date
from [table];
Problem: I can't figure out the next step to combine both of these queries into a single step. I tried using WHERE but i am still doing something wrong.
1st wrong example
SELECT start_time, end_time
From baywheels_2017
WHERE
CAST(start_time AS DATE) AS Start_Date
AND (CAST(end_time AS DATE) AS End_Date);
Any help is greatly appreciated. Thanks for taking the time to look.
You don't need to select the underlying field in order to later cast it; each field in the "select" clause is relatively independent. With the table created by:
CREATE TABLE test (
id SERIAL PRIMARY KEY,
start_time TIMESTAMP WITH TIME ZONE NOT NULL,
end_time TIMESTAMP WITH TIME ZONE NOT NULL
);
INSERT INTO test(start_time, end_time)
VALUES ('2022-10-31T12:30:00Z', '2022-12-31T23:59:59Z');
You could run the select:
SELECT
cast(start_time as date) as start_date,
cast(end_time as date) as end_date
FROM test;
(You can try this out on a website like DB-Fiddle.)

How to convert varchar to timestamp in postgreSQL?

I have data as following, But the column type is Varchar:
2019-09-28T23:59:59.52Z
I assume 52 here is milli seconds, If so..
I would like to convert it as following and change the column type to timestamp:
2019-09-28 23:59:59.52
Can someone let me know how I can convert in postgreSQL?
EDIT:
I can see data in table as (since the column type is varchar):
2019-09-28T23:59:59.52Z
Instead, I want data in the table to be shown as:
2019-09-28 23:59:59 ( and may be .52, if possible)
I need to change the column type to timestamp as well, I guess, Please help with that too.
Answer:
Tim has provided a solution, You can follow that.
But, In my case, It is prod env, So, I have just changed the type using:
ALTER TABLE my_table ALTER COLUMN my_column TYPE TIMESTAMP USING my_column::timestamp without time zone;
Thanks
Your timestamp string literal is already in a format which can be directly cast in Postgres:
SELECT '2019-09-28T23:59:59.52Z'::timestamp; -- 2019-09-28 23:59:59.52
As a test, let's add one day to make sure it's working:
SELECT '2019-09-28T23:59:59.52Z'::timestamp + interval '1 day';
-- 2019-09-29 23:59:59.52
If you want to actually add a new timestamp column using string data from another column, then try:
ALTER TABLE yourTable ADD COLUMN new_ts TIMESTAMP;
UPDATE yourTable SET new_ts = old_ts::timestamp;
ALTER TABLE yourTable DROP COLUMN old_ts;
ALTER TABLE yourTable RENAME COLUMN new_ts TO old_ts; -- optional
The last ALTER statement is optional if you want the new bona fide timestamp column to bear the same name as the old text timestamp column.

changing character into timestamp

I have two columns (date character varying, time character varying) in table VM. Now I want to merge these two columns and change the type as a timestamp. can anyone help me out to do this?
Or else
I want to separate data based on the year (2017, 2018). I used substring(date, '2018') command but it replies with 2017 data showing NULL instead of 2017 along with 2018 data.
You didn't tell us what exactly the contents of that table is, but something like this should work:
Add a new timestamp column (and please find a better name than "timestamp" or ts_column for it)
alter table the_table add ts_column timestamp;
Then you need to update the data
update the_table
set ts_column = to_timestamp(concat("date", ' ', "time"), 'yyyy-mm-dd hh24:mi:ss')
where "date" is not null;
The above assumes that the date stored in the column named "date" and the time stored in the column named "time" are formatted as ISO date and time values. If they are not, you need to adjust the parameter to the to_timestamp() function. For details on the format mask, please see the manual
Then drop the old columns:
alter table the_table drop "date", drop "time";
To extract the year from a date or timestamp column, use the extract function:
where extract(year from ts_column) = 2018
Note that the above will not be able to use an index on ts_column to speed up the query.
An alternative way of writing is:
where ts_column >= date '2018-01-01'
and ts_column < date '2019-01-01';
which would be able to use an index on ts_column

how to insert a time in oracle 10g database

I want to insert date and time in oracle database, I have created the table with columns
create table myadmin
( employe_id number(5),
supervisor Varchar2(20),
department Varchar2(20),
action Varchar2(20),
sdate date,
stime date)
While inserting the values below it gives an error. Please tell me how to insert the time ?
insert into myadmin
( employe_id,supervisor,department,action,sdate,stime) values
(83,'gaurav','helpdesk','pick','23-jan-2013','09:43:00');
You have to use keyword to_date for date insert in oracle like this.
to_date('23-01-2013','dd-mm-yyyy')
Basically you have to use keyword to_date('your date','your date format').
You can also add date and time together if you want and it would be something like this
to_date('23-01-2013 09:43:00','dd-mm-yyyy hh24:mi:ss')
A date in Oracle always has a date part and a time part. Having date and time in two separate columns only makes sense, if it can occur that date is null and time is not. (And still, you could set date to an improbable value like 1.1.0001 then.)
However, if you want to stick to those two separate fields, then make your string a datetime with the to_date function specifying the format used:
insert into myadmin
( employe_id,supervisor,department,action,sdate,stime) values
(83,'gaurav','helpdesk','pick',to_date('23-01-2013','dd-mm-yyyy'), to_date('09:43:00', 'hh24:mi:ss'));