I'm having trouble with my WHERE clause when trying to retrieve records for a certain month.
WHERE NVL(TO_CHAR(prsl.DEADLINE, 'YYYY-MM-DD'), TO_CHAR(prsl.SUBMIT_DATE, 'YYYY-MM-DD')) >= DATE '2015-11-01'
AND NVL(TO_CHAR(prsl.DEADLINE, 'YYYY-MM-DD'), TO_CHAR(prsl.SUBMIT_DATE, 'YYYY-MM-DD')) < DATE '2015-12-01'
Basically what this does is if DEADLINE is not null, use that to compare to the date range. If it is null, use the SUBMIT_DATE for comparison instead.
Using the above query, I get this error:
ORA-01861: literal does not match format string
You can try with EXTRACT:
EXTRACT(MONTH FROM prsl.DEADLINE) = 11
AND EXTRACT(YEAR FROM prsl.DEADLINE) = 2015
Another solution would be this:
WHERE TRUNC(COALESCE(prsl.DEADLINE, prsl.SUBMIT_DATE), 'MM') = DATE '2015-11-01'
Related
I'm looking to see if it's possible to filter out a list of quarter ending date within a field containing date from a table.
Below is my code for grabbing a range of dates but how can I modify it to grab just the quarter ending dates? example for 2017 - I would want it to show 2017-03-31,2017-06-31,2017-09-31,2017-12-31.
Thanks.
a.activity_date Between To_Date('2017-01-01', 'YYYY-MM-DD') and To_Date(Trunc(SysDate, 'Q') - 1)
I am thinking:
where activity_date >= date '2017-01-01'
and activity_date < trunc(sysdate, 'q')
and activity_date = trunc(activity_date, 'q') + interval '3' month - interval '1' day
The first two predicates are an adapted version of those of your original query (using date literals, and not using to_date() on what is a date already). The expression that is the right operand of the third predicate computes the end of the quarter for the current date: the logic is truncate the date the beginning of the quarter it belongs to, add 3 months, then substract one day. We can use that value for filtering.
We could also phrase this with bespoke Oracle date arithmetics:
where activity_date >= date '2017-01-01'
and activity_date < trunc(sysdate, 'q')
and activity_date = add_months(trunc(activity_date, 'q'), 3) - 1
I came across a problem that in selecting the date for current desired month and year. I tried the 2 statements shown below but failed to execute the query
select to_char(sysdate, 'Month') from income
select * from income where to_char(sysdate,month) = 'feb'
Update
But after researching and learning more in depth on oracle docs website. What i came out with is to use "between" clause. Specifying the first day and last day of the month . Doing so, it will execute the desired month/year
For an example
SELECT column_name
FROM table_name where column_name = (Your own value) AND
column_date >= to_date('01/02/2012', 'dd/mm/yyyy')
and column_date < to_date('01/03/2012', 'dd/mm/yyyy')
I hope this help :)
Are you after something like:
select *
from income
where <date_column> >= to_date('01/05/2019', 'dd/mm/yyyy')
and <date_column> < to_date('01/06/2019', 'dd/mm/yyyy')
(replacing <date_column> with the name of the date column in your income table that you want to filter on)?
I think you can use the following query:
select *
from income
where to_char(<date_column>,'MON-RRRR') = 'MAY-2019';
If you want to pass in a string like 'May 2012', then I would recommend:
select i.*
from income i
where i.datecol >= to_date('May 2012', 'Mon YYYY') and
i.datecol < to_date('May 2012', 'Mon YYYY') + interval '1' month;
That said, I think your application should turn the string value into a date range and you should use that range in your query:
select i.*
from income i
where i.datecol >= :datestart
i.datecol < :dateend + interval '1 day';
I strong encourage you to avoid between with dates, particularly in Oracle. The date data type has a built-in time component, and that can throw off the comparisons.
Oracle table in my application has a column with name "transaction_date" of type string. It stores date in the format MMDD, where MM = month and DD = day.
Please help me to write a SQL statement which will compare the transaction_date column with the current system date, if transaction_date is less than or equal to 120 days, then fetch the records from the table.
The problem I am facing is, transaction_date in db does not have year just month and day as a string value, so how to check if that value is not more than 120 days, that check should work if value in column is of previous year. For example, SQL should work for the scenario where current system date is lets say 01 feb 2018, and the transaction_date column in table has value "1225" (25th dec of previous year).
As a general disclaimer, your current table design is sub optimal, because a) you are storing dates as text, and b) you are not even storing the year for each date. From what you wrote, it looks like you want to consider all data as having occurred within the last year, from the current date.
One trick we can try here is to compare the MMDD text for each record in your table against TO_CHAR(SYSDATE, 'MMDD'), using the following logic:
If the MMDD is less than or equal to today, then it gets assigned to current year (2018 as of the time of writing this answer)
If the MMDD is greater than today, then it gets assigned to previous year (2017).
Then, we may build dates for each record using the appropriate year and check if it is within 120 days of SYSDATE.
WITH yourTable AS (
SELECT '0101' AS date_col FROM dual UNION ALL
SELECT '1001' FROM dual UNION ALL
SELECT '1027' FROM dual UNION ALL
SELECT '1215' FROM dual
)
SELECT
date_col
FROM yourTable
WHERE
(date_col <= TO_CHAR(SYSDATE, 'MMDD') AND
TO_DATE(date_col || TO_CHAR(SYSDATE, 'YYYY'), 'MMDDYYYY') >= SYSDATE - 120) OR
(date_col > TO_CHAR(SYSDATE, 'MMDD') AND
TO_DATE(date_col ||
TO_CHAR(TRUNC(ADD_MONTHS(SYSDATE, -12), 'YEAR'), 'YYYY'), 'MMDDYYYY') >=
SYSDATE - 120);
Demo
I am getting the error: ORA-01861: literal does not match format string
I was trying to create a select statement where I can find a the entity_id and name before march 3rd of 2013 and have a status report = W.
SELECT ENTITY_ID,NAME FROM STATUS_TABLE
WHERE DATERETURN <= '2013-03-01'
AND REPORT_STATUS LIKE 'W';
change this:
WHERE DATERETURN <= '2013-03-01'
to this:
WHERE DATERETURN <= to_date('2013-03-01', 'yyyy-mm-dd')
Oracle wont understand literal date format strings. You should to use the TO_DATE function:
WHERE DATERETURN <= TO_DATE('2013-03-01 00:00:00')
or the ANSI syntax
WHERE DATERETURN <= DATE'2014-02-05'
I wan't to get data from week starting date to week ending date by using sql query.I am able to get that too but the query is not running while for different year like say to get all data from "12/27/2015" to "01/02/2015".
My query is :
SELECT * FROM capital
WHERE capital.ACTIVE = 'T'
AND TO_CHAR(capital.CREATE_TIME, 'MM/DD/YYYY') >= '12/27/2015'
AND TO_CHAR(capital.SUBMIT_TIME, 'MM/DD/YYYY') <='01/01/2016'
AND capital.TYPE = 'Fiscal'
Thanks
Please help me friends
change this:
AND TO_CHAR(capital.CREATE_TIME, 'MM/DD/YYYY') >= '12/27/2015'
AND TO_CHAR(capital.SUBMIT_TIME, 'MM/DD/YYYY') <='01/01/2016'
to this
AND capital.CREATE_TIME >= to_date('12/27/2015', 'MM/DD/YYYY' )
AND capital.SUBMIT_TIME <= to_date('01/01/2016', 'MM/DD/YYYY')
AND TO_CHAR(capital.CREATE_TIME, 'MM/DD/YYYY') >= '12/27/2015'
AND TO_CHAR(capital.SUBMIT_TIME, 'MM/DD/YYYY') <='01/01/2016'
CREATE_TIME cannot be greater than SUBMIT_TIME.