java dateformat with oracle format gives ORA-01722: invalid number - sql

The date field in oracle DB is stored in '10-FEB-99' format. My calander function in UI accepts date in this format '02/30/2023'.
When i search date in UI, i have to query the DB with date field from UI to what exists in DB.
select * from case where TO_CHAR(p.OCCASION_FROM_DATE,'dd-MON-yyyy') = TO_CHAR('02/10/1999','MM/dd/yyyy');
When I execute this query I get below exception
**ORA-01722: invalid number
00000 - "invalid number"
Cause: The specified number was invalid.
Action: Specify a valid number.
Tried this
select * from case where TO_CHAR(p.OCCASION_FROM_DATE,'dd-MON-yyyy') = TO_CHAR('02/10/1999','MM/dd/yyyy');

The date field in oracle DB is stored in '10-FEB-99' format.
Is it? Perhaps. It depends on column datatype. What is it? If it is VARCHAR2, then yes - it might be stored that way, and that's wrong. You should always store dates into DATE datatype columns.
Let's presume it is a varchar2 column. In that case, you should match two values: first convert a string (02/10/1999) to a valid date value (using to_date with appropriate format model), and then back to string - again with format model that matches string stored into the column:
select * from case p
where p.OCCASION_FROM_DATE =
to_char(to_date('02/10/1999','MM/dd/yyyy'), 'dd-mon-rr', 'nls_date_language = english);
On the other hand, if values stored in table are in date datatype column (yes, that should be the case!), note that dates aren't stored in human-readable format. That's a 7-byte value representing century/year/month/day/hour/minute/second. The fact that you see it as 10-feb-99 is because your current NLS settings say so.
Query then gets somewhat simpler because you just have to convert string 02/10/1999 to date:
select * from case p
where p.occasion_from_date = to_date('02/10/1999', 'mm/dd/yyyy')

Related

SQL - Date format conversion issue (AUTO to 'MM/DD/YYYY')

In Snowflake, I have a column in a created table called "Date1", that has dates formatted as AUTO (ex. 2022-06-17). I am trying to query that column, but need the date formatted as 'MM/DD/YYYY', yet everything I've tried returns an error of some kind.
When I try date(Date1, 'MM/DD/YYYY) the error says that it can't parse 2022-06-17 for MM/DD/YYYY. When I try to_date(Date1 [MM/DD/YYYY]) or try_to_date(Date1 [MM/DD/YYYY]) the error says it doesn't recognize MM.
Any thoughts?
If you're trying to display the date using a specific format, you're converting to a varchar rather than a date:
select to_varchar(current_date, 'MM/DD/YYYY');
If you're trying to compare a column with a date to a formatted string in MM/DD/YYYY format then:
select current_date = try_to_date('08/04/2022', 'MM/DD/YYYY');
You should try to provide correct format to match 2022-06-17 literal:
SELECT TRY_TO_DATE(Date1, 'YYYY-MM-DD')
FROM tab_name;
Your column is already of type DATE. TO_DATE() and TRY_TO_DATE() convert non-date formats (string, integer) to a DATE type. They are not a means to format your DATE for presentation.
Date data type and presentation format are indepent.
You can alter your session to change the default display format of a date, but the underlying representation in the database remains the same.
alter session set DATE_INPUT_FORMAT='MM/DD/YYYY';
alter session set DATE_OUTPUT_FORMAT='MM/DD/YYYY';
select <col_name> from table; -- Now will show as MM/DD/YYYY for date columns

I have an oracle table which has date in dd-mm-yyyy and dd/mm/yyyy format in same field. Now i have to convert into one common format

I have an oracle table which has date in dd-mm-yyyy and dd/mm/yyyy format in same field. Now i have to convert into one common format.
Please suggest how to approach this?
I did tried but it is failing as it is failing due to invalid month.
Is there a way i can first identify what format the date is and then based on case statement i might convert.
or something easy way? Please
I trust you've learnt your lesson and you're now going to store these dates in the date data type.
Your two different date formats actually aren't important, Oracle already is a little over accepting when it comes to separating characters.
e.g
to_date('01/01/1900','dd-mm-yyyy')
Does not error
I did tried but it is failing as it is failing due to invalid month.
Your error is coming because you've allowed a value that doesn't match either of those formats into your string column.
If you are on version 12.2 at least (which you should be in 2020) then you can use the validate_conversion function to identify rows that don't convert to a date with your format (https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/VALIDATE_CONVERSION.html#GUID-DC485EEB-CB6D-42EF-97AA-4487884CB2CD)
select string_column
from my_table
where validate_conversion(string_column AS DATE,'dd/mm/yyyy') = 0
The other additional helper we got in 12.2 was the on conversion error clause of to_date. So you can do.
alter table my_table add my_date date;
update my_table set my_date = to_date(my_string default null on conversion error,'dd/mm/yyyy');
If you are confident that there is no other format than those two, a simple approach is replace():
update mytable set mystring = replace(mystring, '/', '-');
This turns all dates to format dd-mm-yyyy.
I would suggest taking a step forward and convert these strings to a date column.
alter table mytable add mydate date;
update mytable set mydate = to_date(replace(mystring, '/', '-'), 'dd-mm-yyyy');
This will fail if invalid date strings are met. I tend to consider that a good thing, since it clearly signals that this a problem with the data. If you want to avoid that, you can use on conversion error, available starting Oracle 12:
to_date(
replace(mystring, '/', '-') default null on conversion error,
'dd-mm-yyyy'
)
Then you can remove the string column, which is no longer needed.

Varchar2 to date in Oracle

Can somebody help me with how to convert varchar column into Date data type?
My createDate column has VarChar2 data type in format example 20050923
I would like to convert this column into format example 2005/09/23
ALTER TABLE test ADD (new_create_date DATE);
UPDATE test SET new_create_date=TO_DATE(createDate,'MM/DD/YYYY');
When I run these sql I get format 23-aug-2005. Why?
Internally date is stored as a 7 byte value. Formatting is not something that is stored there. You get formatting when you pull the data out of the table.
Try this.
SELECT TO_CHAR(NEW_CREATE_DATE,'MM/DD/YYYY')
FROM TEST
You could do this:
update test
set new_create_date to_date(createDate, 'YYYYMMDD');
You just need to give to_date() the right format string.
This is because your database NLS_DATE_FORMAT must be in DD-MON-YYYY format, you can check that by using the below mentioned query.
SELECT *
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT';
Also by definition to_date(x,format) converts a string i.e. x to a datetime, the format part is an optional part which indicates the format that is present for the string value x.
You can further use to_char(x,format) function in your case which converts the x datetime to a string as mentioned by the EvilTech.

Ms Access - How to verify the date time format displayed in yyyy/mm/dd HH/MM/SS

May I know could I verify whether the date displayed in the field is in specific format
As per my requirement, the datetime should be displayed in format 'yyyy/mm/dd HH(24hr)/MM/SS'
Eg: The valid value should be '2014/07/18 14:16:48'. If the date is displayed as '18/07/2014 14:16:48', then it is invalid.
Using query how I verify whether it is shown in the format which I have expected. I could use IsDate option to verify it is a valid date and also I could use Mid function to verify the date separator which is '/', but how could I verify the format.
Thanks
If the column is stored as Text, use the SQL Like operator. Select valid dates:
SELECT * FROM myTable WHERE myDate Like "####/##/## ##:##:##"
Select invalid dates
SELECT * FROM myTable WHERE myDate Not Like "####/##/## ##:##:##"
# stands for a single digit.
See Like Operator.
But note that this only makes sense if myDate is a Text! If the type of myDate is Date/Time, the date is stored in a numeric format internally and is only formatted as a date for display. So don't confuse the date value per se and the date as it is displayed.
A Date/Time is always stored in the same way, no matter how it is formatted and displayed!
All you need to do is to open the Table in Design View and to set the Format property.

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