Covert into date format in Oracle - oracle

I have a character field in the database where Date value is stored.
Now I need to convert this character Date field value in below format
YYYY-MM-DDTHH24:MI:SS
I am using below conversion for it, but it's not working
to_char(lastupdate,'YYYY-MM-DD"T"HH24:MI:SS')
Could anyone please help out on this?

As you are stored your date in character literal like mm-dd-yyyy, you first need to convert it to date data_type using to_date and then to character using to_char as suggested by Nicholas Krasnov. Try like this,
SELECT to_char(to_date('01-01-2014', 'dd-mm-yyyy'),'YYYY-MM-DD"T"HH24:MI:SS')
FROM <table_name>;

If you want the date as 2014-01-17 00:00:00:
select TO_CHAR(TO_DATE(last_update, 'DD/MM/YYYY'), 'YYYY-MM-DD HH24:MI:SS') from <table>;

Related

Oracle - Date Format

I need your assistance with converting Oracle dates.
I have a column that stores dates like this 20150731 00:00:34.220. However, I would like to show the column like this 20150731 but when I run a simple select statement to test output I get the following error.
select TO_DATE('20150731 00:00:34.550','YYYYMMDD')
from dual
Error
ORA-01830: date format picture ends before converting entire input string
This query
select TO_DATE('20150731 00:00:34.550','YYYYMMDD')
from dual
leads to error
ORA-01830: date format picture ends before converting entire input string
because you pass string with length 22 characters, but at the same time you pass date format with 8 characters, which obviously doesn't correspond to string. You should write the query as
select to_timestamp('20150731 00:00:34.550','yyyymmdd hh24:mi:ss.ff3')
from dual
As for your table, since you have varchar2 column with dates, you have to take care about table content. Query requires exect matching of the source string and date format.
If you want to show only date without time and you don't need to process this string as date, you can make just
select substr('20150731 00:00:34.550', 1, 8)
from dual
What is the data type of the column? If it is DATE (as it should be) then not it is not stored in the format you say. It is stored in an internal binary format. You would/should use the to_char function to DISPLAY it in whatever format you choose. If you do not use the to_char function, it will be displayed in the format specified by NLS_DATE_FORMAT, which can be specified at several locations.
As for your example, you passed a string format of yyyymmd hh:mi:ss.fff', but you provided a description mask of only YYYYMMDD. It doesn't know what to do with time component. In addition to that when you SELECT TO_DATE, oracle also has to do an implied TO_CHAR to convert it back to a string for display purposes.
In addition, you provided your to_date with a character string that included fractions of seconds. A DATE data type only resolves to seconds. If you need fractional seconds, you need to use TIMESTAMP, not DATE.
If your column is a varchar and you need a date output:
select TO_DATE(substr('20150731 00:00:34.550', 1, 8),'YYYYMMDD') from dual
If it's in a date format and you need a string output:
select to_char(your_column, 'YYYYMMDD') from your_table
Is that being stored in an Oracle datetime column? If not, you may have to do some manipulation to get it into a DD-MON-YYYY format. If it is being stored as a text string you could use SUBSTR( Date_field, Start_Position, Length) to get the first 8 characters. check out this link SUBSTR
Working on the assumption that you're not trying to change the value in the column, and are just trying to show it in the YYYYMMDD format -
As mentioned by a_horse_with_no_name, you'll just need to convert it to a character string. In this example I used systimestamp as my date:
SELECT TO_CHAR(systimestamp,'YYYYMMDD') FROM DUAL
Result:
20160121
That should give you the YYYYMMDD format you want to display.

Proper DateTime Format on Oracle

I have problem with read a datetime format like this : 7/1/2014 6:02:09 AM , my goal is I want to compare some date in query oracle by to_date function but it seems different with the format, like there are no zero in the month, day, and hours.
my thought for the better query is like this to_date('7/1/2014 6:02:09 AM','MM/DD/YYYY HH:MI:SS AM')
but seems like I have wrong format for this.
Thanks in advance
Try this format instead:
to_date('07-01-2014 12:15','MM-DD-YYYY HH:MI'))
Finally I found the format,
I have to change first the AM PM format to 24 hours timestamp :
TO_CHAR(TO_DATE('01/01/1970 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
thank you all audiences :)

Suppress listagg date truncation/conversion

Good day,
I'm doing listagg(application_date, ', ') where application_date is in the form dd/mm/yyyy hh:mi:ss. The above function results in a list of dates in the form DD-MON-YY.
Any advice on how to return a date formatted like the original and not truncated/converted is appreciated.
Thanks.
This looks like Oracle's implicit type conversion has kicked in. The LISTAGG function expects VARCHAR2, your application_date is probably a DATE, so Oracle converts the DATE to a VARCHAR. As you didn't specify a date format, Oracle uses the default date format.
You can avoid this by doing the conversion yourself:
listagg(to_char(application_date, 'DD/MM/YYYY HH:MI:SS'), ',')

Months between sysdate to earlier date

I want to get the months between current date to earlier date.
SELECT MONTHS_BETWEEN(to_date(fld_valid_from,'yyyy-mm-dd hh24:mi:ss'),TO_DATE(sysdate, 'yyyy-mm-dd hh24:mi:ss')) num_months
FROM tbl_customer
But it's not working. I don't know if that's correct or not.
sysdate is already a date, and does not need to be converted to one using to_date(). I suspect that fld_valid_from is also a date.
What error message are you getting? Did you try comparing the two variables without the TO_DATE command?
SELECT MONTHS_BETWEEN (trunc(fld_valid_from),trunc(sysdate))
FROM tbl_customer
This would work if your column fld_valid_from is a date type. You're comparing a date with another date. You use TO_DATE to convert a string data type to a date data type.
With the TRUNC function, you remove the timestamp from the date, and get only...well, the date:
SELECT SYSDATE, TRUNC(SYSDATE) FROM DUAL;
SYSDATE TRUNC(SYSDATE)
---------------------------------------
16/07/2013 10:45:53 16/07/2013
Hope it helps.
Regards.

Oracle Date formatting "2009-02-13T11:46:40+00:00"

I've had some brilliant help before and I'm hoping you can get me out of a hole again.
I've got a date coming in from a web service in this format:
2009-02-13T11:46:40+00:00
which to me looks like standard UTC format.
I need to insert it into an Oracle database, so I'm using to_date() on the insert. Problem is, I cant get a matching formatting string for it and keep getting "ORA-01861: literal does not match format string" errors.
I know its a fairly trivial problem but for some reason I cannot get it to accept the right format string. Any help appreciated.
Thanks :)
Gareth
You can directly convert it to a TIMESTAMP_WITH_TIME_ZONE datatype.
select
to_timestamp_tz('2009-02-13T11:46:40+00:00','YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
from
dual
TO_TIMESTAMP_TZ('2009-02-13T11:46:40+00:00','YYYY-MM-DD"T"HH24:MI:SSTZH:TZM
---------------------------------------------------------------------------
13-FEB-09 11.46.40.000000000 AM +00:00
(I'm assuming the input string is using a 24-hour clock since there is no AM/PM indicator.)
If you want to convert that to a simple DATE, you can, but it will lose the time zone information.
SELECT CAST(TO_TIMESTAMP_TZ(REPLACE('2009-02-13T11:46:40+00:00', 'T', ''), 'YYYY-MM-DD HH:MI:SS TZH:TZM') AS DATE)
FROM dual
To import date in specified format you can set nls_date_format.
Example:
alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS'
This way your SQL statements can be shorter (no casts). For various mask look at Datetime Format Models

Resources