Goal is to extract YYYY
from A.SCC_ROW_ADD_DTTM
with format of DateTm
Using peoplesoft Query Manager, adding an expression hoping to pull Year from a DateTm field.
Running a query on the DateTm field displays date in the following format: 03/19/2017 12:00:23PM
And so I tried
TO_CHAR(TO_DATE(A.SCC_ROW_ADD_DTTM, 'MM/DD/YYYY HH:MI:SSPM'), 'YYYY')
But keep getting the error not a valid month (50,380), but every date in the list starts with months in the format of XX (01-12). Full code below
SELECT A.ITEM_TYPE, B.DESCR, SUM( A.ITEM_AMT- A.APPLIED_AMT), TO_CHAR(TO_DATE(TO_CHAR(CAST((A.SCC_ROW_ADD_DTTM) AS TIMESTAMP),'YYYY-MM-DD-HH24.MI.SS.FF'), 'MM/DD/YYYY HH:MI:SSPM'), 'YYYY'), TO_CHAR(CAST((A.SCC_ROW_ADD_DTTM) AS TIMESTAMP),'YYYY-MM-DD-HH24.MI.SS.FF')
FROM PS_ITEM_SF A, PS_ITEM_TYPE_TBL B
WHERE ( B.ITEM_TYPE = A.ITEM_TYPE
AND ( A.ITEM_TYPE IN ('600000050010','600000050020','600000050030')
AND B.EFFDT =
(SELECT MAX(B_ED.EFFDT) FROM PS_ITEM_TYPE_TBL B_ED
WHERE B.SETID = B_ED.SETID
AND B.ITEM_TYPE = B_ED.ITEM_TYPE
AND B_ED.EFFDT <= SYSDATE) ))
GROUP BY A.ITEM_TYPE, B.DESCR, TO_CHAR(TO_DATE( TO_CHAR(CAST((A.SCC_ROW_ADD_DTTM) AS TIMESTAMP),'YYYY-MM-DD-HH24.MI.SS.FF'), 'MM/DD/YYYY HH:MI:SSPM'), 'YYYY'), A.SCC_ROW_ADD_DTTM
HAVING ( SUM( A.ITEM_AMT- A.APPLIED_AMT) > 0)
ORDER BY 1
I've tried changing the date format but get worse errors when changing from anything but what the expression is now, I just have no idea why it says the months are incorrect.
EDIT: I now notice that peoplesoft Query Manager automatically casts the field to a timestamp, there is no way to prevent this, and so I think there is no way to fix this.
The error, name and how you describe it suggest that
SCC_ROW_ADD_DTTM
is a DATE column, not VARCHAR2 - which is good, because you shouldn't store dates as strings.As it is already a date you should not do
TO_DATE(SCC_ROW_ADD_DTTM, ...)
or some of the other conversions you have now. Dates are stored with an internal format, and are converted to something human-readable either by an explicitTO_CHAR()
or automatically by your client. It looks like your client is showing the values as MM/DD/YYYY HI:MI:SSPM when you query the table. That does not mean the column itself has that (or any) format.You need to remove most of the conversions:
fiddle
I've also switched to modern join syntax, simplified the group by clause, etc.
If your column is actually TIMESTAMP then all the above applies, just also remove the
CAST()
call (fiddle). Even with DATE you could remove that and add the fixed.000000
or just.0
to the format model for display.You can also do
EXTRACT(YEAR FROM ...)
instead ofTO_CHAR(..., 'YYYY')
, which would give the result as a number instead of a string - depends which you want though.