I am using Redshift and am looking to extract the time from the timestamp.
Here is the timestamp: 2017-10-31 23:30:00
and I would just like to get the time as 23:30:00
I tried using cast(the_timestamp_column as time) but ran into this error message:
error message: Function ""time"(timestamp without time zone)" not supported.
Use to_char with an appropriate format mask to extract the time component of your timestamp:
select to_char(the_timestamp_column, 'HH24:MI:SS')
from your_table;
Demo
Related
I am using Hive and wants to get the UTC time.
By running
select date_format(to_utc_timestamp(bigint(1621446734295),'UTC'),'yyyy-MM-dd HH:mm:ss.SSS')
It returns: 2021-05-20 01:52:14.295.
However, this timestamp refers 2021-05-19 17:52:14.295 GMT.
Why does the function to_utc_timestamp still returns time with timezone? Do I need to change some settings for Hive?
I want to truncate a specific date to midnight time in hive.
I have tried the following below, but it puts 12 hour time instead of 00.
date_format(MIN(date_and_time), 'yyyy-MM-dd hh:00:00.000')
Result obtained
2021-11-03 12:00:00.000
Any suggestions on that?
Thank you
Use HH for a 24-hour clock:
date_format(MIN(date_and_time), 'yyyy-MM-dd HH:00:00.000')
I have a scenario like below in hive
convert the current_timestamp to UTC. I am able to do so
select to_utc_timestamp(current_timestamp, 'America/Los_Angeles)';
Result:
2020-02-04 10:00:06.162
Next convert this resulting timestamp to yyyyMMddHHmmssSSS format.
I have tried like below
select from_unixtime((to_utc_timestamp(current_timestamp, 'America/Los_Angeles)', 'yyyy-MM-dd HH:mm:ss.SSS'), 'yyyyMMddHHmmssSSS');
I am unable to get the desired result.
expected result is 20200204100006162
You can use the date_format function if the Hive version >= 1.2.0.
select date_format(to_utc_timestamp(current_timestamp, 'America/Los_Angeles'),'yyyyMMddHHmmssSSS')
I am trying to extract the hour from a timestamp with a timezone. However, my times are coming up incorrectly.
Here's an example, I am using Dbeaver with my timezone set to EST:
SELECT '2020-01-24 14:27:12' AT TIME ZONE 'US/Pacific' as foo,
EXTRACT(HOUR FROM foo) as ex,
DATE_PART('HOUR', foo::timestamp) as dp
RETURNS:
foo |ex |dp
2020-01-24 17:27:12 |22 | 22
Why is my time coming up 3 hours ahead, it should be 3 hours behind?
Extract and DATE_PART don't seem to get me the hour I would like. It looks like it's taking 17 as EST and then converting it to UTC. Here's what I am expecting to get:
foo |ex |dp
2020-01-24 11:27:12 |11 | 11
Check if your timezone is set to EST:
SELECT current_setting('TIMEZONE');
or with:
show timezone;
If it is not you can set it like this:
set timezone to est;
AS shown in this DEMO
If that is not working try with convert_timezone
select convert_timezone('US/Pacific', '2020-01-24 14:27:12')
And exploring the mater on hand I have found this fact:
Note Amazon Redshift doesn't validate POSIX-style time zone
specifications, so it is possible to set the time zone to an invalid
value. For example, the following command doesn't return an error,
even though it sets the time zone to an invalid value.
set timezone to ‘xxx36’;
from this source: https://docs.aws.amazon.com/redshift/latest/dg/CONVERT_TIMEZONE.html
'AT TIME ZONE' does not work like you are expecting. Use convert_timezone() instead.
SELECT
'2020-01-24 14:27:12' AT TIME ZONE 'US/Pacific' as foo,
-- expected '2020-01-24 6:27:12' got '2020-01-24 22:27:12+00'
convert_timezone('UTC', 'US/Pacific', CAST('2020-01-24 14:27:12' AS TIMESTAMP WITHOUT TIME ZONE)) as bar
-- expected '2020-01-24 6:27:12' got '2020-01-24 06:27:12'
;
'AT TIME ZONE' interprets the timestamp as being relative to the specified time zone and converts it to a TIMESTAMPTZ offset to UTC. That is in the above example it converts from US/Pacific to UTC, not the other way around.
It works perfectly fine for me (using dbVisualizer).
The issue is with your SQL Client.
SQL clients often impose formatting that impacts the values you see. You can test this by converting values to Text before sending them to your SQL client:
SELECT
'2020-01-24 14:27:12' AT TIME ZONE 'US/Pacific' as foo,
foo::text as t,
EXTRACT(HOUR FROM foo) as ex,
DATE_PART('HOUR', foo::timestamp) as dp
For me, this results in:
2020-01-24 22:27:12+00 2020-01-24 22:27:12+00 22.0 22.0
Try it in your SQL client and see what happens.
I am using Redshift and am looking to extract the time from the timestamp.
Here is the timestamp: 2017-10-31 23:30:00
and I would just like to get the time as 23:30:00
Is that possible?
In Redshift you can simply cast the value to a time:
the_timestamp_column::time
alternatively you can use the standard cast() operator:
cast(the_timestamp_column as time)
Please go through this link
http://docs.aws.amazon.com/redshift/latest/dg/r_Dateparts_for_datetime_functions.html
timezone, timezone_hour, timezone_minute
Supported by the DATE_TRUNC function and the EXTRACT for time stamp
with time zone (TIMESTAMPTZ)
Examples is here
select extract(minute from timestamp '2009-09-09 12:08:43');
select extract(hours from timestamp '2009-09-09 12:08:43');