Same query works fine in a certain database but throws error in different database - sql

I am trying to fetch the year from 'MON-YY' format and then concatenating the fetched year with '01-JUN' . I used to_date to forcefully convert 'string' ('01-JUN-17') to 'date' type
The below code works fine in a particular database for eg db_1
select to_date('01-JUN-'||(EXTRACT(YEAR FROM to_date('MAY-17','mon-yy'))) ) AS YEAR_START_DATE from dual;
returns :
01-JUN-17
But in db_2 the same code throws the below error :
ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
Can someone please help ?

'01-JUN-' || EXTRACT(YEAR FROM to_date('MAY-17','mon-yy'))
Will generate a string like '01-JUN-2017'
The problem occurs when you try to use TO_DATE( datestring, format_model ) without specifying a format model. In this case the query will use the NLS_DATE_FORMAT session parameter.
You can find the value for this using:
SELECT VALUE
FROM SYS.NLS_SESSION_PARAMETERS
WHERE PARAMETER - 'NLS_DATE_FORMAT';
If this does not match the format you specify then it will raise an exception.
You should explicitly specify the format model (and the language):
SELECT TO_DATE(
'01-JUN-'||EXTRACT(YEAR FROM to_date('MAY-17','mon-yy','NLS_LANGUAGE="ENGLISH"')),
'dd-MON-YYYY',
'NLS_LANGUAGE="ENGLISH"'
) AS YEAR_START_DATE
FROM DUAL;
Or you could use:
SELECT ADD_MONTHS(
TRUNC(
TO_DATE( 'MAY-17', 'MON-YY', 'NLS_LANGUAGE="ENGLISH"' ),
'YY'
),
5
) AS YEAR_START_DATE
FROM DUAL;

You are missing a date format for your first to_date('01-JUN-'..) function. The date format for one database is probably DD-MON-YYYY but it could be DD-MM-YYYY in the other database.
After adding the date format your query looks like this:
select to_date('01-JUN-' || (extract(year from to_date('MAY-17', 'mon-yy'))),'DD-MON-YYYY') as year_start_date
from dual;
It's good practise to always specify a format when converting dates from and to strings.

Related

Getting a date format error while executing

ORA-01840: input value not long enough for date format
01840. 00000 - "input value not long enough for date format"
*Cause:
*Action:
SELECT TO_DATE (
TO_CHAR (TO_DATE (attribute39, 'MM/DD/YYYY'), 'DD/MM/YYYY'),
'DD/MM/YYYY') AS "PO Valid To Date"
FROM table;
Want to execute the query without error,
attribute39 is date formate in mm/dd/yyyy and varchar(250)
TO_DATE supports 'DEFAULT return_value ON CONVERSION ERROR' and if you prefix the date format with 'FX' you will be able to detect rows where attribute39 is not exactly compliant to your expectation:
TO_DATE (attribute39, DEFAULT to_date('01/01/0001','dd/mm/yyyy') ON CONVERSION ERROR, 'FXMM/DD/YYYY')
You could put NULL as DEFAULT if you don't have it as possible value for attribute39, if not selecting a value you are sure is not in your data makes easier to detect rows with invalid attribute39.
You may get ORA-01840 if you have strings where the year is only two digits (meaning from 1950 to 2049).
You could also run a query with a regex to detect unexpected values in attribute39.
select distinct(col1)
from Customer_flex_attr_value
where regexp_like (col1, '([0-9][0-9]|3[0-1])/([0-9]|[0-9]{2})/[0-9]{4}')
order by 1 desc;
This would bring different date format

Convert varchar/timestamp col to Date field

