Convert VARCHAR to Time in oracle - sql

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;

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

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;

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

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.

Converting dates stored as VARCHAR2 to a date

I have seen similar posts to this, but I am not able to resolve my query.
I am trying to query a table that has a column ("VALUE") of VARCHAR2 datatype.
The rows in this column are mixed with both numerical and date values (I do not know why the dates were stored as VARCHAR2).
I only need the dates and I have filtered off the rows with LIKE function.
SELECT
PARENTID,
NAME,
VALUE
FROM TIMINGEVENT
WHERE NAME like 'last%'
;
Now the column only has the dates and I need to convert from VARCHAR2 to date.
PARENTID ++ NAME ++ VALUE
1701480 ++ lastCycle1 ++
1701480 ++ lastCycle2 ++
1701480 ++ lastCycle3 ++ 20150901092520 AM
1701480 ++ lastCycle4 ++ 20150901092834 AM
1701480 ++ lastCycle5 ++ 20150901085047 AM
My attempts to use TO_DATE resulted in the following error:
ORA-01858: a non-numeric character was found where a numeric was expected
I am using Oracle 11g SQL Developer and the NLS preferences for date format is set to DD-MON-RR.
I found the below approach in another post, but when I use it it throws the below error?
SELECT
PARENTID,
NAME,
VALUE,
TO_CHAR(TO_DATE(VALUE, 'MM/DD/YYYY'), 'MM/DD/YYYY') AS "test"
FROM TIMINGEVENT
WHERE NAME like 'last%'
;
ORA-01843: not a valid month
01843. 00000 - "not a valid month"
It seems that you only need the right format:
with test(parentId, name, value) as (
select '1701480','lastCycle1','' from dual union all
select '1701480','lastCycle2','' from dual union all
select '1701480','lastCycle3','20150901092520 AM' from dual union all
select '1701480','lastCycle4','20150901092834 AM' from dual union all
select '1701480','lastCycle5','20150901085047 AM' from dual
)
select to_date(value, 'YYYYMMDDHHMISS AM')
from test
When you use a format specifier inside to_date(), what it does is to try to map the value in the column exactly in the format that has been as format specifier, not something less, not something more.
So, When you use TO_DATE(VALUE, 'MM/DD/YYYY') it tries to map the first 2 characters ie, 20 as MM. Hence it is giving the error as not a valid month.
You need a proper format specifier to deal with the column, as one is shown below -
SELECT TO_DATE(VALUE, 'yyyymmddhhmiss am') FROM TIMINGEVENT
It will give output like -
9/1/2015 9:25:20 AM
Later you can again format the output of this as per your requirement by using to_char and again using a proper format specifier.
SELECT to_char(TO_DATE(VALUE, 'yyyymmddhhmiss am'),'DD-MON-YYYY HH12:MI:SS AM') FROM TIMINGEVENT
This will give an output like -
01-SEP-2015 09:25:20 AM
Please note, it does not matter is your value contains am or pm in it, you can use both am or pm. I mean, ironically it is not mandatory to use 'am' if the value contains 'am' in it. You can use 'pm' too even if the value contains 'am'. Not that it makes much sense in using it that way, still just an FYI.
First understand the usage of to_date and to_char functions:
TO_CHAR:
To change other datatypes like date or number to string, if you are changing a date to character, then you must specify the date format to which it should be converted.
TO_DATE
To change string/char to date, the second parameter is date format of your string in your case, the date format of the string "20150901092520 AM" is "YYYYMMDDHHMISS AM", so you have use it as
to_date('20150901092520 AM', 'YYYYMMDDHHMISS AM'), this will convert it to date object, now to print it required format as "09/01/2015" use to_char and specify the format as "MM/DD/YYYY" like below
to_char(to_date('20150901092520 AM', 'YYYYMMDDHHMISS AM'),'MM/DD/YYYY')
You should be able to use the ISDATE function to test the string before converting
https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_1106.htm
Generally...
SELECT cust_last_name,
CASE value WHEN isdate(value) = 'Yes' THEN convert(date,value)
WHEN isdate(value) <> 'Yes' THEN ''
END
FROM TABLE;

Comparing date with sysdate in oracle

I have a column which is of "DATE" type and I want to run a query on it comparing it with sysdate.
But I am getting following error, Can someone please let me know what I am missing here?
SQL> select distinct file_name as r
from table_1
where view_day >= TO_DATE(SYSDATE-10, 'YYYY/MM/DD');
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
You shouldn't use to_date on a date, To_date is for casting a varchar to date, not a date.
If you do use the function to_date on a date, then oracle will refer to it as a string according to nls_date_format which may vary in different environments.
As #jonearles said, if you want to remove the time in sysdate then use TRUNC
USE:
select distinct file_name as r
from table_1
where view_day >= TRUNC(SYSDATE-10)
Error shows that a VIEW_DAY column is varchar so you need to convert DATE to String. Use TO_CHAR or convert VIEW_DAY to date type.