how to get date part in oracle - oracle

id name date
1 AAA 12-01-15
2 BBB 19-09-12
3 CCC 23-07-10
4 DDD 06-10-01
5 EEE 08-11-05
6 FFF 18-04-99
7 GGG 07-08-12
i have tried it but by converting it is possible but i dont want to convert it i want to extract only date part help me out please
i have a table with date field with datatype i want to extract date from date field
my condition is i want to get records which equal to sysdate i.e i want to get records which are inserted on present day which equal to datepart

If I understand correctly the 'date' column in your table has the DATE datatype. If so then the following should get what you're looking for:
SELECT t.*
FROM YOUR_TABLE t
WHERE TRUNC(t.DATE_FIELD) = TRUNC(SYSDATE)
Of course, based on the data in your question the above query will not return any rows, but if there was data in the table with the same date as SYSDATE they would be returned.
(And by-the-way - since DATE is a data type it's a Bad Idea to use it as the name of a column. Here I've changed it to DATE_FIELD).
Best of luck.

If your column is varchar, then use this
select * from (
select '21-10-15' as dt from dual)
where to_date(dt,'dd-mm-yy') = trunc(sysdate)
If your column is on type date and you are storing only date, not timestamp, then use this.
select * From (
select to_date('21-10-15','dd-mm-yy') as dt from dual)
where dt = trunc(sysdate)
If you are saving the column as date along with time, then use Bob's solution
select * From (
select to_date('21-10-15','dd-mm-yy') as dt from dual)
where trunc(dt) = trunc(sysdate)
I changed the column to dt. Change it as per your convenience.

Related

select record by date is now in oracle

I have following table
Item
Insert_Date
A
11-JAN-23
B
10-JAN-23
And I want to select records have Insert_Date equal Now date without write
select * from *My_Table* where insert_date = '11-JAN-23' ;
I tried
select * from *My_Table* where insert_date = TRUNC(CURRENT_DATE) ;
But it doesn't work;
In Oracle, a DATE is a binary data-type that ALWAYS has the components year, month, day, hour, minute and second. However, client applications (SQL*Plus, SQL Developer, etc.) often do not display the entire DATE and only display the date component and not the time component; that does not mean that the time component does not exist, only that you aren't seeing it with the default formatting.
This means that your date probably also has a non-midnight time component and your query is not matching on the time components. To solve it, you can select on a range:
SELECT *
FROM My_Table
WHERE insert_date >= TRUNC(CURRENT_DATE)
AND insert_date < TRUNC(CURRENT_DATE) + INTERVAL '1' DAY;
Or you can use TRUNC, but that would prevent you using an index on the insert_date column:
SELECT *
FROM My_Table
WHERE TRUNC(insert_date) = TRUNC(CURRENT_DATE);
Note: To change how SQL*Plus and SQL Developer format dates in your current session, you can use:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Try:
SELECT * FROM My_Table WHERE Insert_Date = CAST( GET_DATE() AS Date)

Finding the age of an event in Oracle

I have a table of sales with an ordered column as a timestamp type.
I would like to find the number of days since the last order. I though it should be simple.
I have tried various methods, but I can’t get a meaningful answer:
select max(ordered) from sales; -- 2022-05-17 22:47:24.467000
select sysdate-max(ordered) from sales; -- Unknown column type: 10
select current_time_stamp-max(ordered) from sales; -- Unknown column type: 10
I want to use the result in a CTE to then add to some other dates, so I thought it should at least result in either an interval type or a number of days.
How can I get the age of the above date?
There are 2 common options:
cast timestamp to date and use sysdate - cast(max(...) as date) - in this case you'll get a number in days:
SQL> select sysdate - cast(timestamp'2000-01-01 00:00:00' as date) diff1 from dual;
DIFF1
----------
8290.97766
use systimestamp - max(...) - in this case you'll get an Interval Day to Second:
SQL> select systimestamp - timestamp'2000-01-01 00:00:00' from dual;
SYSTIMESTAMP-TIMESTAMP'2000-01-0100:00:00'
------------------------------------------
+000008291 00:27:19.105859000

PL SQL data is in numeric format (20211023) i want to use the where clause on date column +30days

