CAST datetime to date misalignment in RedShift - amazon-redshift

I need to covert a DATETIME field to DATE in order to join two datasets. No biggy, CAST(DATETIME) as DATE. However, the following is happening:
Datetime
Date
2022-12-02T22:00:00
2022-12-03
2022-12-02T08:00:00
2022-12-02
Clearly something is going on with the interpretation of the times. The DATETIME is on GMT while my system is on EST. My guess is that CAST is reading the datetime as the system's time zone and converting it to GMT on more time. Does anyone have any thoughts on what is happening and how to work around this?
I have tried CAST, CONVERT, TRUNC, AT TIME ZONE... All of them still gives the same result. The result I am searching for is the following:
Datetime
Date
2022-12-02T22:00:00
2022-12-02
2022-12-02T08:00:00
2022-12-02
Thanks!

This is due to your default timezone.
You can change for the current session like so:
SET timezone TO 'UTC';
or equivalently:
SET timezone TO 'GMT';
To change session back to default timezone:
set timezone to default;
https://docs.aws.amazon.com/redshift/latest/dg/r_timezone_config.html

Related

PostgreSQL Format Timestamp with Timezone Offset (Australia/Sydney) as JSON/String

I would like to get a JSON object in Postgres that display timestamp with Australia/Sydney timezone offset (+10:00 or +11:00 during daylight saving), for example I would like json object returned with following values:
"2021-07-31T23:59:59.123456+10:00"
"2021-01-31T23:59:59.123456+11:00"
But when I use either to_json() or to_char() the timestamp value returned is UTC with offset +00:00
select to_json(current_timestamp::timestamptz),
to_char(current_timestamp::timestamptz, 'YYYY-MM-DD"T"HH24:MI:SS:MSOF')
"2021-01-31T07:47:22.895185+00:00"
2021-01-31T07:47:22:895+00
I have tried to add "at time zone 'AEDT'" but it shifts the timestamp value and keep the offset to +00:00.
Thanks.
to_json formats according to the current time zone setting of the session. I suggest that you set the session time zone to Australia/Sydney first.
set time zone 'Australia/Sydney';
select to_json('2021-01-31T07:47:22.895+00'::timestamptz);
Yields 2021-01-31T18:47:22.895+11:00 which I guess is what you need.

Converting DateTime field without timezone in Postgres from Middle European TIme to UTC

I wanted to convert (UPDATE in Postgres with SQL) a DateTime field without timezone from Middle European Time to UTC.
There is a problem here that the DateTime may be daylight time or winter time so that the DateTime is UTC+1 or UTC+2. The sticking point is to find the the DST boundary with Postgres with bord means.
I tried to write a stored procedure that could find the above mentioned DST boundary but I didn't find any simply suitable solution but this one:
https://www.keithf4.com/postgresql_dst/
Is there maybe a simpler solution?
That is simple. Convert it to an absolute timestamp and back. If you specify the time zone correctly, daylight savings time will be considered automatically.
For Austria, it would look like this:
SELECT TIMESTAMP '...' AT TIME ZONE 'Vienna/Europe' AT TIME ZONE 'UTC';

postgresql numeric to timestamp conversion timezone issue

I am trying to convert a numeric to timestamp in postgresql. However, it always converts it in EST timezone. Before running the query, I try the following.
set time zone 'UTC+10';
select to_timestamp(r.date/1000) as current_email_timestamp
FROM email;
However, It the timestamp always seem to be in US timezone. The emails are mostly sent during working hours but when I change the numeric back to timestamp with above query, it shows all the emails at night time
Could it be that the timestamp in numeric was stored in US timezone, or could it be that when converting back from numeric to timestamp, it is not coverting the timezone correctly.
Can someone please tell me how to fix this
Regards
Arif
You can use the at time zone modifier:
select to_timestamp(1411738200),
to_timestamp(1411738200) at time zone 'America/Chicago',
to_timestamp(1411738200) at time zone 'UTC+10'
Your postgres installation probably defaults to EST.

How can i insert timestamp with timezone in postgresql with prepared statement?

I am trying to insert to a timestamp with timezone field of my DB a string which includes date, time and timezone using prepared statement.
The problem is that Timestamp.valueof function does not take into consideration the time zone that the string inludes so it causes an error.
The accepted format is yyyy-[m]m-[d]d hh:mm:ss[.f...] which does not mention timezone.
That is the exact code that causes the error:
pst.setTimestamp(2,Timestamp.valueOf("2012-08-24 14:00:00 +02:00"))
Is there any way that i can overcome it??
Thanks in advance!
The basic problem is that a java.sql.Timestamp does not contain timezone information. I think it is always assumed to be "local timezone".
On solution I can think of is to not use a parameter in a PreparedStatement, but a timezone literal in SQL:
update foo
set ts_col = timestamp with time zone '2012-08-24 14:00:00 +02:00'`;
Another possible solution could be to pass a properly formatted String to a PrepareStatement that uses to_timestamp():
String sql = "update foo set ts_col = to_timestamp(?, 'yyyy-mm-dd hh24:mi:ss')";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, "2012-08-24 14:00:00 +02:00");
I believe that you could use one more field in your database, which would include the time zone. And calculate the time manually after you get these two fields

How to get a datetime variable with a value of 00-00 and month ago with a given timezone?

I am looking for help to accomplish following task in T-SQL:
I have a timezone, and I need to calculate an UTC datetime which is current date minus one month, but has 0 hours and 0 minutes in a given timezone.
Is this what you need?
Select Cast(Floor(Cast(DateAdd(month, -1, getutcdate()) as float)) as datetime)
SQL Server 2008 and later provides the datetimeoffset type that includes a timezone. You can change the timezone using the SWITCHOFFSET function to change timezone and the DATEADD function to add -1 month to the resulting value. Then you can convert to the DATE type to eliminate the time part.
select cast(DATEADD(month,-1,todatetimeoffset(GETUTCDATE(),'+02:00')) as DATE)