I have a table with a column defined as:
"timestamp" int8 NULL,
which stores values like
'1638462043745210034'
When I try to cast it to timestamp or timestamp with time-zone
SELECT '1638462043745210034'::timestamp at time zone 'UTC' ;
it returns an error:
date/time field value out of range: "1638462043745210034" Hint:
Perhaps you need a different "datestyle" setting.
What kind of datestyle can it be and how can it be converted to a normal timestamp?
Looks like nanoseconds since the epoch:
SELECT to_timestamp(1638462043745210034 / 1000000000.0);
to_timestamp
══════════════════════════════
2021-12-02 17:20:43.74521+01
(1 row)
Related
I have timestamps like the following '2018-04-18T18:11:16+01:00' in varchar type and want to transform them to a timestamp type. I am having trouble using the to_timestamp(). Is there a specific way to use the to_timestamp() function that will allow me to do the transformation i want? If not, is there any other way to achieve my goal?
Doing
to_timestamp('2018-04-18T18:11:16+01:00', 'DD-MM-YYYY HH24:MI:SS')
gives an error of
Error: date/time field value out of range:
"2018-04-18T18:11:16+01:00"
I would expect varchars like '2018-04-18T18:11:16+01:00' to be transformed to 2018-04-18 17:11:16 (where type is timestamp)
demo:db<>fiddle
If you simply want to get the string into a timestamp holding the time zone:
Simply cast it into timestamp with time zone (= timestamptz) type:
SELECT '2018-04-18T18:11:16+01:00'::timestamptz
If you just want to cut the time zone part and holding the time without any further calculations: Simply cast it into type timestamp without time zone (= timestamp)
SELECT '2018-04-18T18:11:16+01:00'::timestamp
If you want to convert it into a timestamp at UTC (calculating -1) you can do:
SELECT ('2018-04-18T18:11:16+01:00' AT TIME ZONE 'UTC')::timestamp
I have a String in this format: 2018-11-01T00:00:00-07:00 and I would like to convert it to a TIMESTAMP and insert it into a TIMESTAMP column. However, when I insert it, it drops the -07:00 without first converting it to -00:00. How do I ensure that it is converted and stored in Redshift properly?
Here is an example:
select ORIGINAL_DATE, TO_TIMESTAMP(ORIGINAL_DATE,'YYYY-MM-DD HH24:MI:SS') FROM CDW_LANDING.X where id = XXXXXX;
=> 2018-11-01T00:00:00-07:00 2018-10-31 17:00:00
The TO_TIMESTAMP converts it to 2018-10-31 17:00:00 which is what I want. However, when I insert it, it becomes 2018-11-01 00:00:00 and simply drops the -07:00.
Here is the example:
insert into cdw_stage.X (ORIG_DT)
select TO_TIMESTAMP(ORIGINAL_DATE,'YYYY-MM-DD HH24:MI:SS')
from CDW_LANDING.INVOICE where id = XXXXXX;
But when I query it with select ORIG_DT from cdw_landing.X;, it displays 2018-11-01 00:00:00. What I would like to see is 2018-10-31 17:00:00 which is what the TO_TIMESTAMP function should do.
The ORIG_DT in Redshift is in TIMESTAMP format. The input date is in VARCHAR.
How do I get Redshift to save this correctly? I also added postgres tag because Redshift is based off of postgres. Thank you so much!!!
2018-11-01T00:00:00-07:00 is not a timestamp (timestamp without time zone) literal, strictly speaking. It is a timestamptz (timestamp with time zone) literal. This is the root of all pain in your question. The wrong cast to timestamp ignores the offset. The Postgres manual:
In a literal that has been determined to be timestamp without time zone, PostgreSQL will silently ignore any time zone indication. That
is, the resulting value is derived from the date/time fields in the
input value, and is not adjusted for time zone.
Bold emphasis mine.
The use of TO_TIMESTAMP() can't save you. The Redshift manual:
Formats that include a time zone (TZ, tz, or OF) are not supported as input.
(The same is true in Postgres.)
Solution
Cast to timestamptz (or use a column of that type to begin with), the rest should fall in place:
SELECT cast('2018-11-01T00:00:00-07:00' AS timestamptz);
Or:
SELECT '2018-11-01T00:00:00-07:00'::timestamptz;
The manual about casting in Redshift.
When an actual timestamptz is assigned to a timestamp column it is converted according to the current timezone setting of the session automatically. If you want a different target timezone, use the AT TIME ZONE construct. Details:
Ignoring time zones altogether in Rails and PostgreSQL
The related answer is for Postgres, but timestamp handling in Redshift (while differing in many other aspects!) is the same. The Redshift manual:
When converting DATE or TIMESTAMP to TIMESTAMPTZ, DATE or TIMESTAMP
are assumed to use the current session time zone. The session time
zone is UTC by default. For more information about setting the session
time zone, see timezone.
I have a timestamp as 2017-07-19 11:45:01and i want it to convert to int.
Query:
select cast(max(event_timestamp) as INT) from error_messages where error_level='ERROR' and user_name='git'
Error:
SQL Error [2366] [42846]: [Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
[Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
com.vertica.util.ServerException: [Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
You have to use TIMESTAMPDIFF() this way:
SELECT TIMESTAMPDIFF(SECOND,'001-01-01 00:00:00', '2015-02-23 03:12:35');
timestampdiff
---------------
63560257955
to get the number of time units you want (SECONDs here above) since the timestamp you want...
If you want to get Unix Timestamp of that date as int than search fort that.
One option would be to calculate the range from your date to '1970-01-01' in seconds as int. This is the Unix Timestamp.
Use JULIAN_DAY function in Vertica to convert the time stamp to a integer value or number.
For more details refer Vertica documentation link: https://my.vertica.com/docs/6.1.x/HTML/index.htm#16070.htm
To extract number from date time with 1 second interval.
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-08');
I have a column in my table which is of type Timestamp.
while converting this field to the format: 2003-08-09T05:48:37+05:30, I am using the following query:
select
TO_CHAR(CONSUMER_DLY_TIME, 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
from oms_cust_ord_head;
it gives me the error: "date format not recognized"
How to resolve this ?
A TIMESTAMP value does not contain any time zone information, thus you cannot display it.
Which time zone do you want do be shown?
For time zone of database operating system you can use:
SELECT
TO_CHAR(CONSUMER_DLY_TIME, 'YYYY-MM-DD"T"HH24:MI:SS')||TO_CHAR(SYSTIMESTAMP, 'TZH:TZM')
or for you current session time zone:
SELECT
TO_CHAR(CONSUMER_DLY_TIME, 'YYYY-MM-DD"T"HH24:MI:SS')||TO_CHAR(CURRENT_TIMESTAMP, 'TZH:TZM')
SELECT
TO_CHAR(CAST(CONSUMER_DLY_TIME AS TIMESTAMP WITH TIME ZONE), 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
SELECT
TO_CHAR(CONSUMER_DLY_TIME, 'YYYY-MM-DD"T"HH24:MI:SS')||TZ_OFFSET(SESSIONTIMEZONE)
You should use
select trunc(<timestamp_column>) from your_table
Read Format Models in detail.
Note: Format in which the date will be displayed depends on your session parameter nls_date_format.
In Postgres, is it possible to change the default format mask for a timestamp?
right now comes back as
2012-01-03 20:27:53.611489
I would like resolution to minute like this:
2012-01-03 20:27
I know I can do this on individual columns with to_char() as or stripped down with a substr() by the receiving app, but having it formatted correctly initially would save a lot of work and reduce a lot of errors.
In PostgreSQL, The formatting of timestamps is independent of storage. One answer is to use to_char and format the timestamp to whatever format you need at the moment you need it, like this:
select to_char(current_timestamp, 'yyyy-MM-dd HH24:MI:SS.MS');
select to_timestamp('2012-10-11 12:13:14.123',
'yyyy-MM-dd HH24:MI:SS.MS')::timestamp;
But if you must set the default formatting:
Change the postgresql timestamp format globally:
Take a look at your timezone, run this as an sql query:
show timezone
Result: "US/Eastern"
So when you are printing out current_timestamp, you see this:
select current_timestamp
Result: 2012-10-23 20:58:35.422282-04
The -04 at the end is your time zone relative to UTC. You can change your timezone with:
set timezone = 'US/Pacific'
Then:
select current_timestamp
Result: 2012-10-23 18:00:38.773296-07
So notice the -07 there, that means we Pacific is 7 hours away from UTC. How do I make that unsightly timezone go away? One way is just to make a table, it defaults to a timestamp without timezone:
CREATE TABLE worse_than_fail_table
(
mykey INT unique not null,
fail_date TIMESTAMP not null
);
Then if you add a timestamp to that table and select from it
select fail_date from worse_than_fail_table
Result: 2012-10-23 21:09:39.335146
yay, no timezone on the end. But you want more control over how the timestamp shows up by default! You could do something like this:
CREATE TABLE moo (
key int PRIMARY KEY,
boo text NOT NULL DEFAULT TO_CHAR(CURRENT_TIMESTAMP,'YYYYMM')
);
It's a text field which gives you more control over how it shows up by default when you do a select somecolumns from sometable. Notice you can cast a string to timestamp:
select '2012-10-11 12:13:14.56789'::timestamp
Result: 2012-10-11 12:13:14.56789
You could cast a current_timestamp to timestamp which removes the timezone:
select current_timestamp::timestamp
Result: 2012-10-23 21:18:05.107047
You can get rid of the timezone like this:
select current_timestamp at time zone 'UTC'
Result: "2012-10-24 01:40:10.543251"
But if you really want the timezone back you can do this:
select current_timestamp::timestamp with time zone
Result: 2012-10-23 21:20:21.256478-04
You can yank out what you want with extract:
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 20
And this monstrosity:
SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'EST';
Result: 2001-02-16 20:38:40
In postgres, you can change the default format mask for datetimes - using the set datestyle option; the available options can be found here (see 8.5.2. Date/Time Output).
Unfortunately, all the available options include the number of seconds - you will therefore need to reformat them either in the query or the application code (if applicable).
to_char() is used to create a string literal. If you want a different timestamp value, use date_trunc():
date_trunc('minute', now())
For converting literal input, use to_timestamp():
to_timestamp('2012-01-03 20:27:53.611489', 'YYYY-MM-DD HH24:MI')
This returns timestamptz. Cast to timestamp [without time zone] by appending ::timestamp (which assumes your current timezone setting), or with the AT TIME ZONE construct to define a time zone explicitly.
To my knowledge, there is no setting in PostgreSQL that would trim seconds from timestamp literals by default.