Converting String Date to Date Time in Oracle - sql

I have a situation where I need to convert the datetime value stored as string to Timestamp:
I am using oracle database
This actually works for me select TO_DATE('11-27-2013 21:28:41', 'MM-DD-YYYY HH24:MI:SS') from dual;
But my date value now is diffent from the above:
select TO_DATE('Sunday 6/1/2014 8:00AM', 'MM-DD-YYYY HH24:MI:SS') from dual; - failed. I have 'Sunday' inside my date.

You have to specify a correct format mask to the TO_DATE function. See a full list of format masks along with documentation for the function here: http://www.techonthenet.com/oracle/functions/to_date.php
You can correct your problem by:
SELECT TO_DATE('Sunday 6/1/2014 8:00AM', 'DAY M/D/YYYY HH:MIAM') FROM DUAL;

Try using the correct format. I think this will work:
select TO_DATE('Sunday 6/1/2014 8:00AM', 'DAY MM/DD/YYYY HH:MI AM')
Here is a SQL Fiddle.

The following seems to work fine:
SELECT TO_DATE('Sunday 6/1/2014 8:00AM', 'DAY MM/DD/YYYY HH:MIAM') FROM DUAL
Share and enjoy.

Related

problem with using to_date in oracle query

i simply wanna change a string to a date format using to_date
SELECT TO_DATE('20-APR-20 09.50.06 AM' , 'DD-MOM-YY HH24:MI:SS AM') FROM DUAL;
and also i want to change to 24 format
when i run this i get the ORA-01821: date format not recognized error .
The correct format for converting your string to a date is:
SELECT TO_DATE('20-APR-20 09.50.06 AM' , 'DD-MON-YY HH.MI.SS AM')
FROM DUAL;
If you want it as a string, then you can use TO_CHAR() after converting to a date. That said, I recommend keeping the value as a date.
The correct format is
SELECT TO_CHAR(TO_DATE('20/APR/20 09.50.06 AM' , 'DD-MON-YY HH:MI:SS AM'),'DD-MON-YY HH:MI:SS AM') FROM DUAL;

SQL Date conversion getting "Invalid Number"

