select record by date is now in oracle - 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)

Related

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

Not being able to write a query that retrieves values between certain dates

SELECT * from my_table where product_number = '86354' and period_v in ('1/1/2019','1/31/2019');
What i also tried:
SELECT * from my_table where product_number = '86354' and period_v between '1/1/2019' and '1/31/2019'
SELECT * from my_table where product_number = '86354' and period_v between #1/1/2019#, #1/31/2019#
SELECT * from my_table where product_number = '86354' and period_v between 1/1/2019 and 31/1/2019
my dates in the sql database have the following format:
2/20/2019
month/day/year
I would recommend in Oracle:
select t.*
from my_table t
where t.product_number = 86354 and
period_v >= date '2019-01-01' and
period_v < date '2019-02-01';
If you are storing strings that you are interpreting as dates, then you should fix your data model. Oracle -- as with almost all other databases -- has a type just to store dates, conveniently called date. You can convert the values on the fly:
to_date(period_v, 'DD/MM/YYYY') >= date '2019-01-01' and
to_date(period_v, 'DD/MM/YYYY') < date '2019-02-01';
Although this can be done, I strongly recommend that you use the correct data type.
In MySQL, you would remove the date keyword:
select t.*
from my_table t
where t.product_number = 86354 and
period_v >= '2019-01-01' and
period_v < '2019-02-01';
Note that I removed the single quotes around 86354. Presumably this is a number. It is best not to mix types.
Also, the comparison uses >= and <. That is on purpose The above code works for both dates and dates with time components.

Use time in between in oracle for multiple date

Select *
from mytable
where paid_time Between to_date('00:00:00','HH24:MI:SS' ) and to_date('00:59:59','HH24:MI:SS')
and paid_date Between to_date('1/8/2016','DD/MM/RRRR') and
to_date('10/8/2016','DD/MM/RRRR');
Note :
1. I need perticular time period only for 10 days
Error :
1. data is there but returning Zero kindly help to solve this
You need to use the full date and time TO_DATE otherwise there is conflicting WHERE clauses'DD/MM/RRRR hh24:mi:ss'.
SELECT *
FROM mytable
WHERE paid_time BETWEEN TO_DATE('01/08/2016 00:00:00', 'DD/MM/RRRR hh24:mi:ss') AND TO_DATE('10/08/2016 00:59:59', 'DD/MM/RRRR hh24:mi:ss');
If paid_time is a string then your query will only work at all for certain NLS_DATE_FORMAT settings, due to the implicit conversion you are forcing:
alter session set nls_date_format = 'RRRR-MM-DD';
with mytable (paid_date, paid_time) as (
select date '2016-08-02', '00:01:02' from dual
)
Select *
from mytable
where paid_time Between to_date('00:00:00','HH24:MI:SS' ) and to_date('00:59:59','HH24:MI:SS')
and paid_date Between to_date('1/8/2016','DD/MM/RRRR') and
to_date('10/8/2016','DD/MM/RRRR');
no rows selected
alter session set nls_date_format = 'YYYY-MM-DD';
-- same query
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
alter session set nls_date_format = 'DD/MM/RRRR';
-- same query
ORA-01847: day of month must be between 1 and last day of month
... etc.
When you do to_date('00:00:59','HH24:MI:SS') the generated date defaults to the first of the current month, so when run today it will get a September date:
select to_char(to_date('00:00:59','HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_CHAR(TO_DATE('00
-------------------
2016-09-01 00:00:59
You are then trying to compare your paid_time string with that date, which means the string is implicitly converted to a date using your NLS settings, e.g.:
alter session set nls_date_format = 'RRRR-MM-DD';
select to_char(to_date('00:01:02'), 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_CHAR(TO_DATE('00
-------------------
2000-01-02 00:00:00
So your filter is really looking for rows where the time string, incorrectly converted to a date (exactly which date depends on your actual NLS setting, and many values will error whatever the setting), is in the first minute of the first day of the current month. Which is very unlikely to ever match anything.
If it is a string and is always formatted consistently then you can just compare as a string:
with mytable (paid_date, paid_time) as (
select date '2016-08-02', '00:01:02' from dual
)
Select *
from mytable
where paid_time Between '00:00:00' and '00:59:59'
and paid_date Between to_date('1/8/2016','DD/MM/RRRR') and
to_date('10/8/2016','DD/MM/RRRR');
PAID_DATE PAID_TIM
---------- --------
2016-08-02 00:01:02
As mentioned in comments Oracle's data datatype includes the time, so storing the date (at midnight, presumably) and the time in separate columns just adds complexity and inefficiency.

how to get date part in 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.

Fetching column values based on SYSDATE

I have a table wchih has 2 columns. The definition is
CREATE TABLE LOGGING_T
(
TSTAMP DATE,
LINE VARCHAR2(300)
)
TABLESPACE OPERATIONS
MONITORING
/
The colulmn TSTAMP has values like 30-NOV-11, 29-NOV-11 ... and so on. Now i am doing this query
select * from LOGGING_T where TSTAMP >= (SYSDATE - 1)
The current system date is 01-DEC-11. Ideally, the above statement should return records which has TSTAMP = 30-NOV-11 since i am doing SYSDATE-1 which would be 30-NOV-11. But it isn't fetching those records. Why?
However, if i do this query
select * from LOGGING_T where TSTAMP >= (SYSDATE - 2)
Then it fetches records who TSTAMP is 30-NOV-11. Am i doing something wrong in this simple date operation?
A DATE contains time of day as well as the date.
If SYSDATE was 2011-12-01 1:18:00 PM then SYSDATE-1 would be 2011-11-30 1:18:00 PM.
Are the rows you are expecting to find from November 30th before or after the time element?
If you don't care about the time, and only want to filter based on the date, you can use TRUNC():
select *
from LOGGING_T
where TRUNC(TSTAMP) >= TRUNC(SYSDATE - 1);
You'll may or may not want to make sure both sides of your comparison operator are TRUNC()ed because TRUNC() will just force the time element of the date to be midnight.
select to_char(trunc(sysdate), 'YYYY-MM-DD HH:MI:SS PM')
from dual;
NOW
----------------------
2011-12-01 12:00:00 AM
The value SYSDATE has the time component as well. Most probably the date in your database also has the time component.
Change your query to :
select * from LOGGING_T where TSTAMP >= TRUNC(SYSDATE - 1)
to see all records which were logged from 00:00 yesterday.
To see the actual timecomponents, use to char.
SQL> select sysdate from dual;
SYSDATE
---------
01-DEC-11
1* select to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS') date1 from dual
SQL> /
DATE1
--------------------
01-Dec-2011 16:29:01

Resources