How to convert string to date format? - sql

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');

Related

PLSQL Convert String to Datetime

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.

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.

convert string literal to a date

I have a varchar2 field in my db with the format of for example -
2015-08-19 00:00:01.0
2014-01-11 00:00:01.0
etc.
I am trying to convert this to a date of format DD-MON-YYYY. For instance, 2015-08-19 00:00:01.0 should become 19-AUG-2015. I've tried
select to_date(upgrade_date, 'YYYY-MM-DD HH24:MI:SS') from connection_report_update
but even at this point I'am getting ORA-01830 date format ends before converting the entire input string. Any ideas?
You have details upto milli seconds, for which, you have to use TO_TIMESTAMP() with format model 'FF'
select to_timestamp('2015-08-19 00:00:01.0' ,'YYYY-MM-DD HH24:MI:SS.FF') as result from dual;
RESULT
---------------------------------------------------------------------------
19-AUG-15 12.00.01.000000000 AM
And Date doesn't have a format itself, only the date output can be in a format. So, when you want it to be printed in a different format, you would need to again use a TO_CHAR() of the converted timestamp;
select to_char(to_timestamp('2015-08-19 00:00:01.0' ,'YYYY-MM-DD HH24:MI:SS.FF'),'DD-MON-YYYY') as result from dual;
RESULT
-----------
19-AUG-2015
Why do you store datetimes in a string???
Anyhow. To get from '2015-08-19 00:00:01.0' to a datetime with milliseconds (which is a TIMESTAMP in Oracle) use to_timestamp:
to_timestamp('2015-08-19 00:00:01.0', 'yyyy-mm-dd hh24:mi:ss.ff')
Then to get the desired output format, use to_char:
to_char(thedate, 'DD-MON-YYYY')
Together:
to_char(to_timestamp('2015-08-19 00:00:01.0', 'yyyy-mm-dd hh24:mi:ss.ff'), 'DD-MON-YYYY')
You should be specifying the format that you want in the call of to_date not the current format:
select to_date(upgrade_date, 'DD-MM-YYYY') from connection_report_update

Converting String Date to Date Time in Oracle

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.

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