Amazon Redshift to PostgreSQL query conversion; DATEDIFF clause error [duplicate] - sql

This question already has answers here:
How to get difference of days/months/years (datediff) between two dates?
(10 answers)
how to resolve function datediff(unknown, timestamp without time zone, timestamp without time zone) does not exist
(2 answers)
Closed 8 months ago.
I have a large query written by another party for Amazon Redshift. Data has moved to a postgreSQL server and I would like to use the same query but I am getting an error. I understand that DATEDIFF does not work in postgreSQL and I should be using DATE_PART, but I still can't convert this particular clause. I am very new to postgreSQL syntax and have tried to fix this on my own, but can't get anything I do to completely work. Any help would be appreciated, thanks!
CLAUSE:
assignment_submissions AS (
SELECT submissions.*,
CASE
WHEN assignment_due_at IS NOT NULL
AND assignment_due_at <
COALESCE(submissions.submitted_at, CURRENT_TIMESTAMP::TIMESTAMP)
THEN
COALESCE(
NULLIF(
DATEDIFF(‘DAY’, assignment_due_at,
COALESCE(submissions.submitted_at, CURRENT_TIMESTAMP::TIMESTAMP)
), 0),
1)
END AS late_days
FROM submissions
WHERE assignment_type = 'a'
),
Here is the specific error: An error occurred while communicating with PostgreSQL Error Code: 975DF5A2 ERROR: function datediff(unknown, timestamp without time zone, timestamp without time zone) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts. Position: 5503

Related

Filter by a calculated colum in SQL Oracle [duplicate]

This question already has answers here:
How to use Alias in Where clause? [duplicate]
(1 answer)
Using an Alias in a WHERE clause
(5 answers)
Closed 9 months ago.
I am trying to SELECT columns for which the difference in days exceeds 50.
However I am unable to do so. Find below my query:
SELECT name_,
client_p as Client,
to_date(first_date)-to_date(last_day) as difference
FROM table1.mydata
where difference>50
any insights on this?
Thank you.
You can't reference it that way; either use this
SELECT name_,
client_p AS client,
TO_DATE (first_date) - TO_DATE (LAST_DAY) AS difference
FROM table1.mydata
WHERE TO_DATE (first_date) - TO_DATE (LAST_DAY) > 50;
or - with your query as a CTE (or a subquery):
WITH
temp
AS
(SELECT name_,
client_p AS client,
TO_DATE (first_date) - TO_DATE (LAST_DAY) AS difference
FROM table1.mydata)
SELECT *
FROM temp
WHERE difference > 50;
Note that you should store dates as DATEs, not strings - which is what TO_DATE function suggests.
But, if you do store them as strings, then you should provide format model to the TO_DATE function. It is unknown which format you do have, but - you should know it.
On the other hand, if you store dates as you should (in DATE datatype columns), then remove TO_DATE from your query.
You can't use an alias to filter right away. Either make a subquery and select from that using the alias or change to:
SELECT name_,
client_p as Client,
to_date(first_date)-to_date(last_day) as difference
FROM table1.mydata
where to_date(first_date)-to_date(last_day) > 50

Compare Columns Date /w Todays Date [duplicate]

This question already has answers here:
DATEDIFF function in Oracle [duplicate]
(4 answers)
Closed 3 years ago.
I'm trying to compare the column: LastUpdated with todays date in days, rounded to 1 decimal place. I keep getting the error
ERROR at line 4:
ORA-00904: "DATEDIFF": invalid identifier
Any ideas?
SELECT
DISTINCT "AppName",
"ApprovedForRelease",
DATEDIFF(DAY,"LastUpdated",GETDATE()) AS "DaySinceUpdated"
FROM BR_APP
WHERE "ApprovedForRelease" = 'Y';
In Oracle, you can use subtraction. To get the dates between, truncate off the time:
SELECT DISTINCT "AppName", "ApprovedForRelease",
(TRUNC(sysdate) - TRUNC("LastUpdated")) AS "DaySinceUpdated"
FROM BR_APP
WHERE "ApprovedForRelease" = 'Y';
The code you have used is based on SQL Server.

