PeopleSoft Query and date prompts - sql

I have a PS Query and one of its prompts is ADM_CREATION_DT where the condition is:
This is a FROM date. All records starting from the selected date to the current date will be included in the report. Leaving this blank will default this to the current date.
I'm quite lost how to put this in the criteria.
part where Invalid datatype error happens

I think you can make a decode expression and make the date sysdate if the prompt value is empty.
decode(:1,NULL,sysdate,:1)
here are some search results for: ps query decode prompt sysdate
https://peoplesoftexperts.blogspot.com/2007/12/how-to-default-sysdate-current-date-for.html
https://it.toolbox.com/question/optional-prompt-for-date-in-ps-query-082912
http://www.psoftsearch.com/optional-prompting-ps-query/
Looking at the search results above, you may need to do a TO_CHAR on the date field. If so, I would look at other dates in the SELECT clause of the SQL generated by PS Query and use the same format they do.

Related

cx_Oracle.DatabaseError: ORA-01843: not a valid month

select distinct sko.CONTENTId,
sko.HELPDESKID,
sko.SEGMENTID,
som.SUBMITTED_FOR_NAME,
sko.SUBMITTEDDATE,
to_date(sko.LASTMODIFIEDDATE, 'DD-MM-RR')
from sky_know_obj sko
join sky_object_mass som
on sko.CONTENTId = som.CONTENTId
where sko.LASTMODIFIEDDATE > date'2019-11-03'
and sko.LASTMODIFIEDDATE <= date'2019-12-03'
This is my oracle sql query. i am running it in python. when I run this in the Oracle SQL Developer then it is giving results but whenever I tried to execute it in the pycharm the following error is occuring:
cx_Oracle.DatabaseError: ORA-01843: not a valid month
when I run
select * from nls_session_parameters;
this in oracle sql developer then its showing
NLS_DATE_FORMAT = DD-MM-RR
As you apply TO_DATE to LASTMODIFIEDDATE column and it fails, it seems that not all values have valid month in that column which means that its datatype isn't date but varchar2.
SQL Developer doesn't return all rows, but the first 50 (or so). If your query scans the whole table, or rows that weren't displayed initially, then it'll fail. Try to navigate to the last row in returned record set in SQL Developer to see what happens.
On the other hand, if column's datatype is date, then don't to_date it; there's no point in doing that.
Also, you shouldn't rely on database's settings. Take control over your data and don't specify string when meaning date. Use date literal, e.g.
where lastmodifieddate > date '2019-11-03' -- instead of '03-11-19'

How to Use Between Clause in Sql query without using and clause

I have created a query which gets results between 2 dates, Its working fine..
Now i have a scenario in which users does not enter any date..In this case
I need to fetch all the result from database regardless of date..
The following query fails when user does not enter any date, it returns
empty result set.
Kindly guide me how to resolve this problem.
select * from emp_register
where date between ' 2015-03-26 15:42:52' and ' 2015-03-26 15:42:52'
or status= 'Y'
Date Format is Timestamp
where date between ' 2015-03-26 15:42:52' and ' 2015-03-26 15:42:52'
' 2015-03-26 15:42:52' is NOT a DATE, it is a string. Never ever rely on implicit data type conversion. You might just be lucky to get the result depending on your NLS settings, however, it will certainly fail when the client NLS settings will be different for others.
Now i have a scenario in which users does not enter any date..In this case I need to fetch all the result from database regardless of date
No need to do in PL/SQL, do it in SQL.
Use NVL function on the values. Use a default lower bound and upper bound date values in the BETWEEN clause for the NVL.
For example,
SELECT *
FROM emp_register
WHERE DATE BETWEEN NVL(:date1, DATE '1900-01-01') AND NVL(:date2, DATE '9999-01-01')
OR status= 'Y'
So, whenever :date1 and/or :date2 and/is NULL, then it would use the default date values to fetch all the rows from the table.
There are couple of options:
Using Dynamic SQL to build your queries based on user input
Using multiple IF/ELSE statements to build your query based on user input
Setting default values for start and end dates that would always fit in your time range, such as 1900-01-01 and 2300-01-01 if user didn't pass any.

selecting data fom a view throws error

