oracle sql:how to convert string datetime to specified format - sql

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

Related

How do you convert YYYY_MM to date in Oracle SQL?

I have a column which has dates as texts. Like: '2021_01' (I will reefer this column as TextDate)
I convert it to '2021-01' with this code:
SELECT REPLACE(at.TextDate,'_','-') as DataFormat FROM tableName at
But when I try to cast it to date, or convert it to date, I always get 'Missing expression' errors. I tried this:
SELECT REPLACE(CONVERT(VARCHAR(7), at.TextDate, 126, '_', '-') as date FROM tableName at
But it will give me errors. Any suggestion?
convert means something completely different in Oracle than it does elsewhere. You need the to_date() function:
SELECT TO_DATE(at.textDate, 'YYYY_MM') as DataFormat FROM tableName at
If you want to display it in a particular format then you can either let your client/application do that - most clients by default will use your session's NLS_DATE_FORMAT setting - or explicitly convert it back to a string with the complementary to_char() function.
db<>fiddle
The valid date elements are also in the documentation. You should only convert to a string for display though; while you are manipulating or storing it you should treat it as a date.
How can I filter last 3 months with it?
You need to use Oracle syntax, not SQL Server or other syntax. You also can't refer to a column alias in the same level of query. SO you can recalculate the date value; or as your string format is relatively sane you can convert the target date to a string and compare that, which might allow an index to be used. Something like:
SELECT TO_DATE(at.textDate, 'YYYY_MM') as DataFormat
FROM tableName at
WHERE at.textDate >= TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -3), 'YYYY_MM')
db<>fiddle
TO_DATE with appropriate format mask.
Just to know what's what:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
Code:
SQL> select to_date('2021-01', 'yyyy_mm') from dual;
TO_DATE('2021-01','
-------------------
01.01.2021 00:00:00
SQL>

Change date format in oracle query

When running
select processing_date from table;
i got this result "04-30-2020 20.12.49.978711"
what i want to change the format of the result to "30-APR-20"
is there a way i can do that ?
i tried select to_date(processing_date,'mm-dd-yyyy') from table; but it gives me errors
any help ?
You want to_char():
select to_char(processing_date, 'MM-DD-YYYY')
Dates are stored as an internal format, which you cannot change. If you want the date formatted in a particular way, then one solution is to convert to a string with the format you want.
EDIT:
The date appears to be a string. You can convert it to a date using:
select to_date(substr(processing_date, 1, 10), 'MM-DD-YYYY')
You can then either use as-is or use to_date() to get the format you really want.

oracle sql - a non-numeric character was found where a numeric was expected

I know there are loads of postings regarding fixing this error but, I'm just not understanding it!
val_strg1 value is 01.04.2016. I want to use this and not show lines where this date is older than current date, (i.e. and trunc(sysdate) < dv.val_strg1).
But, even though I have used a to_date format, I still get the a non-numeric character was found where a numeric was expected error?
I have tried several to_date formats;
to_date(val_strg1,'DDMMYYYY'), to_date(val_strg1,'DD-MM-YYYY')
The following gives me a 'not a valid month' error?
to_date(val_strg1,'DD-MON-YYYY')
My script...
select val_strg, val_strg1, to_date(val_strg1,'DDMMYYYY')
from sd_domainval_org
where name = 'HYPERCARE_CUNR'
order by sort_no
How can I use the val_strg1 as a date?
It seems you are using a text column (e.g. VARCHAR2) for the date. And you are saying that
to_date(val_strg1, 'DD.MM.YYYY')
results in an error. So you have a value in that column that does not match the pattern. Here is a query to find such invalid entries:
select *
from domainval
where name = 'HYPERCARE_CUNR'
and not regexp_like(val_strg1, '^[[:digit:]]{2}\.[[:digit:]]{2}\.[[:digit:]]{4}$');
You can then correct the wrong entries, but a better solution would of course be not to store dates in string columns at all. Use date columns instead, so as to not have such issues.
if your string date val_strg1 is in the form 'DD.MM.YYYY' (i.e. '01.04.2016'), then you have to use to_date(val_strg1,'DD.MM.YYYY');
for example: Select to_date('01.04.2016','DD.MM.YYYY') from dual;
If you have errors again, probably you have a string in the recordset that is not in a valid form for the to_date function (check the values in the val_strg1 column).
Bye,
Igor
I found an answer...
and trunc(sysdate) < to_date(regexp_substr(val_strg1, '^[[:digit:]]{2}\.[[:digit:]]{2}\.[[:digit:]]{4}$'),'DD.MM.YYYY')
...seems to work OK.
you have to tune formating string to exactly match your date structure
in this case
select val_strg, val_strg1, to_date(val_strg1,'DD.MM.YYYY')
from sd_domainval_org
where name = 'HYPERCARE_CUNR'
order by sort_no;

How do I convert this oracle date

I am trying to convert from one date format to another. I am not sure how to write the functions.
My source date looks like 01/15/2009 01:23:15
The format I need is 01152009.
Thanks
Try this.
TO_CHAR(TO_DATE('01/15/2009 01:23:15','MM/DD/YYYY HH:MI:SS'),'MMDDYYY')
More info here,
http://psoug.org/reference/date_func.html
Does this work for you? It assumes the date is in date format but will work with timestamp
select to_char(YourDateField,'DDMMYYYY') from dual;
You can always convert it back to a date using the TO_DATE function if you need that format.
select TO_CHAR(TO_DATE('01/15/2009 01:23:15','MM/DD/YYYY MI:HH:SS'),'MMDDYYYY') from dual
if your field is already of data type date then you should only do:
select TO_CHAR(<fieldname>,'ddmmyyyy') ...

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.