How to display data by the current system date - oracle

I have tried this, but it's not working:
SELECT day FROM table
WHERE (SYSDATE - day) *24 < 0.5;
How can I get data in my database with its current date?

Assuming that day is a date column, why not do:
select day
from table
where trunc(day) = trunc(sysdate);

Related

Next week in Oracle

I'm using oracle dbms and I have in Employe table a column Birthdate. I want to write a query that shows the employees who has a birthday next week.
Is this correct ?
select name
from employe
where to_char(birthdate,'DD-MM')=to_char(next_day(sysdate,1)+7,'DD-MM');
That is not the correct usage of next_day(): that function returns the date of the the next instance of a day. For example, to find the date of next Friday:
select next_day(sysdate, 'FRIDAY') from dual;
To find employees whose birthday is seven days from now, you need to just tweak your query a bit:
select name
from employe
where to_char(birthdate,'DD-MM') = to_char(sysdate+7,'DD-MM');
The correct solution would be
SELECT name
FROM employe
WHERE to_char(birthdate
/* "move" the birthdate to the current year
to get a reliable week number */
+ CAST((EXTRACT(year FROM current_date)
- EXTRACT(year FROM birthdate)) || '-0'
AS INTERVAL YEAR TO MONTH),
'IW')
= to_char(current_date + 7, 'IW');
The IW format returns the ISO week containing the date, which is probably what you are looking for. If you start your week on Sunday, add one to both dates.

Oracle SYSDATE-1 not returning 24 hour range

I have a table with a timestamp column. My query has a WHERE clause
AND date_created.TIMESTAMP_DATA >= (SYSDATE - 1)
This should return any timestamp which is now-24 hours but I do not get records done the previous day, only the day of (today). I checked that the db SYSDATE is accurate and manipulating date_created to today returns the results, however if date_create is yesterday #5pm, it does not return
Please try changing query as:
AND date_created.TIMESTAMP_DATA >= (SYSTIMESTAMP - 1)
Abhi
Turned out to be a timezone issue. this solved it
AND date_created.TIMESTAMP_DATA >= (SYSTIMESTAMP AT TIME ZONE 'PST'- INTERVAL '1' DAY)
Edited to include - INTERVAL '1' DAY instead of -1

Is there any better way to get data between days in H2 Database?

We want to select data from table with below condition.
Date of Transactiontime <= (Current Date - n Days)
for e.g.
Today is - 2016-06-21.
Date of Transactiontime = '2016-06-19 11:45:07.148'.
With below query we could get Data which is 2 days older.
Query:
SELECT * FROM T WHERE FORMATDATETIME (Transactiontime,'YYYY-MM-d') <= FORMATDATETIME ( DATEADD('HH',-2*24,Now()), 'YYYY-MM-d');
Dataype of Transactiontime = TIMESTAMP
Is there any better way to get the same results?
Have you tried function DATEDIFF()?
SELECT * FROM T WHERE DATEDIFF('DAY', NOW(), Transactiontime) >= 2
EDIT
Might be better using DAY_OF_YEAR instead of DAY.

how to subtract date from another date in oracle

Let's say I am having a table orders and having column
LastUpdatedDate, status.
What i am trying to do is -
picks all the Order which are ā€˜Pā€™ status and lastupdate is two days
back from current date
.
can any one help me in writing a query to get my result.
Thanks
try
select * from yourtable
where status = 'P'
and LastUpdatedDate < sysdate - 2

"BETWEEN" SQL Keyword for Oracle Dates -- Getting an error in Oracle

I have dates in this format in my database "01-APR-12" and the column is a DATE type.
My SQL statement looks like this:
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno AND s.salestype = 1
AND (s.salesdate BETWEEN '01-APR-12' AND '31-APR-12');
When I try to do it that way, I get this error -- ORA-01839: date not valid for month specified.
Can I even use the BETWEEN keyword with how the date is setup in the database?
If not, is there another way I can get the output of data that is in that date range without having to fix the data in the database?
Thanks!
April has 30 days not 31.
Change
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno AND s.salestype = 1
AND (s.salesdate BETWEEN '01-APR-12' AND '31-APR-12');
to
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno AND s.salestype = 1
AND (s.salesdate BETWEEN '01-APR-12' AND '30-APR-12');
and you should be good to go.
In case the dates you are checking for range from 1st day of a month to the last day of a month then you may modify the query to avoid the case where you have to explicitly check the LAST day of the month
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno
AND s.salestype = 1 AND (s.salesdate BETWEEN '01-APR-12' AND LAST_DAY(TO_DATE('APR-12', 'MON-YY'));
The LAST_DAY function will provide the last day of the month.
The other answers are missing out on something important and will not return the correct results. Dates have date and time components. If your salesdate column is in fact a date that includes time, you will miss out on any sales that happened on April 30 unless they occurred exactly at midnight.
Here's an example:
create table date_temp (temp date);
insert into date_temp values(to_date('01-APR-2014 15:12:00', 'DD-MON-YYYY HH24:MI:SS'));
insert into date_temp values(to_date('30-APR-2014 15:12:00', 'DD-MON-YYYY HH24:MI:SS'));
table DATE_TEMP created.
1 rows inserted.
1 rows inserted.
select * from date_temp where temp between '01-APR-2014' and '30-APR-2014';
Query Result: 01-APR-14
If you want to get all records from April that includes those with time-components in the date fields, you should use the first day of the next month as the second side of the between clause:
select * from date_temp where temp between '01-APR-2014' and '01-MAY-2014';
01-APR-14
30-APR-14

Resources