PLSQL Convert String to Datetime - sql

The 'closed_date' column and 'submit_date' are loaded into Oracle as strings, they look like this:
8/17/2017 12:41 (in 24hrs)
How can I convert this string format into date in the format of
mm/dd/yyyy hh24:mi:ss
Thank you!

Given your date format, you don't want seconds:
select to_date(CLOSED_DATE, 'mm/dd/yyyy hh24:mi') as CLOSED_DATE,
to_date(SUBMIT_DATE, 'mm/dd/yyyy hh24:mi') as SUBMIT_DATE
from s_daily_ops

Tack on a trailing ':00', or remove ':ss' from your format string.

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;

Oracle Convert Char to Date

I have the following date in oracle table:
'2017-08-01 00:00:00,000000000'
I want to convert this to date which I am using the following but I don't know how to deal with zeroes?!
.
.
.
T.EXECUTION_LOCAL_DATE_TIME
between to_date('2017-08-01 00:00:00,000000000', 'yyyy-mm-dd hh:mi:ss')
and to_date('2017-08-10 00:00:00,000000000', 'yyyy-mm-dd hh:mi:ss');
Could anyone help me with this?
Oracle dates do not support milliseconds. Assuming your EXECUTION_LOCAL_DATE_TIME column is a date, then the following comparison is the best you can do:
T.EXECUTION_LOCAL_DATE_TIME
BETWEEN TO_DATE('2017-08-01 00:00:00', 'yyyy-mm-dd hh:mi:ss') AND
TO_DATE('2017-08-10 00:00:00', 'yyyy-mm-dd hh:mi:ss');
If you wanted to convert a text string with milliseconds to a timestamp, you could use something like this:
TO_TIMESTAMP ('2017-08-01 00:00:00,000000000', 'yyyy-mm-dd hh:mi:ss,ff')
Generally speaking this values in not a date but a timestamp so I would use following conversion:
select to_timestamp('2017-08-01 00:00:00,000000000', 'yyyy-mm-dd hh24:mi:ss,ff6')
from dual;
Technically you can have non zeros after comma, so If you want to get correct but not truncated value use timestamp date type.

How to convert string to date format?

Hi any one please help me...
The below mentioned line is one column data.
column1:Supplier Setup request submitted on 2016-01-06 06:00:25.141 by WFS ADMINISTRATOR
I want to divide into two columns like columnA and columnB:
columnA columnB
WFS ADMINISTRATOR 2016-01-06 06:00:25.141
I wrote query by using sub string and instring functions:
select SUBSTR(column1, INSTR(column1,'on',1)+2, INSTR(column1,'by',1)-INSTR(column1,'on',2)-2) as columnB
from xyz
It is working fine, but I want to display date format like MM/DD/YYYY HH24:MI:SS.
I can't able to get date format...
You get the datetime part with
regexp_replace(col, '.*on (.*) by.*', '\1')
You convert such string to timestamp with
to_timestamp(str, 'yyyy-mm-dd hh24:mi:ss.ff')
And you convert a timestamp to a string formatted MM/DD/YYYY HH24:MI:SS with
to_char(ts, 'mm/dd/yyyy hh24:mi:ss')
Together:
to_char
(
to_timestamp
(
regexp_replace(col, '.*on (.*) by.*', '\1'), 'yyyy-mm-dd hh24:mi:ss.ff'
), 'mm/dd/yyyy hh24:mi:ss'
)
assuming that columnB now hold the date part:
to_date(columnB,'MM/DD/YYYY HH24:MI:SS');

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.

Why does this fail Q

I am running an SQL statement with this to create a field and I get the desired result:
TO_CHAR(TRUNC(CURRENT_DATE - 8/24,'hh24'), 'DD-Mon-yyyy hh24:mi') AS "DATEVAR"
When I add this to my where statement in the SQL, I get results with the correct time period but not in the MMDDYY scope. What gives?
WHERE
TO_CHAR(MOPACTIVITY.MOPEND, 'dd-mon-yyyy hh24:mi:') < TO_CHAR(TRUNC(CURRENT_DATE - 8/24,'hh24'), 'DD-Mon-yyyy hh24:mi')
You genius is greatly appreciated.
Respectfully,
Jonathan Morningstar
You are comparing dd-mon-yyyy using "<". Of course the range is off. If you want to compare these as strings, then use yyyy-mm-dd:
WHERE TO_CHAR(MOPACTIVITY.MOPEND, 'yyyy-mm-dd hh24:mi:') < TO_CHAR(TRUNC(CURRENT_DATE - 8/24,'hh24'), 'yyyy-mm-dd hh24:mi')
Wouldn't it be easier to compare these as dates?
where MOPACTIVITY < TRUNC(CURRENT_DATE - 8/24)
to_char returns strings, so you are comparing strings, not date. So they are compared alphabetically.