I have a varchar2 datatype field (lmp_date) that can return either null or what looks like a timestamp value. Changing the database data_type to DATE isn't a possibility, so now I'm needing to convert this to a date, but with the values this column returns, I'm having some problems.
Returned values for lmp_date =
null or 2021-06-11-00.00.00
Date format needed: MM/DD/YYYY
I've tried cast, convert, substr+instr to no avail
ETA - A couple example attempts (because there have been 10+:
select order_no, to_date(lmp_date) lmp_date from table_a - with error message of 'ORA-01861: literal does not match format string'
select order_no, to_date(substr(lmp_date, 1, instr(lmp_date, '00' -15))) lmp_date from table_a - since lmp_date has null value possibilities, this doesn't work successfully
select order_no, cast(lmp_date as date) lmp_date from table_a - with same error message of 'ORA-01861: literal does not match format string'
select order_no, to_date(lmp_date, 'YYYY-MM-DD') lmp_date from table_a - ORA-01830: date format picture ends before converting entire input string
There have been more attempts, this is all I can remember
To convert a string to a date, use the to_date() function with a suitable format mask:
to_date(lmp_date, 'YYYY-MM-DD-HH24:MI:SS')
The format model elements are in the documentation.
The result of that is a date data type, which is an internal 7-byte representation. Your client or application will format that for display, which may be based on your NLS_DATE_FORMAT setting, so you can modify that to change hot all dates are displayed; or use to_char() to convert the date back to a string, e.g.:
to_char(to_date(lmp_date, 'YYYY-MM-DD-HH24:MI:SS'), 'MM/DD/YYYY')
although if you want it as that string you can just use string manipulation with substr() and concatenation:
case when lmp_date is not null then
substr(lmp_date, 6, 2) || '/' || substr(lmp_date, 9, 2) || '/' || substr(lmp_date, 1, 4)
end
db<>fiddle
When you do either of these:
to_date(lmp_date)
cast(lmp_date as date)
this also relies on your session NLS_DATE_FORMAT; and the "literal does not match format string" error indicates that it doesn't match the string, e.g. if you have the still-default 'DD-MON-RR' setting. It would actually work - for you in your current session - if you changed that setting. I've shown that here just for info. But to work for anyone regardless of their session settings, you should use to_date() with an explicit format mask, and don't rely on or assume anything session-specific.
You were nearly there with:
to_date(lmp_date, 'YYYY-MM-DD')
and again the "date format picture ends before converting entire input string" message tells you what is wrong - your string carries on past the YYYY-MM-DD elements. Expanding the format mask to match all of the string, as I did above, means it knows what each part means.
If you were really only interested in the date part then you could cut the end off the string:
to_date(substr(lmp_date, 1, 10), 'YYYY-MM-DD')
but that's only really useful if you have a mix of string values where some have times and some do not. (The resulting date will always have a time; it will just be midnight.) And if you have dates with different formats then it gets a bit complicated - partly why you shouldn't store dates as strings.

format date in oracle SQL

I was trying to learn how to format date in oracle pl oracle, when I ran below query its returns error
SELECT TO_DATE('01-JAN-00', 'YYYY-DD-MM') FROM dual;
the error message is
ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
You are either not using the correct format specifier, or not passing the correct string. You want:
SELECT TO_DATE('2000-01-01', 'YYYY-DD-MM') FROM DUAL;
Or:
SELECT TO_DATE('01-JAN-00', 'DD-MON-YY') FROM DUAL;
Or you can simply declare a DATE litteral:
SELECT DATE'2000-01-01' FROM DUAL;
for my scenario I had to use to_char which perfectly solve the formatting issue.
SELECT TO_CHAR('01-JAN-00', 'yyyy-DD-MM') FROM dual;

Convert VARCHAR to Time in oracle

I am using ORACLE and I want to convert VARCHAR type into Time using following SQL Query
SELECT CUSTOMER_SERVICE_QUOTE_MEAS.ITEM_CREATE_TM,
TO_char(
CUSTOMER_SERVICE_QUOTE_MEAS.ITEM_CREATE_TM,
'hh24:mi:ss')
AS TIME_CHANGE
FROM FDC.CUSTOMER_SERVICE_QUOTE_MEAS CUSTOMER_SERVICE_QUOTE_MEAS
This results in 6/1/2013 11:32:02 AM but I don't want the date part here. I need only time portion 11:32:02, How it can be done ?
The field CUSTOMER_SERVICE_QUOTE_MEAS.ITEM_CREATE_TM has the data which look like this 113202.
Any help will be appreciated, Thanks.
Is the datatype of the item_create_tm column a NUMBER? The to_char is expecting a date column. To replicate the error:
SELECT to_char(113202, 'hh24:mi:ss') FROM dual;
It generates this error.
ORA-01481: invalid number format model
01481. 00000 - "invalid number format model"
*Cause: The user is attempting to either convert a number to a string
via TO_CHAR or a string to a number via TO_NUMBER and has
supplied an invalid number format model parameter.
*Action: Consult your manual.
You could either transform it to a date first and then to a formatted char.
SELECT to_char(to_date(113202, 'hh24miss'), 'hh24:mi:ss') FROM dual;
Output
11:32:02
Or substring the number to a char with : separators.
SELECT substr(113202, 1, 2) || ':' || substr(113202, 3, 2) || ':' || substr(113202, 5, 2) FROM dual;
Output
11:32:02
you can wrap dates with to_char and a format something like this:
select to_char( mydate, 'hh24:mi:ss' ) as mytime from mytable;

Asking User to input date in sql giving ORA-00932: inconsistent datatypes: expected DATE got NUMBER Error

I am trying to input a date value from the user and then using that value in the query.
select * from TB_MNP_GTY_TRANS_STEPS where CREATE_DATETIME>=&startdate
Now when i run the sql statement in Toad and input 8/1/2012 as date data type i am getting
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
Can someone suggest where i am wrong.Note that CREATE_DATETIME is of Date Type.
You should really specify what date format you are using in your parameter:
SELECT *
FROM TB_MNP_GTY_TRANS_STEPS
where CREATE_DATETIME >= TO_DATE(&startdate, 'DD/MM/YYYY');
Read about date formats here
Currently your session is expecting the date to be in its default NLS_DATE default fomat and obviously the format of the date you're entering is different.
Explicitly specifying date formats prevents this issue from occurring.
Hope it helps...
EDIT:
If you want to pass in the 8th January 2012 then you could specify your variable value as:
08/01/2012
And your select would be:
SELECT *
FROM TB_MNP_GTY_TRANS_STEPS
where CREATE_DATETIME >= TO_DATE(&startdate, 'DD/MM/YYYY');
Depending upon your environment you might need to wrap the variable in single quotes (for TOAD you definiely will) i.e.
SELECT *
FROM TB_MNP_GTY_TRANS_STEPS
where CREATE_DATETIME >= TO_DATE('&startdate', 'DD/MM/YYYY');
The error you are getting is caused by the format of the date string you are entering not matching EXACTLY the format you are specifying (see the leading "0" before the 8 and 1 in the day and month!)
Date casting necessary
select * from TB_MNP_GTY_TRANS_STEPS where CREATE_DATETIME>=to_date(&startdate, 'MM-DD-YYYY')
and while passing parameter you should pass value in quoets as '08-09-1999'