Oracle - time and date format - oracle

I have an Oracle question, hope someone can guide me. I am using php and sql to select columns from a table. Part of the info I get is the time and date of when the column was created. For the moment I get the date in DD-MM-YYYY format. How can I change it so I can get it in YYYY-MM-DD format?
Thanks

try
SELECT TO_CHAR ( MyDateColumn, 'YYYY-MM-DD' ) FROM MyTable

Related

How to convert datetime in string format in hadoop to short date?

I'm using the following the query to get the date out of string (20191101154559) in Hadoop(hive).
select max(cast(to_date(from_unixtime(unix_timestamp(substr(column3,1,8), 'yyyymmdd'))) as date)) as dt from databasea.table_name_b;
The output i'm getting after running the above script is '31/01/2019' where as the table holds dates of nov 2019. Not sure where i'm going wrong here.
Appreciate if anyone can look into this issue.
from_unixtime you can use it to get in 'yyyymmdd' or 'yyyy-MM-dd'
whatever format you want data in.
For Eg - select from_unixtime(unix_timestamp(substr('20191101154559',1,8), 'yyyymmdd'),'yyyymmdd');
This will return 20191101

Oracle- save dates with correct centuryinfo

I am trying to insert nvarchar2 values into my oracle Date field. When my source nvarchar2 value is '20701130', I want to save it as '30/11/2070' in my oracle column. And, if the source value is '21150529', I want to save it as '29/05/2115'.
I tried different formatting options to achieve this, but the year is always getting saved as two digits and is not indicating the correct century.
One of my Queries:
INSERT INTO MY_TABLE (MY_DATE_FIELD)
VALUES(TO_DATE('21061030', 'RRRR-MM-DD'));
Now, when I select after the above query, I get the result as 30/10/06 which is not we want, it should be '30/10/2106'.
You can go about it in many ways:
You alter the session date format itself to a format you want to achieve like :
ALTER SESSION SET NLS_DATE_FORMAT = 'RRRR-MM-DD';
Another approach is once you have achieved a date from a string passed using to_date then you can again add to_char to display the date in a format you want.
To_date will convert your string to a date using the format you have provided but while selecting it will still display the date as per the format provided in the session that's why we use To_char to change it to a format we want to display it into.
select to_char(TO_DATE('21061030', 'RRRR-MM-DD'),'RRRR-MM-DD') "DATE" from dual;
Thanks for the help.
I have pasted my SQL below if anyone is interested.
-- To insert into table
UPDATE MY_TABLE SET MY_DATE_FLD = TO_CHAR(TO_DATE('21061030', 'RRRR-MM-DD'),'DD-MM-YYYY') ;
-- To read from the table, this gave me the result 30/01/2106
select TO_CHAR(DATE_FLD,'DD/MM/YYY') AS START_DATE from MY_TABLE;
Appreciate the help.
Kiran.

How to load files using SQLLDR with date format as yyyymmddhhmmss?

I need to load a table with a .csv file which contains date "20140825145416".
I have tried using (DT date "yyyymmdd hh24:mm:ss") in my control file.
It throws an error as ORA-01821: date format not recognized
I require the data in table as "MM/DD/YYYY HH:MM:SS".
Sample data : 20140825145416
thanks in advance.
Well, I would be remiss if I did not point out that the correct answer is to never store dates as VARCHAR2 data, but make it a proper DATE column and load it like this:
DT DATE "YYYYMMDDHH24MISS"
Formatting is done when selecting. It will make your life so much easier if you ever need to use that date in a calculation.
That out of the way, If you have no control over the database and have to store it as a VARCHAR2, first convert to a date, then use to_char to format it before inserting:
DT CHAR "to_char(to_date(:DT, 'YYYYMMDDHH24MISS'), 'MM/DD/YYYY HH24:MI:SS')"
Note 'MI' is used for minutes. You had a typo where you used 'MM' (months) again for minutes.
I know it's already been said in the previous answer, but it's so important, it's worth repeating. Do not store dates as varchars !!
If your DT column is timestamp then this might work
DT CHAR(25) date_format TIMESTAMP mask "yyyymmddhhmiss"
I used something like this in external tables. Maybe this might help
https://docs.oracle.com/cd/B19306_01/server.102/b14215/et_concepts.htm
and
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:8128892010789

In Oracle, Covert Timestamp to Number

In oracle database, I have a table with one column for "Update date" having data type as Timestamp (MM/DD/YYYY HH:MI:SS AM).
I want to convert this timestamp into a number, including the Hour, Minute and Seconds part.
Anyone please suggest what should be my query to get the same.
Any help would be appreciated.
Thanks
Rajat Arora
Solution query---
Select
TO_NUMBER(TO_CHAR(Timestamp, 'YYYYMMDDHH24MISS'))
From
dual;

SQL stored Procedure - Convert date format

I have a table with multiple columns. One among those is a date column with DATE format as
TIMESTAMP (6) WITH TIME ZONE
Now I want to copy this date to another table but after changing the date format to
YYYY - MM - DD HH:MM:SS
How can I conver Date from one date format to another in Oracle stored procedure?
Is their any specific function to do so?
Kindly suggest.
try with
TO_TIMESTAMP(date-string,'YYYY-MM-DD HH24:MI:SS')

Resources