Hello,
I want to get records between date range, using Oracle-PL/Sql.
Following is the query:
Select * From [Table_Name]
where (to_date(P_End_Date, 'DD/MM/YYYY') between to_date(FromDate, 'DD/MM/YYYY') and to_date(ToDate, 'DD/MM/YYYY'));
P_End_Date, FromDate and ToDate are of Varchar datatype
I am getting error in query as ORA - 01843:Not a valid month
P_End_Date column has Hypen(-) or Slash(/) in some records in my table, will it affect error in query ?
Thank you
Naimish MakwanaPosted Jan 31, 2023, 3:53 AM
Hi Sushant,
You can create a function to convert string to different formate like below:
then you can use this function in your query:
Reference : https://stackoverflow.com/questions/43776687/oracle-convert-many-date-formats-to-a-single-formatted-date
Thanks
Naimish
Sushant TorankarPosted Jan 30, 2023, 3:59 PM
Thank you Muhammad Imran Ansari for a reply.
I tried following solution, but still it I'm getting same error.
Maybe my date contains other than Slash or Hypen.
Any other to fix it ?
Thank you
Muhammad Imran AnsariPosted Jan 29, 2023, 5:41 PM
Hi Sushant,
The error "ORA-01843: not a valid month" is usually caused by a mismatch between the date format specified in the query and the actual date format in the data.
if some of the records in the P_End_Date column contain a slash (/) or a hyphen (-) instead of a forward-slash (/) as the date separator, the conversion will fail and the query will return the ORA-01843 error.
One way to solve this problem is to use the 'replace' function to replace any slashes or hyphens with forward-slashes before converting the strings to dates.