Cast timestamp to integer in Redshift - sql

I'm trying to cast or convert a timestamp column to an integer value in a Redshift database. I know it is possible in other SQL-based databases and I've done this before, however in Redshift I can't get it to work.
I've tried some variations of cast and also convert (http://docs.aws.amazon.com/redshift/latest/dg/r_CAST_function.html).
Here is an example:
SELECT cast(finish_time as integer) FROM table;
It gives me the error message:
SQL Error Invalid operation: cannot cast type timestamp without time zone to integer;
Is it possible to get a timestamp as an integer?

try select extract('epoch' from finish_time), it will give you Unix ms timestamp

Related

Convert dates to UTC in DB2

I need to convert dates in a DB2 Database to UTC values.
Usually the TO_UTC_TIMESTAMP function would be the way to do this, but this method seems to only work if the source time-zone is known. The problem I'm facing is that I need an SQL script, that converts the existing dates from the current time-zone of the database to UTC, without hard-coding the current time-zone.
What I already tried is the following SQL:
-- assuming there is a table called 'test' with a column 'col1' of the type timestamp
SELECT col1 AS my_time_zone, TO_UTC_TIMESTAMP(col1, CURRENT TIMEZONE) AS utc FROM test;
This does not convert the date value, because CURRENT TIMEZONE returns an integer value (in my case 10000 for UTC+01:00). This integer value seems to be converted to a VARCHAR, that cannot be interpreted by the TO_UTC_TIMESTAMP function.
From the DB2 documentation:
timezone-expression
[...] If the expression is not a VARCHAR, it is cast to VARCHAR before the function is evaluated.
[...] If the timezone-expression returns a value that is not a time zone in the IANA time zone database, then the value of expression is returned without being adjusted.
So my question is: Is there a way to get the current time-zone from a DB2 database, in a format that the TO_UTC_TIMESTAMP function can use? Or is there any other way to convert dates from the current time-zone to UTC?
That would be simply col1 - current timezone, given that col1 has the timestamp data type.

postgresql - creae view showing showing time as human readable [duplicate]

I am new to postgresql bot not to sql in general. I have a table that I need to read values from, on of the columns is a unix timestamp that I want to convert in to a more human readable format thus I found this:
SELECT lt,dw,up,to_char(uxts, 'YYYY-MM-DD HH24:MI:SS')
from products;
But that produces an error:
ERROR: multiple decimal points
I am lost here. I am sure someone can show me how to do it. The documentation isn't that clear to me. Postgresql 9.5 is the database.
to_char() converts a number, date or timestamp to a string, not the other way round.
You want to_timestamp()
Convert Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp
So just apply that function on your column
SELECT lt,dw,up,to_timestamp(uxts) as uxts
from products;
This assumes that uxts is some kind of number data type (integer, bigint or double precision)

Multiple to_timestamp() transformation in SELECT

I am currently having a varchar tm column, that stores timestamps such as: '15.11.2021 11:07:27'
The datestyle is currently set to ISO,MDY
How can I transform that varchar using SELECT value in order to preserve its format?
If I use to_timestamp(tm, 'DD.MM.YYYY HH24:MI:SS')::timestamp without time zone I still get it in other format 2021-11-15 11:07:27
I also tried to do double to_timestamps, but then an error function to_timestamp(timestamp with time zone, unknown) does not exist appears.
So the question is: Is there any way to convert from varchar type "15.11.2021 11:07:27" to timestamp type 15.11.2021 11:07:27 using select statement?
DbFiddle
PS. Even though I used it in DBFiddle, I cant change datestyle on the target server
Postgres version 13.5
EDIT: also, if I use cast(tm as timestamp) I receive date/time field value out of range: "15.11.2021 11:07:27" error.
You could try using:
CONVERT(varchar, timestamp, 103) AS YOUR_VALUE
This way you would convert timestamp without changing its original form.

Converting string to timestamp issues sql

I'm trying to cast a string to timestamp but I'm getting the following error:
Failed to output to file. Query failed: Value cannot be cast to timestamp: 2020-03-23T05:17:44.000Z
I'm using the query below:
select CAST(purchase_date AS timestamp)
from main_table
You can use from_iso8601_timestamp function if timestamp with time zone type is acceptable.
Or, you can use date_parse function.
Try parse_datetime():
select parse_datetime('2020-03-23T05:17:44.000Z', '%Y-%m-%dT%H:%i:%s.%fZ')

postgresql - datetime: does postgresql have datetime type like in mysql?

I know in mysql there is datetime type but couldn't find couterpart in postgresql. I know there is timestamp type in postgresql but that's a timestamp instead of datetime.
Thanks
A MySQL DATETIME is equivalent to a PostgreSQL TIMESTAMP.
Both store a date+time value in an undefined time line. They assume the time offset or time zone is of no interest, or is defined by the application.