How do I go from Varchar string into Unix time type? Greenplum - postgresql

I'm fairly inexperienced with SQL let alone PostgreSQL so appreciate your help.
Let's say we have a varchar column called ID that is a combination of IP address and a unix epoch time string.
So when I run this query:
SELECT substring(b.id from char_length (r.IP_address)+2 for 10), b.id, r.ip_address
FROM bq b
INNER JOIN event r ON r.visitor_id::TEXT = b.id::TEXT
LIMIT 3;
Output
substring id ip_address
1460854333 97.128.39.256.1460854333288493 97.128.39.256
So the output of the substring is a unix time stamp but it is in a varchar format. How do I convert the varchar into a unix timestamp? Ultimately I am going to convert that time stamp into date I just don't think I can directly go from the varchar of a unix string to a date?

As Terra said, use the to_timestamp() function:
# select to_timestamp('1460854333') ;
to_timestamp
------------------------
2016-04-16 17:52:13-07
(1 row)
If a date is the type you're after, just add the ::date cast:
# select to_timestamp('1460854333')::date ;
to_timestamp
------------------------
2016-04-16
(1 row)
The function is basically executing this:
select ('epoch'::timestamptz + '1460854333'::float * '1 second'::interval) ;

Related

How to pass datetime field in UTC format as a parmeter in Query in DB2

I have a date time field which is coming from an external system in UTC format 2022-01-02T08:00:00.000+00:00. This value should be queried in DB2 to determine whether the record exists or not. The date stored in DB2 is in the format 2022-01-01 08:00:00.000 Is there any way to convert the incoming date in the format 2022-01-01 08:00:00.000 ?
The final query should be something like
select * from table where changedate = '2022-01-02T08:00:00.000+00:00'
Db2 doesn't store timestamps in a string format. Some binary format is used for that.
So, if I got you right, your question should be changed to "how to convert YYYY-MM-DDTHH24.MI.SS.FF3XXXXXX string representation of timestamp to the timestamp data type".
Unfortunately, there is no such a built-in pattern in the TO_DATE / TIMESTAMP_FORMAT function, but you can use the following expression. T column has the timestamp data type, and you may use this expression in the select * from table where changedate = ... statement.
SELECT
S
, TO_DATE (TRANSLATE (SUBSTR (S, 1, 23), ' ', 'T'), 'YYYY-MM-DD HH24:MI:SS.FF3')
+ CAST (TRANSLATE (SUBSTR (S, 24), '', ':', '') || '00' AS DEC (6)) AS T
FROM
(
VALUES
'2022-01-02T08:00:00.000+00:00'
, '2022-01-02T08:00:00.000+03:30'
, '2022-01-02T08:00:00.000-03:30'
) T (S)
S
T
2022-01-02T08:00:00.000+00:00
2022-01-02-08.00.00.000000
2022-01-02T08:00:00.000+03:30
2022-01-02-11.30.00.000000
2022-01-02T08:00:00.000-03:30
2022-01-02-04.30.00.000000

Converting Integer values to Date in Presto SQL

Below is a script i am trying to run in Presto; Subtracting today's date from an integer field I am attempting to convert to date. To get the exacts days between. Unfortunately, it seems the highlighted block does not always convert the date correctly and my final answer is not correct. Please does anyone know another way around this or a standard method on presto of converting integer values to date.
Interger value in the column is in the format '20191123' for year-month-date
select ms, activ_dt, current_date, date_diff('day',act_dt,current_date) from
(
select ms,activ_dt, **CAST(parse_datetime(CAST(activ_dt AS varchar), 'YYYYMMDD') AS date) as act_dt**, nov19
from h.A_Subs_1 where msisdn_key=23480320012
) limit 19
You can convert "date as a number" (eg. 20180527 for May 27, 2018) using the following:
cast to varchar
parse_datetime with appropriate format
cast to date (since parse_datetime returns a timestamp)
Example:
presto> SELECT CAST(parse_datetime(CAST(20180527 AS varchar), 'yyyyMMdd') AS date);
_col0
------------
2018-05-27
You can use below sample query for your requirement:
select date_diff('day', date_parse('20191209', '%Y%m%d'), current_timestamp);

Extract Only Date from timestamp in postgres

I have a 'timestamp without time zone' value in a column example:
PunchIn = "2019-06-10 09:10:00"
How can I extract "2019-06-10 00:00:00" from this column?
demo:db<>fiddle
SELECT my_date::date::timestamp
This converts the date column my_date to date (cuts the time part) and if you cast it back into timestamp it gets the 0 time.
Alternatively you can use the date_trunc function:
SELECT date_trunc('day', my_date)
SELECT SUBSTRING("PunchIn = '2019-06-10 09:10:00'" FROM 11 FOR 21);
should return the string you're looking for (if I count the index number right:)
You can use the DATE function to extract date:
SELECT DATE(SUBSTRING("PunchIn = '2019-06-10 09:10:00'" FROM 11 FOR 21));

How can I have timestamp displayed in UTC+02 (same timezone) for both the queries below?

My first query is:
SELECT distinct wfc_request_job_id,wfc_request_job_info,
replace(iso_cc,';',' ') as "iso_cc",to_char(wfc_request_start_ts,'yyyy-MM-dd HH:mm:ss') as ts,
sent_message_count,
(link_object_count + poi_object_count + point_address_object_count) as request_object_count
FROM wfc_request_job
where
wfc_request_job_id=173526;
This returns ts as 2015-08-16 03:08:59
Second Query:
SELECT wfc_request_job_id,wfc_request_start_ts,wfc_request_end_ts,replace(iso_cc,';',' ') as "iso_ccs",sent_message_count,wfc_queue_name
FROM wfc_request_job
where
to_char(wfc_request_start_ts,'YYYY-MM-DD') >= to_char(to_date('08/16/2015','MM/DD/YYYY'),'YYYY-MM-DD')
and to_char(wfc_request_start_ts,'YYYY-MM-DD') <= to_char(to_date('08/16/2015','MM/DD/YYYY'),'YYYY-MM-DD')
order by wfc_request_job_id desc
This returns ts of the job id mentioned above as - "2015-08-16 15:58:59.809+02"
How can I make both the queries return ts in UTC+02 - i.e. same timezone
The data type of wfc_request_start_ts is - timestamp with timezone
I changed to queries to have the format HH24:MI:SS however that did not help. Please note that the webapp using these queries will be opened in both Germany and USA.
According to postgresql manual to_char there is TZ (and OF as of v9.4) template patterns for Date/Time formatting.
Therefore in query you need to add it so
postgres=# select to_char(now(),'yyyy-MM-dd HH24:mm:ss TZ');
to_char
------------------------
2015-08-19 12:08:56 CEST
(1 row)
Also, make sure you specify timezone when converting
so instead
to_date('08/16/2015','MM/DD/YYYY')
use
TIMESTAMP WITH TIME ZONE '2015-08-16 00:00:00+02';
in second query.

character string that represents date in PostgreSQL

How do I return a character string that represents the specified datepart of the specified date using PostgreSQL?
Note: In SQL Server we use the following syntax.
Example: (Using SQL Server)
SELECT DATENAME(DW,'2014-05-22'); /*For Weekday */
As documented in the manual the to_char() function can be used for this:
select to_char(DATE '2014-05-22', 'Day');
returns Thursday
date_part(text, timestamp)
Example for day of the week:
select date_part('dow', timestamp '2014-05-22 00:00:00')