I get input as 2011/11/13 00:00:00. So I made the query as:
select * from xxcust_pfoa434p_vw
where week_ending_date = to_date(substr(:value,1,10),'YYYY/MM/DD')
The same statement gives proper result when queried against other tables. But throws error when I query this against the view xxcust_pfoa434p_vw
I have a view xxcust_pfoa434p_vw which has a column week_ending_date of date data type.
The value in that column is like 3/2/2014,12/25/2011 i.e. MM/DD/YYYY
Even
select * from xxcust_pfoa434p_vw where week_ending_date='3/2/2014'
also gives
ORA-01843: not a valid month. What is the cause for this error.
You say
"The same statement gives proper result when queried against other
tables. But throws error when I query this against the view
xxcust_pfoa434p_vw"
So clearly the problem is with the view. You also say
"[the view] has a column week_ending_date of date data type. The value
in that column is like 3/2/2014,12/25/2011 i.e. MM/DD/YYYY "
Those values would only display like that if the default date mask for you system were MM/DD/YYYY. This is easy enough to check with the query
select * from V$NLS_PARAMETERS
where parameter = 'NLS_DATE_FORMAT';
Personally, my money is on that column not being a date column. ORA-01841 always indicates oracle attempting to cast a string into a date and finding a value which doesn't fit the explicit or default format mask. Plus the so-called date '3/2/2014' lacks leading zeroes and that's suspicious too.
I think whoever wrote that view decided to fix the format of week_ending_date and so deployed TO_CHAR to present a string not a date datatype. A DESC in SQL*Plus or looking at the view TEXT in ALL_VIEWS will reveal the answer.
select * from xxcust_pfoa434p_vw
where week_ending_date=to_date('03/02/2014','MM/DD/YYYY');
Even if you see formatted date in this format - it is only a visual representation, when oracle process your query it automatically convers string given by you into its own interal representation.
It is always better to use proper SQL one YYYY-MM-DD:
for 2nd march: select * from xxcust_pfoa434p_vw where week_ending_date = to_date('2014-03-02', 'YYYY-MM-DD')
for 3rd february: select * from xxcust_pfoa434p_vw where week_ending_date = to_date('2014-02-03', 'YYYY-MM-DD')
this conforms to SQL standard and do not produce confusion between DD/MM/YYYY and MM/DD/YYYY
Just quote from standard:
There is an ordering of the significance of <datetime field>s. This
is, from most significant to least significant: YEAR, MONTH, DAY,
HOUR, MINUTE, and SECOND.
UPDATE: it is very good idea always use to_date function to specify exact format and avoid dependancy on any kind of localization settings

character_length Teradata SQL Assistant

I have to run column checks for data consistency and the only thing that is throwing off my code is checking for character lengths for dates between certain parameters.
SEL
sum(case when ( A.date is null or (character_length(A.date) >8)) then 1 else 0 end ) as Date
from
table A
;
The date format of the column is YYYY-MM-DD, and the type is DA. When I run the script in SQL Assistant, I get an error 3580 "Illegal use of CHARACTERS, MCHARACTERS, or OCTET_LENGTH functions."
Preliminary research suggests that SQL Assistant has issues with the character_length function, but I don't know how to adjust the code to make it run.
with chareter length are you trying to get the memory used? Becuase if so that is constant for a date field. If you are trying to get the length of the string representation i think LENGTH(A.date) will suffice. Unfortanatly since teradata will pad zeros on conversions to string, I think this might always return 10.
UPDATE :
Okay so if you want a date in a special 'form' when you output it you need to select it properly. In teradata as with most DBs Date are not store in strings, but rather as ints, counting days from a given 'epoch' date for the database (for example the epoch might be 01/01/0000). Each date type in teradata has a format parameter, which places in the record header instructions on how to format the output on select. By default a date format is set to this DATE FROMAT 'MM/DD/YYYY' I believe. You can change that by casting.
Try SELECT cast(cast(A.date as DATE FORMAT 'MM-DD-YYYY') as CHAR(10)) FROM A. and see what happens. There should be no need to validate the form of the dates past a small sample to see if the format is correct. The second cast forces the database to perform the conversion and use the format header specified. Other wise what you might see is the database will pass the date in a date form to SQL Assitant and sql assitant will perform the conversion on the application level, using the format specified in its own setting rather then the one set in the database.

Updating a date in Oracle SQL table

I am trying to update a date in a SQL table. I am using Peoplesoft Oracle. When I run this query:
Select ASOFDATE from PASOFDATE;
I get 4/16/2012
I tried running this query
UPDATE PASOFDATE SET ASOFDATE = '11/21/2012';
but it is not working.
Does anyone know how I would change the date to the one desired?
This is based on the assumption that you're getting an error about the date format, such as an invalid month value or non-numeric character when numeric expected.
Dates stored in the database do not have formats. When you query the date your client is formatting the date for display, as 4/16/2011. Normally the same date format is used for selecting and updating dates, but in this case they appear to be different - so your client is apparently doing something more complicated that SQL*Plus, for example.
When you try to update it it's using a default date format model. Because of how it's displayed you're assuming that is MM/DD/YYYY, but it seems not to be. You could find out what it is, but it's better not to rely on the default or any implicit format models at all.
Whether that is the problem or not, you should always specify the date model:
UPDATE PASOFDATE SET ASOFDATE = TO_DATE('11/21/2012', 'MM/DD/YYYY');
Since you aren't specifying a time component - all Oracle DATE columns include a time, even if it's midnight - you could also use a date literal:
UPDATE PASOFDATE SET ASOFDATE = DATE '2012-11-21';
You should maybe check that the current value doesn't include a time, though the column name suggests it doesn't.
Here is how you set the date and time:
update user set expiry_date=TO_DATE('31/DEC/2017 12:59:59', 'dd/mm/yyyy hh24:mi:ss') where id=123;
If this SQL is being used in any peoplesoft specific code (Application Engine, SQLEXEC, SQLfetch, etc..) you could use %Datein metaSQL. Peopletools automatically converts the date to a format which would be accepted by the database platform the application is running on.
In case this SQL is being used to perform a backend update from a query analyzer (like SQLDeveloper, SQLTools), the date format that is being used is wrong. Oracle expects the date format to be DD-MMM-YYYY, where MMM could be JAN, FEB, MAR, etc..
Just to add to Alex Poole's answer, here is how you do the date and time:
TO_DATE('31/DEC/2017 12:59:59', 'dd/mm/yyyy hh24:mi:ss')