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? - postgresql

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;

Related

Need help to identify the actual date time format

I have been struggling since few days to identify correct format for sample input date:
2020-01-30 14:39:25.022000 +00:00:00
Background :
I am working on data migration with postgres. And, have identified that in DB, the datetime related fields like 'modifiedAt' have datatype as varchar, which is bit weird. These records have values in above mentioned format.
I have fixed it in new postgres DB instance with datatype as timestamp. After migrating data, these fields are showing datetime in following format:
2020-01-30 14:39:25
How can I format above date to give output in this dateformat 2020-01-30 14:39:25.022000 +00:00:00?
Any help will be appreciated.
First you want to use timestamptz not timestamp. timestamptz does not actually store the time zone, it just makes the value time zone aware. For more information on this see:
Time Stamps 8.5.1.3. Time Stamps.
Second it looks like you did something like timestamp(0) which reduced the precision to whole seconds. If you leave the precision alone you get:
select '2020-01-30 14:39:25.022000 +00:00:00'::timestamptz;
timestamptz
-----------------------------
01/30/2020 06:39:25.022 PST
-- I am in PST so the value gets rotated to that time zone for display.
-- If you want something closer to you desired output, then:
select to_char('2020-01-30 14:39:25.022000 +00:00:00'::timestamptz AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS.USOF');
to_char
-------------------------------
2020-01-30 14:39:25.022000+00
While your timestamp strings do appear to have a time zone component, it is +00, which is the default for timestamps. So, you might be able to just use TO_TIMESTAMP here with an appropriate format mask covering microseconds:
WITH yourTable AS (
SELECT '2020-01-30 14:39:25.022123 +00:00:00'::text AS ts
)
SELECT TO_TIMESTAMP(LEFT(ts, 26), 'YYYY-MM-DD HH24:MI:SS.US')
FROM yourTable;
Demo
The output value from the demo above is 2020-01-30 14:39:25.022123+00

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

PostgreSQL how to convert date with timezone offset to UTC?

I have a PostgreSQL table with date field in the following format
2017-09-07T17:24:33+08:00
and I want to convert it to UTC time.
I've looked around but found no way to do that with this specific time format. What am I missing?
Thanks
timezone definition (https://www.postgresql.org/docs/9.1/functions-datetime.html) : The function timezone(zone, timestamp) is equivalent to the SQL-conforming construct timestamp AT TIME ZONE zone
SELECT timezone('UTC','2017-09-07T17:24:33+08:00');
if selecting from column,
with t as (
SELECT '2017-09-07T17:24:33+08:00' as tm
)
SELECT timezone('UTC',tm::timestamptz) as ts
from t;
I have a PostgreSQL table with date field in the following format: 2017-09-07T17:24:33+08:00
This is incorrect. Per Postgres documentation,
All timezone-aware dates and times are stored internally in UTC. They are converted to local time in the zone specified by the TimeZone configuration parameter before being displayed to the client.
To display a TIMESTAMP WITH TIME ZONE attribute (which, again, is stored internally in UTC) in a specific time zone, you have a few options:
By default, “They are converted to local time in the zone specified by the TimeZone configuration parameter before being displayed to the client.” This can be set in postgresql.conf, using the SQL command SET TIME ZONE, or using a number of other options.
Use the AT TIME ZONE construct.
So, in regards to your original question: You don't have to do anything to your timestamptz for Postgres to store it in UTC. To display your attribute in UTC, you can either change the TimeZone configuration paramter, or use the construct below:
SELECT dt_with_timezone AT TIME ZONE 'UTC' FROM my_table

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

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;

what will be data type to store date time in cassandra

what will be data type to store date time in cassandra
CREATE TABLE testTable (
dateValue date,
time timestamp
)
n my insert staements would be like this ,
insert into caliper.log_per_day ( timeStampValue,dateValue ) values ('2015-12-30 16:10:31','2015-12-30');
i wanted to store date & time both in one column like this '2015-12-30 16:10:31'.
but if i used timestamp it would be store like this '2015-12-30 04:10:31+0530'
Notes : primary key n other things are skip here... ignore it.
Cqlsh will display timestamps in the following format by default:
yyyy-mm-dd HH:mm:ssZ
The Z in these formats refers to an RFC-822 4-digit time zone,
If no time zone is supplied, the current time zone for the Cassandra
server node will be used.
so if you don't want to store in this way you can store it as varchar.