I am new to postgresql bot not to sql in general. I have a table that I need to read values from, on of the columns is a unix timestamp that I want to convert in to a more human readable format thus I found this:
SELECT lt,dw,up,to_char(uxts, 'YYYY-MM-DD HH24:MI:SS')
from products;
But that produces an error:
ERROR: multiple decimal points
I am lost here. I am sure someone can show me how to do it. The documentation isn't that clear to me. Postgresql 9.5 is the database.
to_char() converts a number, date or timestamp to a string, not the other way round.
You want to_timestamp()
Convert Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp
So just apply that function on your column
SELECT lt,dw,up,to_timestamp(uxts) as uxts
from products;
This assumes that uxts is some kind of number data type (integer, bigint or double precision)
Related
If I do the following query,
select dt from table where dt <= timestamp '2021-06-01T10:45:00Z'
I get the ORA-01861: literal does not match format string error. However, if I remove T and Z from the string, it works fine.
So, my question is, how can I query a timestamp with T and Z here. I need to be able to do that, because the timestamp is returned by a tool in this format (so, I can't remove T and Z myself) and another tool directly uses that in a query. I can however change the query myself. The timestamp there is referred as a variable.
If your dt column is a timestamp with no time zone or date and you are ignoring the time zone from the fixed string then you can do:
where dt <= to_timestamp('2021-06-01T10:45:00Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z")
or
where dt <= to_date('2021-06-01T10:45:00Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z")
If you want to honour the time zone, because e.g. dt is a time stamp with timezone, then you can declare the value as UTC:
where dt <= from_tz(to_timestamp('2021-06-01T10:45:00Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"), 'UTC')
Be careful with the table column data type though.
And if have numbers after seconds, such as 10:38:10.11956, what would be the format?
Add .FF for fractional seconds:
to_timestamp('2021-06-01T10:38:10.11956Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF"Z")
Not that this won't work with to_date() as that doesn't have fraction-second precision. If necessary you can cast a timestamp to a date, or round/trunc to do that implicitly.
The format model elements are in the documentation; that also shows character literals - like the T and Z in this format.
A brute force method would use to_timestamp_tz() and to replace the timezone with an hour offset:
to_timestamp_tz(replace('2021-06-01T10:45:00Z', 'Z', ' +00'), 'YYYY-MM-DD"T"HH:MI:SS TZH')
I have a value in my csv file for timetamp as '1522865628160'. When I load the data in bigQuery where this field type is timestamp, it saves the timestamp as '1522865628160000'. so when I query like
select * from <tablename> limit 1
it gives me error
Cannot return an invalid timestamp value of 1522865628160000000 microseconds relative to the Unix epoch. The range of valid timestamp values is [0001-01-1 00:00:00, 9999-12-31 23:59:59.999999]; error in writing field timestamp"
please help
I think the issue here is that you tried to load your UNIX timestamp data into a timestamp column in BigQuery. A BigQuery timestamp column is not the same thing as a UNIX timestamp. The latter is just a numerical value representing the number of seconds since the start of the UNIX epoch in 1970.
So the fix here would be to load your data into an INT64 (or INTEGER if you are using legacy) column. From there, you may convert your UNIX timestamp to a bona fide date or timestamp.
There is a MSEC_TO_TIMESTAMP() function which can convert an integer number of milliseconds since the UNIX epoch to a bona fide timestamp, e.g.
SELECT MSEC_TO_TIMESTAMP(1522865628160)
2018-04-04 11:13:48 UTC
So I've been given a lovely little database. One of the tables in the database (several million rows large) has this column:
time_in character varying(255)
Stored in there is an epoch timestamp. What is the most sane way I can convert this to a proper epoch timestamp column without losing data?
First off there is no separate epoch timestamp datatype so the type you want to convert to is just regular timestamp. In the PostgreSQL Documentation - ALTER TABLE there's an example that fits to your case almost perfectly (I just added a cast to integer):
ALTER TABLE foo
ALTER COLUMN time_in SET DATA TYPE timestamp with time zone
USING
timestamp with time zone 'epoch' + time_in::integer * interval '1 second';
Note that the conversion might take some time and will produce an error if all of the rows are not valid epoch times.
Or, quoting the manual here:
A single-argument to_timestamp function is also available; it accepts
a double precision argument and converts from Unix epoch (seconds
since 1970-01-01 00:00:00+00) to timestamp with time zone. (Integer
Unix epochs are implicitly cast to double precision.)
ALTER TABLE foo ALTER COLUMN time_in
SET DATA TYPE timestamptz USING to_timestamp(time_in::float8);
But first, decide whether timestamp (timestamp without time zone) or timestamptz (timestamp with time zone) is the better choice for you:
Ignoring timezones altogether in Rails and PostgreSQL
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
I'm trying to create a column of type TIMESTAMP such that would store timestamps in the following format (i.e. including the "AM"/"PM" indicator):
20-04-2013 06:56:37 AM
I suppose I could use the to_char(..) function and store the timestamps as TEXT values, however isn't it possible to achieve the same effect with a vanilla TIMESTAMP column?
If you want to format a timestamp, use to_char when you SELECT it, or format the date in the client.
Timestamps don't have a format. They're stored as the number of fractional seconds since 1970-01-01 UTC inside the database and they're formatted into ISO timestamps for input and output. Inside the database they're just a number.
There is no way to override the default timestamp output format, since that would lead to a setting that could confuse applications that expect a single consistent format.
If the app wants a different date output format it has to ask for it with an appropriate to_char call.
Is this roughly what you are looking for?
select to_char(now(), 'DD-MM-YYYY HH12:MI:SS AM')