How to extract only date value from date field in Oracle? [duplicate] - oracle

This question already has answers here:
How to correctly handle dates in queries constraints
(4 answers)
Closed 8 years ago.
I am new to Oracle database.I dont have much knowledge about date-time concepts in Oracle.
The problem i am facing is to retrieve records which are entered on a particular date.But when i am executing SQL query on database it returns zero records.
Database has date field which contains records with both datetime value.
SQL Query: SELECT * FROM table WHERE table.daterecord = to_date(03-Mar-2010)
It is not returning any record but if i change my query to
SELECT * FROM table WHERE table.daterecord > to_date(04-Mar-2010)
It will return some records.
The above difference is because of time.How can i extract time value from date. Can I use trunc function for this? Thanks in advance for your valuable suggestions.

Yes you can use TRUNC function.
SELECT *
FROM table
WHERE TRUNC(table.daterecord) = TO_DATE('03-Mar-2010', 'DD-MON-RRRR')

see this SO for suggestions of "How to correctly handle dates in queries constraints"?
In addition to the answers already provided, I would suggest using a range since this is more easily indexable:
SELECT *
FROM table
WHERE table.daterecord >= TO_DATE('03-Mar-2010', 'DD-MON-RRRR')
AND table.daterecord < TO_DATE('04-Mar-2010', 'DD-MON-RRRR')

you can use TRUNC
For example :
SELECT *
FROM table
WHERE TRUNC(table.daterecord) = TO_DATE('03-Mar-2010', 'DD-MON-RRRR')

Related

SQL query for latest record [duplicate]

This question already has answers here:
Oracle select most recent date record
(4 answers)
Closed 5 years ago.
I want to select the latest record from a table as based on a date field (crtn_dt). The query below does not work. Does anyone have an idea how it should be fixed?
select * from parcels
order by crtn_dt desc
where rownum = 1
You'd need to order data in the subquery and filter them in an outer query.
select *
from (
select *
from parcels
order by crtn_dt desc
)
where rownum = 1
order by clause is among last operations to perform.
What your query does, apart from being semantically incorrect, it returns one (thanks to rownum = 1 predicate) arbitrary row, and then applies order by clause to that one row.

Searching first condition first and only if not available then second condition

I am writing an SQL query where the query should first search the first value, and only if this value is missing the query should search for the second value.
I have two tables. One of these tables contains the modification date (this is not always filled and can be null) and a creation date which is always filled.
Now what I want is that the query first looks in the table with the modification date and only if it is null looks at the table with the creation date.
Example of the query:
Select *
from all_articles
where to_char(modification_date, 'YYYYMMDD') = to_char(sysdate, 'YYYYMMDD')-1
-- if this is an empty record then
to_char(creation_date, 'YYYYMMDD') = to_char(sysdate, 'YYYYMMDD')-1
Can anyone help me with this query?
Almost all the major RDBMS' available have in built functions to handle such a situation.
The Oracle DB has NVL function which works as follows:
NVL(Modified_dt, Create_dt);
The above will return Modified_dt column data by default. However, if that isn't available, it will return Create_dt.
See here for details:
http://www.w3schools.com/sql/sql_isnull.asp

Oracle - Another way for month query?

first... sorry for my english.
I have a query like this:
Select *
From tableA
Where (
TO_NUMBER(TO_CHAR(dateA(+),'SYYYY')) = 2013
AND TO_NUMBER(TO_CHAR(dateA(+),'MM')) = 02
AND to_number(to_char(dateA(+),'dd')) <= 25
)
and retrieve me the data from each date until last number that I give as parameter, in this case the day 25. This working but delay very much because the form of "Where" statement... anybody know another way that retrieve the data so fast and with the same functionality?
It sounds like you want
SELECT *
FROM tableA
WHERE dateA BETWEEN trunc( date '2013-02-26', 'MM' ) AND date '2013-02-26'
This will return all the rows where dateA is between the first of the month and the specified date. If there is an index on dateA, Oracle would be able to use it for this sort of query (though whether it actually would is a separate issue).

DATEDIFF (in months) in linq

select col1,col2,col3 from table1
where(DATEDIFF(mm, tblAccount.[State Change Date], GETDATE()) <= 4
I want to convert this sql query to LINQ. but I dont know any DateDiff alternative in LINQ. can you please suggest me?
You're looking for SqlMethods.DateDiffMonth.
EDIT: In EF4, use SqlFunctions.DateDiff.
Putting aside your original question for a moment, in your query you use:
where(DATEDIFF(mm, tblAccount.[State Change Date], GETDATE()) <= 4
This query would always cause a full table scan, since you're comparing the result of a function call against a constant. It would be much better if you calculate your date first, then compare your column value against the calculated value, which would allow SQL to use an index to find the results instead of having to evaluate every record in your table.
It looks like you're trying to retrieve anything within the past 4 months, so in your application code, try calculating the date that you can compare against first, and pass that value into your Linq2Entities expression:
DateTime earliestDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-4);
var results = from t in context.table1
where t.col3 >= earliestDate
select t;
In EF6, the class to use is DbFunctions. See, for example, DbFunctions.DiffMonths.
I solved this problem in another manner: I calculated the date from code:
theDate = Date.Now.AddMonths(-4)
And in EF change the condition:
tblAccount.[State Change Date] < theDate

Oracle date function

I am executing the below query,It returns me the blank row.However there are records in the table having upd_time = '12-MAR-08'.I don't understand why it is not returning the date '12-MAR-08'.Please help me out??
SELECT br_data.upd_time FROM BANKREC.br_data
where br_data.upd_time = '12-MAR-08';
It's likely that upd_time isn't exactly 12-MAR-08. The date format is not showing the time component, but it's probably there (DATE data type in Oracle can contain both date and time components).
Try this (it will allow you to see the time components):
alter session set nls_date_format='DD-MON-YY HH24:MI:SS';
SELECT br_data.upd_time FROM BANKREC.br_data
where br_data.upd_time >= to_date('12-MAR-08','DD-MON-YY')
and br_data.upd_time < to_date('13-MAR-08','DD-MON-YY');
Is it the same if you do a
SELECT br_data.upd_time FROM BANKREC.br_data
where trunc(br_data.upd_time) = '12-MAR-08';
It could be that the upd_time is not a date, but a timestamp, so it actually contains for instance '12-MAR-08 05:30' which wouldn't be the same thing. trunc() removes the time part of the timestamp.
Since it doesn't return an error, I assume that it parses the date correctly, but otherwise you could try with to_date('12-MAR-08','DD-MON-YY')
You should use Oracle's function to convert your string properly into a date using
to_date('12-MAR-08', 'DD-MMM-YY')
Then you have to take into account that the Oracle "Date" datatype also contains time information to the nearest second. This means that the date that was constructed in the first step is actually midnight on March 12th. So you have to make sure that the upd_time is truncated to midnight:
trunc(upd_time, 'DAY') = to_date('12-MAR-08', 'DD-MMM-YY')
Your full query becomes
SELECT br_data.upd_time
FROM BANKREC.br_data
WHERE trunc(upd_time, 'DAY') = to_date('12-MAR-08', 'DD-MMM-YY');
There are other ways to skin this cat (you could transfer your updTime column to a proper char field with to_char(upd_time, 'DD-MMM-YY')), but it's usually advisable make the data you are looking for similar to what you can find in the database as that increases your chances of using an index for the lookup.
i don't have access to an oracle db at the moment but i remember using to_char.
try
SELECT br_data.upd_time FROM BANKREC.br_data where to_char(br_data.upd_time, 'DD-MON-YY') = '12-MAR-08';

Resources