0

Screenshot of query executed The date string in 'Sat Oct 29 10:47:50 CDT 2016' has to be converted to date/timestamp format in oracle query. I need to extract only date and time from it. I tried using to to_date() but didn't work. Can one someone please help.

Error: ORA-01841: (full) year must be between -4713 and +9999, and not be 0 01841. 00000 - "(full) year must be between -4713 and +9999, and not be 0" *Cause: Illegal year entered *Action: Input year in the specified range

4
  • Please post the to_date() syntax you used. Which format did you use? docs.oracle.com/cd/B19306_01/server.102/b14200/… Commented Jan 31, 2017 at 6:14
  • 1
    Please show in the question how did you use to_date function.? Commented Jan 31, 2017 at 6:15
  • @Wernfried Please give a solution then if you have one. I have never even used Oracle hence I could not test my answer. Commented Jan 31, 2017 at 7:49
  • Order_date has the string format of timestamp of order received as mentioned above and query is -- select to_date(order_date ,'YYYY-MM-DD HH24:MI:SS') from table_name where rownum<=3; Commented Jan 31, 2017 at 9:23

1 Answer 1

1

Try this one:

SELECT TO_DATE(
   REGEXP_REPLACE('Sat Oct 29 10:47:50 CDT 2016', '^\w+ (\w+ \d+ \d{2}:\d{2}:\d{2}).+(\d{4})', '\1 \2'), 
      'Mon DD HH24:MI:SS YYYY')
FROM DUAL

Data type DATE or TIMESTAMP does not support any time zone information, you have to remove this part before you can do the conversion.

Sign up to request clarification or add additional context in comments.

4 Comments

This extracts only date from it, we need time also from it.. the DB column has date stored as "Sat Oct 29 10:47:50 CDT 2016" in string format and we need to pull the records between 15min/30min interval by querying on the string colum
No, it also reads the time as by HH24:MI:SS. Check NLS_DATE_FORMAT setting of your current session.
Yeah.."HH24:MI:SS" this reads the time, but somehow the output didn't return the time, it only shows . And the NLS_DATE_FORMAT is "DD-MON-RR" am not aware of how this effects the query output? query screenshot [i.sstatic.net/YZOz2.png]
NLS_DATE_FORMAT defines the default output format of DATE values. Change it (e.g. to yyyy-mm-dd hh24:mi:ss) or define the output format explicitly by TO_CHAR(TO_DATE(REGEXP_REPLACE(...., 'yyyy-mm-dd hh24:mi:ss')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.