How to get difference between 2 dates in PostgreSQL with TIMESTAMPDIFF? [duplicate]

This question already has answers here:
Difference between two dates in postgresql
(1 answer)
Difference in years between two dates
(1 answer)
How to get difference of days/months/years (datediff) between two dates?
(10 answers)
Closed 4 years ago.
I'm trying to get it as:
SELECT * FROM "Employee" WHERE TIMESTAMPDIFF('YEAR', "BirthDate", "HireDate");
but I get the next error:
ERROR: function timestampdiff(unknown, timestamp without time zone, timestamp without time zone) does not exist
LINE 1: SELECT * FROM "Employee" WHERE TIMESTAMPDIFF('YEAR', "BirthD...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
I've found another way to get difference between 2 dates but in my instructions it says that I have to get it via TIMESTAMPDIFF.
Can someone help me where to look and how to fix my error?
you can use minus operator
EXTRACT(DAY FROM (HireDate)-(BirthDate)) AS DateDifference
Example select date '2001-10-01' - date '2001-09-28'
DEMO IN FIDDLE
PostGrey Docs

Cast timestamp to integer in Redshift

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

Query having issues while taking Date and String parameters [duplicate]

This question already has an answer here:
Oracle "Invalid Number" caused by to_char function
(1 answer)
Closed 6 years ago.
I have created a sql query in which I'm passing a parameter to the section shared below
BETWEEN
NVL(TO_CHAR(:DATE1, 'DD-MON-YYYY'), DATETIME)
AND
NVL(TO_CHAR(:DATE2, 'DD-MON-YYYY'), DATETIME)
When I run this query with the data type it works fine but when I run this query with VARCHAR2 parameter it gives the following error:
[Error] Execution (9: 25): ORA-01722: invalid number
When I run this query with null, the query shows up all the records.
actually I need to run this query using a STRING parameter not with the DATE dataype so when I pass the parameter to the same query in toad it will work fine even with the null values.
Here is my complete query:
SELECT rownum,ACCOUNT_NO,CUSTOMER_NAME,CARD_NO, SOURCE, ATM_ID, ISSUER_BANK_NAME,ASE.STATUS_DESC, CARD_TYPE, CARD_RESP,
DATETIME,BR_INPUT_DATE BR_ACTIVITY
FROM ATM_RCCM, ATM_STATUS_ERRORS ASE
where ASE.STATUS_DESC = NVL(:PT, ASE.STATUS_DESC)
AND BR_TRACKING_STATUS = ASE.STATUS_CODE
AND CARD_TYPE = NVL(:CT, CARD_TYPE)
AND DATETIME
BETWEEN
NVL(TO_CHAR(:DATE1, 'DD-MON-YYYY'), DATETIME)
AND
NVL(TO_CHAR(:DATE2, 'DD-MON-YYYY'), DATETIME)
Also note that the DATETIME column has the default datatype of VARCHAR2.
Elementary dear Watson: TO_CHAR converts dates to varchar2. Why do you expect it to work if you give it the wrong data type? When you give it a varchar2 input, it will (stupidly in my opinion) try to convert it to a date first, instead of throwing a compilation error. It will use your session's NLS_DATE_FORMAT though, NOT the format model you have in TO_CHAR. So in your case, it can't and it throws a runtime error.
The comparison is wrong anyway; it is pretty clear you want to compare dates, not strings. So why are you converting the dates to strings first, and then compare? With string comparisons, 12-JAN-2012 is before 9-MAR-1993.
Did you just mean to use TO_DATE, not TO_CHAR? Give it a try and see what happens! Just make sure you wrap DATETIME within TO_DATE too, with the appropriate format model.
It has to be an issue with the parameters you are entering. Likely your :DATE1 or :DATE2 are in the incorrect format, or you may have an alpha character in CT or PT