Select * from Table where date >='20210911' + 30days
the date is in numeric format and what to pull the records for a specific date +30days of specific date
Could you please help
Uh. Never store dates into any other datatype column but DATE. Now you first have to "convert" it, then do the arithmetic.
select *
from some_table
where to_date(date_column, 'yyyymmdd') > date '2021-09-11' + 30
--------
apply format mask that matches data in that column
Hope (should I say pray?) that all values represent valid dates. Nobody prevents you to store e.g. 20228579 into it, and - applying to_date to it - results in
SQL> select to_date('20228579', 'yyyymmdd') from dual;
select to_date('20228579', 'yyyymmdd') from dual
*
ERROR at line 1:
ORA-01843: not a valid month
SQL>
Once again, bad, BAD idea!
how to apply between clause ( where date_column between date '2021-09-11' and date '2021-09-11'+30
If you have an index on the column that you want to use then convert the value to a date then add 30 days to it and convert it back to a number:
SELECT *
FROM Table_Name
WHERE date_number BETWEEN 20210911
AND TO_NUMBER(
TO_CHAR(
TO_DATE(20210911, 'YYYYMMDD')
+ INTERVAL '30' DAY,
'YYYYMMDD'
)
)
If you don't have an index and want a simpler query then:
SELECT *
FROM Table_Name
WHERE TO_DATE(date_number, 'YYYYMMDD') BETWEEN DATE '2021-09-11'
AND DATE '2021-09-11' + INTERVAL '30' DAY
The best solution would be to convert your numeric column to a DATE column:
ALTER TABLE table_name ADD date_column DATE;
UPDATE table_name SET date_column = TO_DATE(date_number, 'YYYYMMDD');
ALTER TABLE table_name DROP COLUMN date_number;
Then:
SELECT *
FROM Table_Name
WHERE date_column BETWEEN DATE '2021-09-11'
AND DATE '2021-09-11' + INTERVAL '30' DAY
db<>fiddle here

Delete from Oracle table rows older than 1 month

In Oracle I have a table with a DB_FLD_4 defined as VARCHAR2(20 BYTE) but actually holds DATE information with looks like this:
select DB_FLD_4 from DM_SUPDES_DISTRIB order by DB_FLD_4 desc;
4/9/2017
4/7/2017
4/6/2017
Kind of m/d/yyyy format.
I would like to create SP that would delete every day rows from the table older than 1 month.
My sysdate looks like this:
select sysdate from dual;
SYSDATE
24-APR-17 04.41:00
Please help to manipulate formats to make this possible.
Many thanks !!!
Use to_date() on your column, then trunc the sysdate to "round" to the day
delete
from MyTable
where to_date(MyColumn, 'MM/DD/YYYY') < add_months(trunc(sysdate), -1)

Using a case when to know if date format is right

I want to migrate a table which contains some columns with dates. The issue is my dates are often in dd/mm/yyyyy HH24:MM:YYYY format. But sometimes it appears that the format is only dd/mm/yyyy, or blank.
I guess that's why I'm getting ORA-01830 when I'm trying to migrate the datas.
I tried
CASE
WHEN TO_DATE(MYDATE,'DD/MM/YYYY')
then TO_DATE(MYDATE,'DD/MM/YYYY 00:00:00')
END AS MYDATE
But I'm not sure if it is possible to test the date format (and ofcourse it's not working).
Thank you
TO_DATE cannot test date format, but you can do it. If Lalit's answer would not be enough, try something like
select
case when my_date like '__/__/__' then to_date(my_date, 'dd/mm/yy')
when my_date like '__-__-__' then to_date(my_date, 'dd-mm-yy')
...
end
So you have the data type issue. DATE is stored as string literal. As you have mentioned that the date model has the DD/MM/YYYY part same, just that the time portion is either missing for some rows or the entire value is NULL.
For example, let's say your table have the values like -
SQL> WITH dates AS(
2 SELECT 1 num, '29/12/2014 16:38:57' dt FROM dual UNION ALL
3 SELECT 2, '29/12/2014' FROM dual UNION ALL
4 SELECT 3, NULL FROM dual
5 )
6 SELECT num, dt
7 FROM dates
8 /
NUM DT
---------- -------------------
1 29/12/2014 16:38:57
2 29/12/2014
3
SQL>
TO_DATE with proper format model should do the trick.
Let's stick to a format model first.
SQL> alter session set nls_date_format='dd/mm/yyyy hh24:mi:ss';
Session altered.
Now, let's use TO_DATE to explicitly convert the string literal to date.
SQL> WITH dates AS(
2 SELECT 1 num, '29/12/2014 16:38:57' dt FROM dual UNION ALL
3 SELECT 2, '29/12/2014' FROM dual UNION ALL
4 SELECT 3, NULL FROM dual
5 )
6 SELECT num, to_date(dt, 'dd/mm/yyyy hh24:mi:ss') dt
7 FROM dates
8 /
NUM DT
---------- -------------------
1 29/12/2014 16:38:57
2 29/12/2014 00:00:00
3
SQL>

Resources