Oracle Is there a way to update date and not month, year - oracle

Is there a way to update the date part of a column of type Date. For example, a row in the DB has value 2013-11-22 [stored in YYYY-MM-DD format]. And I want to update it to 2013-11-01.
If it had been one or few rows I could have done a simple update. But the real problem is that there are about hundred thousand rows with different date values. And I want to update all the dates to the 1st of each month. For example
Actual What I would like it to be
2013-01-22 2013-01-01
1989-10-03 1989-10-01
2004-07-01 2004-07-01
2005-12-31 2005-12-01
Would appreciate any help, how can i get this done through SQL. Thanks in Advance.

Assuming that the data type is a date, use trunc
TRUNC(date_column, 'mm')
Here is a sqlfiddle demo

Yes, you can use the trunc() function.
For example:
UPDATE thetable set datevalue = trunc(datevalue, 'MM');

Related

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.

Oracle select date without time and keep date as data type

I have a column name 'Cos_Date' with value like 14APR2017:00:00:00.
However, for a new column name 'Arrival_Date', I would like to keep the date information but omit time, and keep the data type as Date but not Character. Ex, 14APR2017.
I have tried:
select TO_CHAR(Cos_Date, 'DD-MON-YYYY') ARRIVAL_DATE
But it will delete time information, but data type turns to Character.
I search on this site, and tried both:
select TO_DATE(TO_CHAR(Cos_Date, 'DD-MON-YYYY'), 'DD-MON-YYYY') ARRIVAL_DATE
and:
select TRUNC(Cos_Date) ARRIVAL_DATE
But it will not omit time information.
Can I try something else?
Thank you!
You can't "omit" the time portion of a DATE column in Oracle. The DATE data type always contains a time component. If you don't want to see the time, don't display it, e.g.,
SELECT TO_CHAR(TRUNC(Cos_Date),'DD-MON-YYYY') FROM dual;
In Oracle there is no date data type that has only a year-month-day component.
The DATE data type is stored internally as 7- or 8-bytes which always has year (2-bytes), month (1-byte), day (1-byte), hour (1-byte), minute (1-byte) and second (1-byte).
The TIMESTAMP data type also has fractional seconds (and can also have a time zone).
Can I try something else?
No, you either use a VARCHAR2 string or use a DATE or TIMESTAMP and accept that it has a time component.
Selecting date values without time:
SELECT date_col
FROM table
WHERE TO_CHAR (date_col, 'HH24:MI:SS') = '00:00:00';

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

How Should I Represent Time of Day in an Oracle database?

I would like to store time of day - e.g. 18:00 in an Oracle database, and would like to do comparison queries with it.
What is the recommended way to represent a time of day column?
You can use a column with datetime typoe and so yo can perform comparisio on datetime column and show th time with to_char(datetime)
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions180.htm
eg :
SELECT TO_CHAR(your_col, 'HH24:MI')
FROM your_table;
for format models see http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm

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;

Resources