1

I'm trying to convert string column to date in Oracle SQL.

Here is my an example of value from the data:

'03/12/14 11:00:00'

Here is my query:

select to_date(A,'DD/MM/YY HH24:MI:SS')
from MyTable

Here is an example of the output:

03-DEC-14

Why my query return only the date but not the hour?

4
  • 1
    Because a date doesn't have an hours part? Try TIMESTAMP, maybe it works better. Commented Feb 3, 2015 at 15:39
  • possible duplicate of Convert a string date into datetime in Oracle Commented Feb 3, 2015 at 15:39
  • @jarlh: in Oracle date does have a time. Commented Feb 3, 2015 at 15:42
  • @a_horse_with_no_name. Thanks for the info! (I'm not an Oracle user, but I believe you've already found that out!) Commented Feb 3, 2015 at 15:43

4 Answers 4

3

Assuming you are using SQL*Plus (or SQL Developer) the default NLS_DATE_FORMAT is applied when a DATE value is displayed. To verify your current format you can run:

select value 
from nls_session_parameters 
where parameter = 'NLS_DATE_FORMAT';

To adjust this, run:

alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';

Then you should see the time as well.

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

Comments

1

You are trying to DISPLAY the datetime value. Use TO_CHAR along with proper FORMAT MODEL.

For example,

select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;

A DATE consists of both date and time portions. You just need to make sure you use appropriate format model to display it. Else, you end up with a format which depends on your locale-specific NLS settings.

Comments

0

You can use timestamp

select to_timestamp(A, 'DD/MM/YY HH24:MI:SS') from MyTable

Comments

0

If you want a query that returns the time portion of the date without having to alter the nls_date_format you can convert the date you got from the to_date function to a text representation (the default text representation is dd-Mon-yy as you found out) like this:

select to_char(
   to_date(A,'DD/MM/YY HH24:MI:SS'),
   ''DD/MM/YY HH24:MI:SS')
from MyTable;

Ideally, column A of MyTable should have been defined as a date column instead of a varchar2 column. There are several reasons to do that; data integrity, and better SQL optimization.

Comments

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.