Hello I have the following date in SQL ORCLE
2020-02-07 13:21:56.478000
And I want it in the following format:
27-JAN-20 03.00.00.000000000 AM
I googled around and found the to_char and to_date and came up with this
(TO_TIMESTAMP(TO_CHAR('2020-02-07 13:21:56.478000','DD-MON-YY HH:MI:SS.FF'))
But I keep getting "invalid number" and oracle is not telling me where it is. Any ideas?
Use the following method
to_char(
timestamp '2020-02-07 13:21:56.478000',
'DD-MON-YY HH:MI:SS.FF'
)
The problem is that to_char() expects a date-like datatype as argument, while your are giving it a string.
If you are dealing with a literal string, then you use the date literal syntax:
to_char(
timestamp '2020-02-07 13:21:56.478000',
'DD-MON-YY HH:MI:SS.FF AM'
)
If you are dealing with a string column, then you can turn it to a timestamp first with to_timestamp():
to_char(
to_timestamp(my_string_col, 'YYYY-MM-DD HH24:MI:SS.FF'),
'DD-MON-YY HH:MI:SS.FF AM'
)
Finally, if you are dealing with a timestamp column, then you can use to_char() directly:
to_char(my_date_col, 'DD-MON-YY HH:MI:SS.FF AM')
Note: your original format modifier was missing the AM/PM part, I added it.

What is difference between TO_CHAR and TO_DATE

I'm running two queries
select TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS PM') from dual;
It displays date with exact time.
select TO_DATE(SYSDATE, 'DD-MON-YYYY HH:MI:SS PM') from dual;
It displays date with default time 12:00:00 AM.
I do not understand TO_CHAR and TO_DATE usage. How to display date with exact time by using TO_DATE
to_char function is used to convert the given data into
character....
SQL> SELECT TO_CHAR(SYSDATE, 'dd/mm/yyyy') FROM dual;
TO_CHAR(SY
------------------
04/04/2012
to_date is used to convert the given data into date data
formate data type....
eg: to_date('070903', 'MMDDYY') would return a date value
of July 9, 2003.
Reference: Interview Question

Select date from between two timestamps

I am facing the following problem.
I have a database with a table which saves Dates (with its time).
Now I would like to know all the tables information where the date is in between two timestamps, but I am getting the following error:
01830. 00000 - "date format picture ends before converting entire input string".
What I did so far is this query:
SELECT * FROM ARBEITSBLOCK WHERE STARTZEIT BETWEEN '30.11.2015 19:00:00'
and '01.12.2015 19:05:00';
And this which doesn't give me any result but there should be:
SELECT * FROM ARBEITSBLOCK
WHERE TO_CHAR(STARTZEIT,'DD.MM.YYYY H24:MM:SS') BETWEEN '30.11.2015 13:00:00'
and '01.12.2015 19:05:00';
Try this statement (using Oracle syntax)
SELECT *
FROM ARBEITSBLOCK
WHERE STARTZEIT BETWEEN TO_DATE ('12/04/2015 09:00:00 AM', 'mm/dd/yyyy hh:mi:ss AM')
AND TO_DATE ('12/04/2015 10:00:00 AM', 'mm/dd/yyyy hh:mi:ss AM');
If STARTZEIT is a DATE column, then why are you trying to compare it to a string?
By doing that, you are relying on Oracle being able to say "aha! This string is really a date, so I will attempt to convert it for you!". That's all well and good, but how will Oracle know how the date-in-the-string is formatted?
Well, there's the nls_date_format parameter which is defaulted to 'DD-MON-RR', and I think you can now see why you're getting the "date format picture ends before converting entire input string" error, since 'DD-MON-RR' is a lot shorter than '30.11.2015 19:00:00'.
Instead of relying on this implicit conversion and the bugs that go right along with that (as you've discovered!), you should explicitly convert the string into a date, which you can easily do with the to_date() function.
E.g.:
select *
FROM ARBEITSBLOCK
WHERE STARTZEIT BETWEEN to_date('30.11.2015 19:00:00', 'dd.mm.yyyy hh24:mi:ss')
and to_date('01.12.2015 19:05:00', 'dd.mm.yyyy hh24:mi:ss');
Oracle does not store dates in the format you see. It stores it internally in 7 bytes with each byte storing different components of the datetime value.
You must use TO_DATE with proper FORMAT MODEL to explicitly convert the literal to DATE.
SELECT *
FROM ARBEITSBLOCK
WHERE STARTZEIT BETWEEN
TO_DATE('30.11.2015 19:00:00', 'DD.MM.YYYY HH24:MI:SS')
AND
TO_DATE('01.12.2015 19:05:00', 'DD.MM.YYYY HH24:MI:SS');
Remember, the DATE data type has both date and time elements, TIMESTAMP is an extension to DATE data type.

How to change the date format from MM/DD/YYYY to YYYY-MM-DD in PL/SQL?

I have a date column in a table stored as MM/DD/YYYY format. I have to select and store the same date in another table in YYYY-MM-DD format i.e. XSD Date Format. But I am not able to do it. I am using this query:
select to_date(date_column,'YYYY-MM-DD') from table;
But still I am not able to do it. Giving me error
ORA-01843 : not a valid month
use
select to_char(date_column,'YYYY-MM-DD') from table;
It sounds like you've got it the wrong way round. If your existing data is in MM/DD/YYYY format, then you want:
select to_date(date_column,'MM/DD/YYYY') from table;
to convert the existing data to DATE values. (I do wonder why they're not stored as dates, to be honest...)
If you want to perform the conversion in one step, you might want:
select to_char(to_date(date_column,'MM/DD/YYYY'), 'YYYY-MM-DD') from table;
In other words, for each row, parse it in MM/DD/YYYY format, then reformat it to YYYY-MM-DD format.
(I'd still suggest trying to keep data in its "natural" type though, rather than storing it as text in the first place.)
I assume that you can use the Oracle SQL Developer, which you can download from here.
You can define the date format which you want to work with:
ALTER SESSION SET nls_date_format='yyyy-mm-dd';
With this, now you can perform a query like this:
SELECT * FROM emp_company WHERE JDate = '2014-02-25'
If you want to be more specific you can define the date format like this:
ALTER SESSION SET nls_date_format='yyyy-mm-dd hh24:mi:ss';
To convert a DATE column to another format, just use TO_CHAR() with the desired format, then convert it back to a DATE type:
SELECT TO_DATE(TO_CHAR(date_column, 'DD-MM-YYYY'), 'DD-MM-YYYY') from my_table
select to_date(to_char(ORDER_DATE,'YYYY/MM/DD'))
from ORDERS;
This might help but, at the end you will get a string not the date. Apparently,
your format problem will get solved for sure .
For military time formatting,
select TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24:mm:ss') from DUAL
--2018-07-10 15:07:15
If you want your date to round DOWN to Month, Day, Hour, Minute, you can try
SELECT TO_CHAR( SYSDATE, 'yyyy-mm-dd hh24:mi:ss') "full-date" --2018-07-11 10:40:26
, TO_CHAR( TRUNC(SYSDATE, 'year'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-year"-- 2018-01-01 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'month'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-month" -- 2018-07-01 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'day'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-Sunday" -- 2018-07-08 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'dd'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-day" -- 2018-07-11 00:00:00
, TO_CHAR( TRUNC(SYSDATE, 'hh'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-hour" -- 2018-07-11 10:00:00
, TO_CHAR( TRUNC(SYSDATE, 'mi'), 'yyyy-mm-dd hh24:mi:ss') "trunc-to-minute" -- 2018-07-11 10:40:00
from DUAL
For formats literals, you can find help in
https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions242.htm#SQLRF52037
You can do this simply by :
select to_char(to_date(date_column, 'MM/DD/YYYY'), 'YYYY-MM-DD') from table
According to the comments, the data-type in the datatable is DATE.
So you should simply use:
"select date_column from table;"
Now if you execute the select you will get back a date data-type, which should be what you need for the .xsd.
Culture-dependent formating of the date should be done in the GUI (most languages have convenient ways to do so), not in the select-statement.
Basically , Data in a Date column in Oracle can be stored in any user defined format or kept as default.
It all depends on NLS parameter.
Current format can be seen by : SELECT SYSDATE FROM DUAL;
If you try to insert a record and insert statement is NOT in THIS format then it will give :
ORA-01843 : not a valid month error.
So first change the database date format before insert statements ( I am assuming you have bulk load of insert statements) and then execute insert script.
Format can be changed by :
ALTER SESSION SET nls_date_format = 'mm/dd/yyyy hh24:mi:ss';
Also You can Change NLS settings from SQL Developer GUI , (Tools > preference> database > NLS)
Ref: http://oracle.ittoolbox.com/groups/technical-functional/oracle-sql-l/how-to-view-current-date-format-1992815
This worked for me! You can convert to datatype you want be it a date or string
to_char(TO_DATE(TO_CHAR(end_date),'MM-DD-YYYY'),'YYYY-MM-DD') AS end_date
Late reply but for.databse-date-type the following line works.
SELECT to_date(t.given_date,'DD/MM/RRRR') response_date FROM Table T
given_date's column type is Date
Just to piggy back off of Yahia, if you have a timestamp you can use this function to cast exclusively as date, removing the timestamps.
TO_CHAR(CAST(DateTimeField AS DATE), 'YYYY-MM-DD') AS TrackerKey__C
Or in my case I need the below format
TO_CHAR(CAST(DateTimeField AS DATE), 'YYYYMMDD') AS TrackerKey__C
SELECT TO_DATE(TO_CHAR(date_column,'MM/DD/YYYY'), 'YYYY-MM-DD')
FROM table;
if you need to change your column output date format just use to_char this well get you a string, not a date.
use
SELECT STR_TO_DATE(date_column,'%Y-%m-%d') from table;
also gothrough
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html