Hardcoded date required instead of automated date minus 1 - sql

case when "DocDate"<=CAST(ADD_DAYS(CURRENT_DATE,-1) as date) then sum("TransValue") else 0
I need a hardcoded date instead of (CURRENT_DATE) so that my user can able to fetch the report on all days with actual result for that particular date.
Thanks
Kumar

Related

current month record should be display from table

please assist me to correct it.i want to show current month data i tried this but its showing error the literal is too long :
number date
10 20-Jan-2018
20 30-Oct-2018
30 24-Sep-2018
24 01-Oct-2018
select number
from table
where date <= to_char(sysdate,'mm');
It looks like your date column contains text. You should ideally always store date information in proper date columns. As a workaround, we can go in the other direction and use TO_DATE on your date column, to compare it to the first of the current month:
SELECT number
FROM yourTable
WHERE TO_DATE(date, 'dd-mon-yyyy') < TRUNC(sysdate, 'mm');

Modifying SYSDATE function

In one of my SQL queries, I am using
[... and z.READ_TIMESTAMP > TIMESTAMP_TO_EPOCH(TRUNC(SYSDATE-3)]
If I want the date to be exactly 5/31/2017, will I use 'SYSDATE' (date-n) function or some other expression? or how can a modify my query for 5/31/2017
If you want the date to be exactly 5/31/2017 then use TO_DATE() or TO_TIMESTAMP() depending on which data type you need (date or timestamp). As you are using SYSDATE already the the date data type should work.
-- e.g.
select
to_date('5/31/2017','mm/dd/yyyy')
, to_timestamp('5/31/2017','mm/dd/yyyy')
from dual
...
and z.READ_TIMESTAMP > TIMESTAMP_TO_EPOCH(to_date('5/31/2017','mm/dd/yyyy'))
HOWEVER
I suspect you may want more than just a way to establish a fixed date. For example are you asking for "how do I get that last day of the previous month?" which perhaps can be satisfied by using >= and the first day of current month like this:
...
and z.READ_TIMESTAMP >= TIMESTAMP_TO_EPOCH(trunc(sysdate,'MM'))
or if it really is the last day of the previous month can be achieved with a combination of LAST_DAY() and ADD_MONTHS()
and z.READ_TIMESTAMP >
TIMESTAMP_TO_EPOCH( last_day(add_months(trunc(sysdate,'MM'),-1)) )
Without knowing a great deal more about the nature of your data and query purpose please do note that each date you use when "truncated" also has the time set to 00:00:000 - so IF you data contains time within a day other than 00:00:00 then these 2 queries might NOT produce the same result
.... datetimecolumn > to_date('05/31/2017','mm/dd/yyyy') -- "a"
.... datetimecolumn >= to_date('06/01/2017','mm/dd/yyyy') -- "b"
For example "a" the entire 24 hour duration of 05/31/2017 would be included in the results, but for example "b" that same 24 hour duration would be excluded from results. In my experience the last day of any month isn't really the best method for locating date/time based data, instead usually it is the first day of the next month that produces the correct result.

how to compare date parts in SQL Oracle

I have a bit tricky question. E.g. I have a start_date: 15/01/2015 and an end date: 17/03/2015 for given record and I would like to generalize that to know, if the record with those exact start and end date belongs to January (by my definition even if it is 31/01/2015, then it belongs to January).
I did the following:
sum(case when to_date('2015/01/01','yyyy/mm/dd') between ROUND(dtime_method_start,'MONTH') and ROUND(dtime_method_end,'MONTH') then 1 else 0 end) as flag_jan
But the problem with Round function is, that it takes everything from 16-31 as next month, which is no good for me. How can I fix it or rewrite it to make it comply with my definition?
I want to know if a record with certain dtime_method_start and dtime_method_end belongs to January. I have many records with many different start and end dates and want to know how many of them belong to January.
SELECT expected,
CASE
WHEN to_date('01/01/2015','DD/MM/YYYY') = ALL (trunc(start_date,'MONTH'), trunc(end_date,'MONTH'))
THEN 1
ELSE 0
END flag_jan
FROM
(SELECT 'notmatch 0' expected
, to_date('15/01/2015','DD/MM/YYYY') start_date
, to_date('17/03/2015','DD/MM/YYYY') end_date
FROM dual
UNION ALL
SELECT 'match 1'
, to_date('12/01/2015','DD/MM/YYYY')
, to_date('23/01/2015','DD/MM/YYYY')
FROM dual
) dates;
this query compares the truncated start_date and end_date to match the first day of the month.
To check another month_flag, juste change the date in the first case expression.
Just use trunc instead of round. Trunc with parameter 'MONTH' will truncate the date to the first day of month. If you test with between using first day of month it's ok.
I would compare the stored dates directly to a range based on the input date, rather than applying a function to every date:
count(case when dtime_method_start >= trunc(to_date('2015/01/01','yyyy/mm/dd'),'mm')
and dtime_method_start < add_months(trunc(to_date('2015/01/01','yyyy/mm/dd'),'mm'),1)
then 1
end) as flag_jan
Or you could
count(case when trunc(dtime_method_start,'mm') = trunc(to_date('2015/01/01','yyyy/mm/dd'),'mm')
then 1
end) as flag_jan

Check whether date falls in the date range of the current year in sql

Suppose i have an input start date as "01/01/2014" and input end date as "31/01/2014". I need to check whether the input start date and input end date falls within highlighted record.In the highlighted record i have two columns monthfrom and monthto.Which is from December to February.But in my table there is no year mentioned.I need to take for current year but if i take current year then my query fails
SELECT * FROM tablename WHERE dateformatted BETWEEN '01/12/2014' AND '28/02/2014'
If i take end date as next year also again my query fails
SELECT * FROM tablename WHERE dateformatted BETWEEN '01/12/2014' AND '28/02/2015'.
Suppose i format the highlighted details as above query then i can check the input date falls within the date range.After checking the input date falls in the highlighted record i need to get number of days using weekfrom and weekto column.If i use this query
SELECT * FROM tablename WHERE dateformatted BETWEEN '01/12/2014' AND '28/02/2015'.
then will get wrong result because weekfrom will be different in the current year and next year.
Suppose i use the query like this
SELECT * FROM tablename WHERE dateformatted BETWEEN '01/12/2014' AND '28/02/2014'.Again i will not get the expected result because am checking for December to February of the current year.Please help me i need to check two conditions
Whether input date falls in the highlighted record
Count the number of days if it falls within the highlighted record with weekfrom and weekto keeping into consideration.
Week from and weekto may be in reverse order also as saturday to sunday(7 to 1).Here also we need to count the number of working days.
Instead of using this:
SELECT * FROM tablename WHERE dateformatted BETWEEN '01/12/2014' AND '28/02/2014'
Use this:
SELECT * FROM tablename WHERE dateformatted BETWEEN #01/12/2014# AND #28/02/2014#

how to know if between 2 date's it past 5 days - Oracle 10g?

I have two date's:
1) 01/05/2009
2) 06/05/2009
How can I know if there's 5 days between them?
You can subtract two dates to get the difference in days between them (the unit for date columns in Oracle is 1 day).
In your example, I assume date is spelled in DD/MM/YYYY format. So you could do this:
select case when
abs(to_date('01/05/2009','DD/MM/YYYY') - to_date('06/05/2009','DD/MM/YYYY')) = 5
then 'YES'
else 'NO'
end as ARE_DATES_5_DAYS_APART
from
dual;
If the two dates are two columns in a table, then use table name instead of "dual" in query above.
First, get your dates into variables. Then you can use simple addition to determine if the required number of days have passed as Oracle treats addition and subtraction with dates as adding or subtracting days.
For instance Date1, + 5 will equal the date plus 5 days.
In PL/SQL your block may end up looking something like this:
declare
date1 date := to_date('01/05/2009', 'DD/MM/YYYY');
date2 date := to_date('06/05/2009', 'DD/MM/YYYY');
begin
if date1 + 5 >= date2 then
dbms_output.putline('Date 2 is more than five or more days past date 1!');
else
dbms_output.putline('There is less than five days between date 1 and date 2!');
end if;
end;
First decide whether your date format is DD/MM/YYYY or MM/DD/YYYY