Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7733157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:54:54+00:00 2026-06-01T06:54:54+00:00

I have a text field that is a date, although sometimes it also has

  • 0

I have a text field that is a date, although sometimes it also has the time component. Ex:

"23/1/1999"
"25/2/2003 05:18:00"

Now, i simply want to query them and order by the date component only. My attempt

TO_DATE(
       TO_CHAR(
           TO_DATE(mytextfield  , 'DD-MM-YYYY HH:MI:SS')
       ,'DD-MM-YYYY')
,'MON-YYYY')

The above fails on the outer TO_DATE (ORA-01843 invalid month.)

TO_CHAR(
    TO_DATE(mytextfield   , 'DD-MM-YYYY HH:MI:SS')
,'DD-MM-YYYY')

The above works, but i’m left with a string, not a date.

——– Solution using accepted answer

Just use

EXTRACT(MONTH FROM  TO_DATE(timetable.string_time)  , 'DD-MM-YYYY HH24:MI:SS'))

EXTRACT(YEAR FROM   TO_DATE(timetable.string_time)  , 'DD-MM-YYYY HH24:MI:SS'))

As stated on the answer, you can swap the order you use it in the ORDER BY and in the SELECT.

Will need a little number formatting on the month, and some concats, but this works very well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:54:55+00:00Added an answer on June 1, 2026 at 6:54 am

    NLS_DATE_FORMAT specifies the default date format to use with the TO_CHAR and TO_DATE functions. Let’s change it to get nice output from SQL*Plus:

    ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
    

    First we use TO_DATE to convert the textual representation of date into real date type (note that the date format below accepts the shorter text):

    SELECT TO_DATE('25/2/2003 05:18:00', 'DD-MM-YYYY HH24:MI:SS') FROM DUAL;
    2003-02-25 05:18:00
    
    SELECT TO_DATE('25/2/2003', 'DD-MM-YYYY HH24:MI:SS') FROM DUAL;
    2003-02-25 00:00:00
    

    Second – we want to truncate the date to get rid of unnecessary fields (TRUNC function):

    SELECT TRUNC(TO_DATE('25/2/2003 05:18:00', 'DD-MM-YYYY HH24:MI:SS')) FROM DUAL;
    2003-02-25 00:00:00
    
    SELECT TRUNC(TO_DATE('25/2/2003 05:18:00', 'DD-MM-YYYY HH24:MI:SS'),'MONTH') FROM DUAL;
    2003-02-01 00:00:00
    

    Now you can use this in ORDER BY or GROUP BY provided that you want the ordering based on year/month/day (and so on).

    If you want to do some exotic sorting (for grouping you don’t care: {year,month} isn’t any different from {month, year})- e.g. month/year, then I think you need to compose the individual fields and use the following:

    SELECT EXTRACT(MONTH FROM DATE '1998-03-07') FROM DUAL;
    3
    
    SELECT EXTRACT(YEAR FROM DATE '1998-03-07') FROM DUAL;
    1998
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text field that uses jQuery UI Datapicker. It also has an
I have model Article it has field title with some text that may contain
I have a form text field that pulls a date from a database (in
I have a text field that displays 3 parameters with different formats. It has
I have a form page that has an inputText field that accepts a date.
In a form I have one Date text field and 2 dropdowns: Start time
I would like to have a text field that a user can enter a
I'm have a TextViewCell with a text field that that I'm using in a
I have 1.5 million records, each with a text field body that contains a
I have a feed that is populating a single text field in a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.