convert text to time stamp in postgreSQL - postgresql

I have data extracted from a website as the image
[data]
I need to convert it as timestamp date and time
how can I do that

The text timestamps are already in a format which should be directly castable, e.g.
SELECT '2021-07-06T12:32:00'::timestamp;
Demo

Related

Convert timestamp to string from firebase

enter image description hereI accessed the timestamp from firestore and printed it as an string but it's not showing in date format! I'm not being able to print timestamp inside Text widget without converting it into string fromat either.
If you don't want to use Firestore function, than the easiest way to convert a timestamp to a JavaScript date or as you said string is like this:
new Date(timestamp.seconds * 1000).toLocaleDateString()
The timestamp is an object with seconds and nanoseconds, you create an date instance, pass the timestamp seconds and multiply with 1000. Now you can format this as you want.

How can we convert a given datetime format in PostgreSQL?

I have a date time column which is of the following format:
2019-11-10-07.10.55.865000
I want my format to be as follows:
2019-11-10 07:10:55.865000
How can I do this in PostgreSQL 9.6.11?
We can try making a full roundtrip from text to timestamp, then back to text again:
SELECT
TO_CHAR(TO_TIMESTAMP('2019-11-10-07.10.55.865000', 'YYYY-MM-DD-HH.MI.SS.US'),
'YYYY-MM-DD HH:MI:SS.US') AS ts_out;
This outputs:
2019-11-10 07:10:00.865000
Demo
As a side note, you should seriously consider not storing your timestamps as text in the first place. Ideally, if you want to view your timestamp column a certain way, e.g. for reporting purposes, you should only have to make a single call to TO_CHAR with the format mask you want to use.
There is the to_char(timestamp, text) function, e.g. to_char(current_timestamp, 'HH12:MI:SS')
https://www.postgresql.org/docs/current/functions-formatting.html

How to convert these character fields to time stamp in hive

I have these character fields actv_date and actv_time in a hive table
ap_actv_date
171205 (YYMMDD)
ap_actv_time
1954359 (HHMMSSF)
I want the output to be a time stamp field of format YYYY-MM-DD HH:MM:SS.f
Use this.
from_unixtime(unix_timestamp(concat(ap_actv_date,ap_actv_time), 'YYMMDDHHMMSSF'))

Date Format Conversion in Hive

I'm very new to sql/hive. At first, I loaded a txt file into hive using:
drop table if exists Tran_data;
create table Tran_data(tran_time string,
resort string, settled double)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n';
Load data local inpath 'C:\Users\me\Documents\transaction_data.txt' into table Tran_Data;
The variable tran_time in the txt file is like this:10-APR-2014 15:01. After loading this Tran_data table, I tried to convert tran_time to a "standard" format so that I can join this table to another table using tran_time as the join key. The date format desired is 'yyyymmdd'. I searched online resources, and found this: unix_timestamp(substr(tran_time,1,11),'dd-MMM-yyyy')
So essentially, I'm doing this: unix_timestamp('10-APR-2014','dd-MMM-yyyy'). However, the output is "NULL".
So my question is: how to convert the date format to a "standard" format, and then further convert it to 'yyyymmdd' format?
from_unixtime(unix_timestamp('20150101' ,'yyyyMMdd'), 'yyyy-MM-dd')
My current Hive Version: Hive 0.12.0-cdh5.1.5
I converted datetime in first column to date in second column using the below hive date functions. Hope this helps!
select inp_dt, from_unixtime(unix_timestamp(substr(inp_dt,0,11),'dd-MMM-yyyy')) as todateformat from table;
inp_dt todateformat
12-Mar-2015 07:24:55 2015-03-12 00:00:00
unix_timestamp function will convert given string date format to unix timestamp in seconds , but not like this format dd-mm-yyyy.
You need to write your own custom udf to convert a given string date to the format that you need as present Hive do not have any predefined functions. We have to_date function to convert a timestamp to date , remaining all unix_timestamp functions won't help your problem.
select from_unixtime(unix_timestamp('01032018' ,'MMddyyyy'), 'yyyyMMdd');
input format: mmddyyyy
01032018
output after query: yyyymmdd
20180103
To help someone in the future:
The following function should work as it worked in my case
to_date(from_unixtime(UNIX_TIMESTAMP('10-APR-2014','dd-MMM-yyyy'))
unix_timestamp('2014-05-01','dd-mmm-yyyy') will work, your input string should be in this format for hive yyyy-mm-dd or yyyy-mm-dd hh:mm:ss
Where as you are trying with '01-MAY-2014' hive won't understand it as a date string

Format of date / time values in database tables

I am reading a csv file with date fields of formatted mm/dd/yyyy. I expected the same kind of format from a Postgres table after the import, but I see yyyy-mm-dd hh:mm:ss.
The date fields in my table are defined as timestamp without time zone data type.
How do I maintain the same format of data? I am using PostgreSQL 9.3.
Postgresql only stores the value, it doesn't store formatting (which would waste space).
You can use the to_char function in your query if you like to get the output formatted in a special way. Details are in the manual.