Getting error when converting timestamp column to time - amazon-redshift

I am getting the following error message, when trying to convert timestamp column to time.
Error message:
SQL Error [500310] [0A000]: [Amazon](500310) Invalid operation: Specified types or functions (one per INFO message) not supported on Redshift tables.
Here is my sql snippets I have been using to convert the column over, but all give me the above error:
SELECT pg_catalog.time(timestamp) AS myTime from tableA.time
SELECT "time"(timestamp) AS myTime from tableA.time
SELECT to_char(cast(timestamp as "time")) from tableA.time
select timestamp::time from tableA.time
What has worked for me but what I do not need, since I need to run mathematical operations on the time, is below the sql scripts.
SELECT
EXTRACT(HOUR FROM timestamp) AS HOUR,
EXTRACT(MINUTE FROM timestamp) AS MINUTE,
EXTRACT(SECOND FROM timestamp) AS SECOND
FROM tableA.time;
is there any way I could check the properties of the column to see if the reason the column is not converting to time? I have tried simple lines like this (select '2016-10-01 12:12:12'::time) that work perfectly fine not sure why the whole column is not converting over. If anyone could provide me some help that would be great!
thank you in advance.
-EDIT-
I looked at the column with the timestamp and the type of the column is "timestamp without time zone". Maybe that is what is causing the error.

Related

discarding milliseconds from the average time calculated

Here is my code:
SELECT cluster_id, AVG(min_per_imei_cluster::TIME) as avg_time
FROM arrival
group by cluster_id
The avg_time column gives values with milliseconds and beyond. How do I truncate or round to the nearest second?
I tried using
AVG(min_per_imei_cluster::TIME)::timestamp(0)
but got the following error:
SQL Error [42846]: ERROR: cannot cast type interval to timestamp without time zone
It looks like you can use the date_trunc function to specify the level of precision that you want. So in this instance, you would do date_trunc('second',min_per_imei_cluster).

How to transfer specific number to date in PostgreSQL?

Need to transfer numeric columns with numbers like '201 711' and '201 806' to dates like '2017-11-01' and '2018-06-01' ('YYYY-MM-01'). When I try this
select to_date(debt_max_period,'YYYY-MM') as date1 from debt;
I get
SQL Error [42883]: ERROR: function to_date(numeric, unknown) does not
exist.
Will be thankful for any ideas!
You need to cast the number to a text/varchar value. As the number doesn't contain the - character, you also need to remove that from your format mask.
to_date(debt_max_period::text, 'yyyymm')
Oracle -
select TO_CHAR(TO_DATE(START_DATE,'YYYYMMDD'),'YYYY-MM-DD') STARTDATE from account
You can use like below as well.
Postgresql -
select TO_CHAR(TO_DATE(START_DATE::text,'YYYYMMDD'),'YYYY-MM-DD') STARTDATE from account

Keeping only date in a timestamp while joinng to date in another table

I am working with some tricky data, and am trying to join a timestamp to a date while getting rid of the timestamp all together and just keeping the date. I have tried a few different methods, but this is the most recent:
convert(row_add_ts, convert(date, current_timestamp)) as Row_add_ts
this is what the data looks like currently:
2017-01-01 00:00:08
this is how I want it to look:
2017-01-01
The join that I currently have looks like:
Left outer join Table 2 b
on a.row_adds_ts = b.Table_date
I keep getting different errors, but the most recent is:
[Code: -206, SQL State: 42703] DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=DATE, DRIVER=4.19.49. 2) [Code: -514, SQL State: 26501] DB2 SQL Error: SQLCODE=-514, SQLSTATE=26501, SQLERRMC=SQL_CURLH200C1, DRIVER=4.19.49
It looks like it is failing to convert the timestamp data type over to a date data type. I'm not sure how to proceed from here.
I have also tried:
left(cast(row_adds_ts as date), 10)
to no prevail.
So it looks like the below query worked to give me what I was looking for:
Date(row_add_ts)

Convert Time Zone in Amazon Redshift

I have a datetime column in a redshift table. It contains date in UTC format.
So, when I am querying based on time_zone like UTC+5:30/ UTC+4:30, I want to convert the table datetime column to chosen time_zone and proceed further. It gives me wrong result.
Using this method :
CONVERT_TIMEZONE ( ['source_zone',] 'target_zone', 'timestamp')
Query Type 1: Wrong input, correct answer
SELECT id, convert_timezone('UTC+5:30','UTC', date) as converted_time, ingest_date
FROM table_name
WHERE conditions
Query Type 2: Correct input, wrong answer -> It again subtracting 5:30 from the date in column
SELECT id , convert_timezone('UTC','UTC+5:30',ingest_date) as converted_time, ingest_date
FROM table_name
WHERE conditions
Query Type 3: Wrong input, correct answer
SELECT id, convert_timezone('UTC','UTC-5:30',ingest_date) as converted_time, ingest_date
FROM table_name
WHERE conditions
How to convert / parse the UTC column into selected timezone?
In Redshift, CONVERT_TIMEZONE interprets the offset as the time from UTC. For example, an offset of +2 is equivalent to UTC–2, and an offset of -2 is equivalent to UTC+2. CONVERT_TIMEZONE does not use the prefix string when calculating the offset, even if the string represents a valid time zone. For example, 'NEWZONE+2’, 'PDT+2', and'GMT+2' all have the same result. If a string does not include an offset, then it must represent a valid time zone or CONVERT_TIMEZONE returns an error.
For converting time_zone, if you send "UTC+5:30/UTC-4:30", amazon interpreting it as "UTC-5:30 / UTC+4:30".
Now you can convert + into - and vice versa before sending it to redshift.
(http://docs.aws.amazon.com/redshift/latest/dg/CONVERT_TIMEZONE.html)

PostgreSQL does not order timestamp column correctly

I have a table in a PostgreSQL database with a column of TIMESTAMP WITHOUT TIME ZONE type. I need to order the records by this column and apparently PostgreSQL has some trouble doing it as both
...ORDER BY time_column
and
...ORDER BY time_column DESC
give me the same order of elements for my 3-element sample of records having the same time_column value, except the amount of milliseconds in it.
It seems that while sorting, it does not consider milliseconds in the value.
I am sure the milliseconds are in fact stored in the database because when I fetch the records, I can see them in my DateTime field.
When I first load all the records and then order them by the time_column in memory, the result is correct.
Am I missing some option to make the ordering behave correctly?
EDIT: I was apparently missing a lot. The problem was not in PostgreSQL, but in NHibernate stripping the milliseconds off the DateTime property.
It's a foolish notion that PostgreSQL wouldn't be able to sort timestamps correctly.
Run a quick test and rest asured:
CREATE TEMP TABLE t (x timestamp without time zone);
INSERT INTO t VALUES
('2012-03-01 23:34:19.879707')
,('2012-03-01 23:34:19.01386')
,('2012-03-01 23:34:19.738593');
SELECT x FROM t ORDER by x DESC;
SELECT x FROM t ORDER by x;
q.e.d.
Then try to find out, what's really happening in your query. If you can't, post a testcase and you will be helped presto pronto.
try cast your column to ::timestamp like that:
SELECT * FROM TABLE
ORDER BY time_column::timestamp