I am trying to only display the date and time of a table in a certain format. This format is DD-MON-YYYY and the time HH24:MI:SS. I don't understand how to make both formats work together. I can get them to function separately.
select to_char(sysdate, 'DD-MON-YYYY', systimestamp,'HH24:MI:SS') from dual;
My error is 'too many arguments'. I want to understand why it isn't working.
From the documentation TO_CHAR takes three arguments when using dates
a date or date time
a format model
optional NLS parameter for the localization
You can concatenate the two results together with this.
select to_char(sysdate, 'DD-MON-YYYY')||' '|| TO_CHAR(systimestamp,'HH24:MI:SS') from dual;
But why would when you do it one call
SELECT TO_CHAR(systimestamp,'DD-MON-YYYY HH24:MI:SS') from dual;
NB SQL is not case sensitive in regards to keywords. Upper or lower case both work.
Try:
select to_char(sysdate, 'DD-MON-YYYY HH24:MI:SS') from dual;
I tried to display the sqlcommand after I saved into the database. Everything seemed to work pretty fine, but when I opened my table from TOAD, the dates are wrong.
Here is my sql command:
INSERT INTO USERTASK (USERTASKKEY, USERID, TASKKEY, TASKDATE, CREATEDATE, CREATEUSERID)
VALUES (USERTASKSEQUENCE.NEXTVAL, 'admin2', '1',
TO_DATE('05-06-2015','yyyy/mm/dd HH24:MI:SS'),
TO_DATE('2015-06-16 15:39:42', 'yyyy/mm/dd HH24:MI:SS'), 'admin')
In my database the date save as:
6/20/0016 3:00:00.000000
Here you can see a screenshot with several dates wrong:
What am I doing wrong?
In the TO_DATE function you have to keep the format.
The first parameter is the data and the second parameter is the format you are putting it. For example:
to_date('29-Oct-09', 'DD-Mon-YY')
to_date('10/29/09', 'MM/DD/YY')
to_date('120109', 'MMDDYY')
to_date('29-Oct-09', 'DD-Mon-YY HH:MI:SS')
to_date('Oct/29/09', 'Mon/DD/YY HH:MI:SS')
to_date('October.29.2009', 'Month.DD.YYYY HH:MI:SS')
So if you put TO_DATE('05-06-2015','yyyy/mm/dd HH24:MI:SS') it tries to convert 05-06-2015 to the format yyyy/mm/dd HH24:MI:SS
Try with TO_DATE('05-06-2015','DD/MM/YYYY')
The below one is working fine and returns date in the desired format
select TO_DATE(TO_CHAR(max(entdate), 'DD/MM/YYYY'), 'DD/MM/YYYY') as last_transaction_date from table;
After adding hh24:mm:ss and error of "format code appears twice" ORA-01810 appears
select TO_DATE(TO_CHAR(max(entdate), 'DD/MM/YYYY hh24:mm:ss'), 'DD/MM/YYYY hh24:mm:ss') as last_transaction_date from table;
I'm not able to understand the difference between both cases
MI not MM for minutes
"Some people mistakenly use the MM format code to represent minutes, thus using the MM format for both the months and the minutes."
select TO_DATE(TO_CHAR(max(entdate), 'DD/MM/YYYY HH24:MI:SS'), 'DD/MM/YYYY HH24:MI:SS') as last_transaction_date from table;
for rate in ( select tgt.carrier_code, tgt.tc_code, tgt.exp_date, (TO_date(src.eff_date,'DD/MM/RRRR HH24:MI:SS') - 1/(24*60*60)) eff_date from mira_rate
tgt, mira_rate_dummy src
where src.carrier_code=tgt.carrier_code and src.tc_code=tgt.tc_code)
loop
update mira_rate
set exp_date=to_date(rate.eff_date,'DD/MM/RRRR HH24:MI:SS')
end loop;
When i run this query I am getting Format error.
How to correct this? I treid giving diff to_date options it says some .
Pls suggest
Assuming the question title means you got ORA-01843: not a valid month, this is probably down to your unnecessary to_date() calls as a_horse_with_no_name suggested, but it depends on your NLS_DATE_FORMAT setting. It's easy enough to reproduce, but only by making an assumption about your environment:
alter session set nls_date_format = 'MM/DD/RRRR';
select to_date(eff_date, 'DD/MM/RRRR HH24:MI:SS') from (
select (to_date(sysdate, 'DD/MM/RRRR HH24:MI:SS') - 1/(24*60*60)) eff_date
from dual
);
which gives:
select (to_date(sysdate, 'DD/MM/RRRR HH24:MI:SS') - 1/(24*60*60)) eff_date
*
ERROR at line 2:
ORA-01843: not a valid month
or since the error's pointing to the inner select, just:
SQL> select to_date(sysdate, 'DD/MM/RRRR HH24:MI:SS') from dual;
select to_date(sysdate, 'DD/MM/RRRR HH24:MI:SS') from dual
*
ERROR at line 1:
ORA-01843: not a valid month
The issue is that you're doing an implicit conversion of your date; effectively:
select to_date(to_char(sysdate, 'MM/DD/RRRR'), 'DD/MM/RRRR HH24:MI:SS')
The implicit conversion renders that as 01/28/2013 (or with the time as well, if that's in your NLS date mask), and when you then try to convert that back to a date with mask DD/MM/RRRR ... it's trying to use '28' as the month, which is clearly invalid.
If your fields are already dates then your to_date() calls are pointless, confusing and in this case causing an error. You should be able to do:
for rate in (select tgt.carrier_code, tgt.tc_code, tgt.exp_date,
src.eff_date - interval '1' second as eff_date
from mira_rate tgt, mira_rate_dummy src
where src.carrier_code=tgt.carrier_code and src.tc_code=tgt.tc_code)
loop
update mira_rate
set exp_date = rate.eff_date;
end loop;
I changed your - 1/(24*60*60) to - interval '1' second because I find that clearer, but it has the same effect.
Your update doesn't have a where clause though, but since it was missing a ; that might have been lost in transcription.
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