Apache Hive: converting timestamp from string to timestamp and save it table - sql

I have a table like this in hbase:
tableExaple (timestamp, timestamp_string, someOtherStuff)
timestamp has the datatype timestamp
timestamp_string hast the datatype string and has the pattern 'yyyy-MM-dd HH:mm:ss.SSS'
Now I would like to read the value from timestamp_string convert it with the hive-UDF unix_timestamp(string date, string pattern) to a timestamp and save this in the same table to the value timestamp.
How can I do this?
Stuff like
INSERT INTO tableExaple (timestamp) SELECT unix_timestamp(timestamp_string, 'yyyy-MM-dd HH:mm:ss.SSS') FROM tableExaple;
does not work.

I don't think unix_timestamp likes the millisecond format. Since timestamp_string is of type string, you can just split on the '.' and grab the date and time. So if you had 2014-11-11 08:09:10.123, split(timestamp_string, '\\.') will give you [2014-11-11 08:09:10, 123]. You can now reference your array by [0] and [1].
Example:
SELECT otherStuff
, timestamp
, unix_timestamp(split(timestamp_string, '\\.')[0], 'yyyy-MM-dd HH:mm:ss') new_time
FROM some_table
If you want to include the milliseconds in your new_time column, just grab the [1]th index from the split array (and divide by 1000).

Related

Postgres Timestamp to DATE dd-mm-yyyy

im trying to insert select a casted timestamp in a date colum timestamp like 02-09-2021 00:00:00, and i need to convert this timestamp to date dd-mm-yyyy without hhmmss, i've tried select date(column) as newdate but when i check my table insert, it keeps like timestamp, all the solutions that i tried only runs perfectly only in a select sentence, but when i try to insert select.. i keep with timestamp type..
If you need to convert formatted text to timestamp then use to_timestamp with explicit format specification. Try this:
select to_timestamp('02-09-2021 00:00:00', 'dd-mm-yyyy hh24:mi:ss');
to_timestamp
2021-02-09 00:00:00.000 +0200
To convert it to date:
select to_date('02-09-2021 00:00:00', 'dd-mm-yyyy');
If you need to change the type of an existing timestamp column to date then:
alter table the_table alter column the_column type date using the_column::date;

Is possible cast as Date with format 'yyyy-MM-dd HH:mm:ss.S'?

I have one table with two columns, id (string) and myTs (bigint).
I want to create a View with this two columns, but myTs must be date type:
CREATE myView AS SELECT id, myToDate(myTs) FROM myTable;
myTs is a timestamp, I want to transform it into a date with timezone Europe/Madrid and format yyyy-MM-dd HH:mm:ss.S.
For example, with myTs = 1167529028000, if I execute:
from_utc_timestamp(myTs, 'Europe/Madrid')
--Result: '2006-12-31 02:37:08.0'
I get the correct result but the type is timestamp, If I try cast it to date I lost the format:
cast(from_utc_timestamp(myTs, 'Europe/Madrid') as date)
--Result: '2006-12-31'
Another option:
date_format(from_utc_timestamp(myTs, 'Europe/Madrid'), 'yyyy-MM-dd HH:mm:ss.S')
--Result: '2006-12-31 02:37:08.0'
The result is String, if I cast as Date, again removes the format:
cast(date_format(from_utc_timestamp(myTs, 'Europe/Madrid'), 'yyyy-MM-dd HH:mm:ss.S') as Date)
--Result: '2006-12-31'
You explained your question pretty well. But unfortunately, it's not possible. If you define any field as date, it is simply a date. The moment you add hours, mins, secs - any of them, you have to define them as a timestamp. Date format will simply have year month and the day.

concat date and varchar and convert to timestamp

i have column with date type, and another with varchar type, i want to concatinate this column then convert it in timestamp
i try this but doesn't work
CAST (adjustmentDate || adjustmenttime AS TIMESTAMP (0) FORMAT 'YYYYMMDDHHMISS' )
Can you please try
select
cast(adjustmentDate as timestamp(0)) +
( CAST (adjustmenttime AS time(4) format 'HHMISS') - time '00:00:00' hour to second)
;
I works for me with some literals, so I'm hoping it can work for your table.

How to convert oracle timestamp to postgres?

How to convert the oracle timestamp to postgres timestamp,we have a values in oracle data base as below,
15-JUN-2014 01.00.00.0000 PM
I have stored as varchar in postgres/Greenplum. How can I cast to timestamp in postgres,
Output required As below ,
15-06-2014 13.00.00.0000
As 24 hrs format.
Timestamps are timestamps. They represent a date (incl. time) using an internal representation. But you can convert timestamps to/from strings using various formats. For example, if you need to display them or in order to export your data to an other software requiring some specific representation.
In the following code for Oracle, I convert from a string to a timestamp (TO_TIMESTAMP) using a format corresponding to your first example. Then I "convert back" that timestamp to a string (TO_CHAR) using an other format corresponding to your second representation:
-- Convert a string to a timestamp
WITH sample_data AS
(SELECT TO_TIMESTAMP('15-JUN-2014 01.00.00.0000 PM',
'DD-MON-YYYY HH12.MI.SS.FF4 PM') ts FROM DUAL)
-- Convert a timestamp to a string using a format corresponding
-- to "15-06-2014 13.00.00.0000"
SELECT TO_CHAR(ts, 'DD-MM-YYYY HH24.MI.SS.FF4') as result FROM sample_data;
Producing (the string):
result
15-06-2014 13.00.00.0000
Please note that you can convert from string to date in PostgreSQL too using (almost?) exactly the same date functions and formats.
SELECT To_timestamp(event_timestamp,'DD-MON-YY HH12.MI.SS.SSSS AM.PM')::timestamp without time zone
FROM jiodba.ext_wfa_accounting_msg limit 1000;
Please find the answer

How to format bigint field into a date in Postgresql?

I have a table with a field of type bigint. This field store a timestamp.
I want to date format the field like this :
to_char( bigint_field,'DD/MM/YYYY HH24:MI:SS')
I get the following error :
ERROR: multiple decimal points
État SQL :42601
TO_CHAR(TO_TIMESTAMP(bigint_field / 1000), 'DD/MM/YYYY HH24:MI:SS')
This is what worked for me
to_timestamp( bigint_field/1000)::date
This depends on what the bigint value represents - offset of epoch time, or not.
select to_timestamp(20120822193532::text, 'YYYYMMDDHH24MISS')
returns
"2012-08-22 19:35:32+00"
I did it like this:
to_timestamp(to_char(20120822193532, '9999-99-99 99:99:99'),'YYYY-MM-DD HH24:MI:SS')
the result looks like this:
2012-08-22 19:35:32
you also can use this in you select statemant, just exchange the number with your database colunm.
Step by Step Explanation:
to_char(20120822193532, '9999-99-99 99:99:99')
This will create a string like this:
"2012-08-22 19:35:32"
now we can easiely convert this into a timestamp:
to_timestamp('2012-08-22 19:35:32','YYYY-MM-DD HH24:MI:SS')
Result will look the same as before, but it's now a timestamp.
Also, if you use this for a command like
CREATE TABLE table2 AS SELECT to_timestamp(to_char(tb1.date, '9999-99-99 99:99:99'),'YYYY-MM-DD HH24:MI:SS') AS realDate FROM table1 AS tb1;
you might end up with timstamptz (timestamp with time zone) instead of timestamp (timestamp without time zone). You can change it like this:
ALTER TABLE table2 ALTER realDate SET DATA TYPE timestamp USING realDate;