How do I convert timestamp and offset columns to a single timestamptz column? - postgresql

Image I have a table containing the following two columns:
timestampwithouttimezone (of type TIMESTAMP WITHOUT TIME ZONE)
utcoffset (of type INTEGER)
I want to convert those two column to a single one of type TIMESTAMP WITH TIME ZONE. Can this be achieved using a ALTER TABLE ALTER COLUMN [column] SET DATE TYPE TIMESTAMP WITH TIME ZONE query and the additional USING clause?
Or do I need a separate UPDATE query that takes the offset and sets the timezone of the timestamps? If that's the case, what would that query be? I can't find any examples that show how to update the timezone using an integer.

You could do that like this, assuming the offset is in hours:
ALTER TABLE mytab
ALTER timestampwithouttimezone
TYPE timestamp with time zone
USING CAST (timestampwithouttimezone::text || ' '
|| to_char(utcoffset, 'S00FM')
AS timestamp with time zone),
DROP utcoffset;

Related

PostgreSQL function lags when passing a date from another table as argument

I created a function that retrieves data from multiple table and insert the result into a new table. I am passing few dates in the where clause to filter the applicable information. The dates are in timestamptz format. The query takes roughly a min to process. However when I make the change in the where clause to pass the date from another table, the query lags a lot. i.e.
My initial argument that doesn't lag is
where timestamp = '2022-01-05 04:00:00+00'
The lag is when I change this where clause to pass this date from another table.
where timestamp = select date_time from table_Test
The date_time in table_test is defined as timestamptz and the value is '2022-01-05 04:00:00+00'.
Any idea why the query lags when I pass the date argument from another table?
Thanks,

I have varchar column with "2022-11-30T17:30:00.000Z" format how can i transform it to timestamp with time zone column in postgres?

We are storing date time in "2022-11-30T17:30:00.000Z" format in var char column i know its totally wrong but I want to rectify the mistake and use the column as timestamp with time zone.
I Tried using column::timestamp AT TIME ZONE 'Asia/Calcutta' but still date and time is wrong as it does not considers +5:30 in my time zone.please suggest how can i transform the column or if i could use the same column.
select dtm from dashboard where (select dtm::timestamp AT TIME ZONE 'Asia/Calcutta')::date between '2022-11-30' and '2022-11-30' order by dtm;

Redshift: converting timestamp to date in specific time zone

Using psycopg to connect to a Redshift DB I am trying to group by date (in a specific time zone, given at query time). All date_times are stored without time zone (and correspond to UTC timestamps).
I have been trying the following:
SELECT DISTINCT DATE(TIME_ZONE(%(time_zone)s, date_time_dim.timestamp)) AS date,
SUM(meals.sugar_in_g) AS total_sugar
FROM meals
INNER JOIN date_time_dim
ON meals.created_at = date_time_dim.timestamp
WHERE meals.patient_id=%(patient_id)s
AND date_time_dim.timestamp >= %(utc_start_date_time)s
AND date_time_dim.timestamp <= %(utc_end_date_time)s
GROUP BY date
ORDER BY date ASC;
with the following query dictionary:
utc_start_date_time UTC-converted date time
utc_end_date_time UTC-converted date time
patient_id an integer
time_zone, a string, ex: 'US/Hawaii'
The goal being to have one entry for each date (in the given time zone).
What I tried gives me:
function time_zone("unknown", timestamp without time zone) does not exist
What am I missing ?
Edit: This is the same with CONVERT_TIME_ZONE, with and without source time_zone, with and without type casting time_zone to VARCHAR.
The function in Redshift to return a timestamp in a different timezone is TIMEZONE() not TIME_ZONE() - see https://docs.aws.amazon.com/redshift/latest/dg/Date_functions_header.html

save epoch time of on column into another column postgres

I have a table with two field in postgresql. One field is creation_time which is a timestamp with timezone and is filled with datetimes. Other field is foo which is filled by null values and I want to update this column by the epoch time of other column for each row.
How this is possible? I know that extract(epoch) can convert timestamp to epoch, but I don't know how to use it for my purpose.
This should work. foo should be of type bigint.
UPDATE table SET foo = EXTRACT(epoch FROM creation_time)

Convert bigint data type to timestamp (and subsequently to date) in redshift

I need to convert the value stored in a bigint column to a date field. The first step of the conversion involves converting it to timestamp, and subsequently use the TRUNC method to convert this column to a date value.
However, my query is failing while converting the bigint value to timestamp.
The error that I'm getting is:-
Amazon Invalid operation: cannot cast type bigint to timestamp without time zone;
The query I'm trying for now is something like this:-
select ts::timestamp from events limit 1;
I was able to avoid the time zone error by using the method described in this thread: https://stackoverflow.com/a/36399361
My dates are based on epochs, and I was able to do the following:
SELECT
(TIMESTAMP 'epoch' + contract_start_date * INTERVAL '1 Second ')
FROM
table_name
SELECT TIMESTAMP 'epoch' + {column of bigint}/1000 * INTERVAL '1 second' as adate FROM tbl
If you are starting with a POSIX timestamp, and trying to get a timezone aware datetime value, you will need to supply a timezone - even if you later want to truncate the time part away. I'm not familiar with redshift, but perhaps there is a way to specify you mean UTC.