Parse a string to a date without slashes in Oracle - sql

In Oracle, I'm able to parse a string to a date type if the user input: dd/mm/yyyy.
I would like to be able to parse without the slash:
01022012 should be parsed to 01/02/2012
a way is text processing and insert slash, then convert to date type. Is there another simple way?
TNX

select to_date('01022012','DDMMYYYY') from dual
==>
TO_DATE('01022012','DDMMYYYY')
------------------------------------
01-Feb-2012
-------- End of Data --------
1 row(s) fetched

TO_DATE doesn't require you to have any separating characters between your date parts. It might also be worth noting for your future reference that if the user input contains characters oracle doesn't care about (it only cares about date parts like dd, mm, yyyy) then they can be anything. All these work fine:
SELECT to_date('12/12/2012', 'dd/mm/yyyy') from dual
SELECT to_date('12-12-2012', 'dd/mm/yyyy') from dual
SELECT to_date('12#12;2012', 'dd#mm%yyyy') from dual
To_date doesn't insist that the separator characters and other ignored text in the date format picture shall match up with the ignored text in the input

Related

Convert number to Time of Day SQL

The time of day in our system is stored as a number, how do you convert 83315 to 8:33:15 AM ?
Here is one way (assuming you need the output in string data type):
with
inputs (tod_number) as (
select 83315 from dual union all
select 143305 from dual
)
select tod_number,
to_char(to_date(to_char(tod_number, 'fm00g00g00'
, 'nls_numeric_characters=.:')
, 'hh24:mi:ss')
, 'hh:mi:ss AM') as tod_string
from inputs
;
TOD_NUMBER TOD_STRING
---------- -----------
83315 08:33:15 AM
143305 02:33:05 PM
The with clause is not part of the actual query - it's there just to simulate input data. Remove it, and replace the table name (inputs in my query) and column name (tod_number) with your actual table and column names. Change the column name for the output (tod_string) as needed.
The solution begins by converting the number to a string in the hh24:mi:ss format (pretending that the group separator is : so that the string has colons instead of commas or periods). Then convert this to a date, with the incomplete format model hh24:mi:ss; Oracle adds a date component, but you don't care what that is, either on input or on output. Then convert back to a string with the desired format model, hh:mi:ss AM.

oracle sql:how to convert string datetime to specified format

I've been searching around for a while now, but I can't seem to find the answer to this small problem.
how to convert string 06-JUL-89 to datetime 06/07/1989?
I've tried with code like the following:
TO_CHAR(TO_DATE(BORN,'DD-MON-YY'),'DD/MM/YYYY')
however, the result shows wrong: to be 06/07/2089? how do i solve the problem?
With RR format model.
SQL> select to_char(to_date('06-jul-89', 'dd-mon-rr'), 'dd/mm/yyyy') result from dual;
--
RESULT here
----------
06/07/1989
SQL>
By the way, it looks as if you store date values as strings. If so, don't do that - switch to DATE datatype (as soon as possible).
It seem that you have the problem of year 2k :
TO_DATE('06-jul-89', 'dd-mon-yy') => 06/07/2089
You must use TO_DATE('06-jul-89', 'dd-mon-rr') => 06/07/1989

String to Date SQL

I have a Comments (Clob) field which is a collection of the time the comment was made, comment made by and the comment itself.
I need to extract the 'Date', 'Comment by' from the field.
Using Regular expressions, I was able to extract the date fields and comment_by field but the date is in character.
I'm not able to convert this field into Date.
Comments Example:
7/18/2018 10:36:29 AM, beginj requesting cancellation as steps are no longer viable.
5/16/2018 8:28:04 AM, josephav Not applicable for GSC
I can get the 'date' using this regular expression (RE):
regexp_substr((dbms_lob.substr(requestcommentsdisplay,50,1)),'[^ ]+',1)
I can get the Comment_by name using this regular expression (RE)
regexp_substr((dbms_lob.substr(requestcommentsdisplay,50,1)),'[a-z]+',1)
So the dates after I extract using RE are like below:
10/5/2017
4/2/2012
12/31/2015
3/16/2014
I need to convert these into Proper dates so that I can use these further in my code
Any Help is appreciated :)
Thanks
Use
SELECT
CASE WHEN (length(<regex_string>).
<9)
Then
TO_DATE('0'||<regex_string>,'MM/DD/YYYY')
but oracle parses it with single digit as well though you can
try to debug and print dates for single digit via this
condition.
ELSE
TO_DATE(<regex_string>,'MM/DD/YYYY')
END CASE FROM TABLE;
to convert the string to date format as the o/p you got from regex_substr is a string.
This will allow you to read the input in the format you are receiving and will convert to actual oracles date format.
You can set nls_territory parameter depending on your country, before calling the query
alter session set nls_territory = 'AMERICA';--alternatively convert to 'TURKEY' as an example
and then try the following
with tab(date_chr) as
(
select '10/5/2017' from dual union all
select '4/2/2012' from dual union all
select '12/31/2015' from dual union all
select '3/16/2014' from dual
)
select to_date(date_chr,'MM/DD/YYYY')
from tab;

to_date function strange behaviour for specific year

Different behaviour ,when the date contains year 2000
select to_date(add_months(sysdate,-50),'dd/mm/yyyy') from dual --error
ORA-01858: a non-numeric character was found where a numeric was expected
-- 04/08/2000 12:59:15 contains year 2000
select to_date(sysdate,'dd/mm/yyyy') from dual --this work,but diff output
You should use TO_CHAR instead of TO_DATE.
The first parameter of TO_DATE function is a STRING parameter so here your data is converted to STRING representation and then converted to date format BUT in the DIFFERENT format:
to_char(add_months(sysdate,-50),'dd/mm/YYYY')
Your first query select to_date(add_months(sysdate,-50),'dd/mm/yyyy') from dual works fine for me
Result:
TO_DATE(ADD_MONTHS(SYSDATE,-50),'DD/MM/YYYY')
---------------------------------------------
04-OCT-09

Modify an existing to_char date format

Oracle SQL automatically converts my field D.START_DT to the following format:
TO_CHAR(D.START_DT,'YYYY-MM-DD')
Which makes it difficult for me to modify my own date format.
I've tried wrapping another TO_CHAR around it with no luck.
TO_CHAR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 'MM/DD')
And I've tried SUBSTR to select certain characters, with no luck. I think the hyphen is getting int he way.
SUBSTR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 6, 7) || '/' || SUBSTR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 9, 10)
What is the work around for this?
I agree with RMAN Express and see no problems converting dates to any format you need...
In case you still have problems try this (first to_char() in outer query is optional):
SELECT to_char(to_date(some_date, 'YYYY-MM-DD'), 'MM/DD') final_date
FROM
(
SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') some_date -- this is your "auto converted" date
FROM dual
)
/
A DATE datatype has no format. When you see a date printed on a screen, there was something that APPLIED the format you see. Could be a "default" in the program you are using (like SQL Developer) or your NLS setting, etc. But, a DATE datatype has no format. So, you have complete control over the format you see on screen.
The simplest is to use the TO_CHAR function:
select TO_CHAR(D.START_DT,'YYYY') from dual;
returns just the four digit year.
See TO_CHAR date format options.
http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements004.htm#CDEHIFJA
You should always supply the format in your code and not rely on some other "default" to supply it.