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.
Related
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.
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'
I have a question regarding getting yesterday's data in oracle sql
my query is something like this:
SELECT
SUM(CURRENT_WEIGHT)
FROM
FABRICATION_HIST
WHERE
FAB_ENTRY_DATE = TO_DATE(SYSDATE-1, 'MM/DD/YYYY')
The value in FAB_ENTRY_DATE is like this:
8/25/2014 3:03:11 PM
And it doesn't show any data when I execute the query, Please help me
SELECT
SUM(CURRENT_WEIGHT)
FROM
FABRICATION_HIST
WHERE
to_date(FAB_ENTRY_DATE, 'MM/DD/YYYY') = TO_DATE(SYSDATE-1, 'MM/DD/YYYY')
[You should match only the date part]
or
SELECT
SUM(CURRENT_WEIGHT)
FROM
FABRICATION_HIST
WHERE
FAB_ENTRY_DATE >= (SYSDATE - 1) and FAB_ENTRY_DATE < SYSDATE;
More efficient (if FAB_ENTRY_DATE has index - uses it):
SELECT SUM(CURRENT_WEIGHT)
FROM FABRICATION_HIST
WHERE FAB_ENTRY_DATE >= TRUNC(SYSDATE)-1
AND FAB_ENTRY_DATE < TRUNC(SYSDATE)
NOTE:
Don't use TO_DATE(SYSDATE, 'MM/DD/YYYY') to drop time part:
as it works only if default date format on database is 'MM/DD/YYYY', if not - you get errors like this:
ORA-01843: not a valid month : SELECT TO_DATE(SYSDATE, 'MM/DD/YYYY') FROM DUAL
You have equality. 8/25/2014 3:03:11 PM won't be equal to 8/25/2014
You need to specify a range:
WHERE FAB_ENTRY_DATE >= TO_DATE(SYSDATE-1, 'MM/DD/YYYY')
AND FAB_ENTRY_DATE < TO_DATE(SYSDATE, 'MM/DD/YYYY')
Try this:
SELECT
SUM(CURRENT_WEIGHT)
FROM
FABRICATION_HIST
WHERE
TO_CHAR(FAB_ENTRY_DATE, 'MM/DD/YYYY') = TO_CHAR(SYSDATE-1, 'MM/DD/YYYY')
Since you don't need to get the sum on a span of dates (between two dates), you can compare the date that you want as varchar without the Time portion. "8/25/2014" = "8/25/2014"
you can trunc data column:
SELECT
SUM(CURRENT_WEIGHT)
FROM
FABRICATION_HIST
WHERE
to_date(FAB_ENTRY_DATE, 'MM/DD/YYYY') = trunc(SYSDATE) - 1
see Oracle's documentation
I have a column named StartDate containing a date in this format: 03-03-2012 15:22
What I need is to convert it to date. It should be looking like this: DD/MM/YYYY
What I have tried without success is:
select
p1.PA_VALUE as StartDate,
p2.PA_VALUE as EndDate
from WP_Work p
LEFT JOIN PARAMETER p1 on p1.WP_ID=p.WP_ID AND p1.NAME = 'StartDate'
LEFT JOIN PARAMETER p2 on p2.WP_ID=p.WP_ID AND p2.NAME = 'Date_To'
WHERE p.TYPE = 'EventManagement2'
AND TO_DATE(p1.PA_VALUE, 'DD/MM/YYYY') >= TO_DATE('25/10/2012', 'DD/MM/YYYY')
AND TO_DATE(p2.PA_VALUE, 'DD/MM/YYYY') <= TO_DATE('26/10/2012', 'DD/MM/YYYY')
Is there a way to do this?
EDIT1: the PA_VALUE column is: VARCHAR2
You can use TRUNC on DateTime to remove Time part of the DateTime. So your where clause can be:
AND TRUNC(p1.PA_VALUE) >= TO_DATE('25/10/2012', 'DD/MM/YYYY')
The TRUNCATE (datetime) function returns date with the time portion of
the day truncated to the unit specified by the format model.
When you convert your string to a date you need to match the date mask to the format in the string. This includes a time element, which you need to remove with truncation:
select
p1.PA_VALUE as StartDate,
p2.PA_VALUE as EndDate
from WP_Work p
LEFT JOIN PARAMETER p1 on p1.WP_ID=p.WP_ID AND p1.NAME = 'StartDate'
LEFT JOIN PARAMETER p2 on p2.WP_ID=p.WP_ID AND p2.NAME = 'Date_To'
WHERE p.TYPE = 'EventManagement2'
AND trunc(TO_DATE(p1.PA_VALUE, 'DD-MM-YYYY HH24:MI')) >= TO_DATE('25/10/2012', 'DD/MM/YYYY')
AND trunc(TO_DATE(p2.PA_VALUE, 'DD-MM-YYYY HH24:MI')) <= TO_DATE('26/10/2012', 'DD/MM/YYYY')
Outside the scope of the question, but storing dates as strings is bad practice, and storing date times is even worse.
We need to convert the strings to dates in order to do any form of date processing (arithmetic, interval assessment, etc) on them
Strings offer no guarantees regarding format, so we run the risk of date corruption crashing our code. We can defend against this by employing VALIDATE_CONVERSION() (available since 12c, find out more ) but it's still a PITN
Using non-standard datatypes makes it harder to reason about the data model and the code we build over it.
We can use TRUNC function in Oracle DB. Here is an example.
SELECT TRUNC(TO_DATE('01 Jan 2018 08:00:00','DD-MON-YYYY HH24:MI:SS')) FROM DUAL
Output:
1/1/2018
Try
SELECT to_char(p1.PA_VALUE,'DD/MM/YYYY') as StartDate,
to_char(p2.PA_VALUE,'DD/MM/YYYY') as EndDate
...
If your column with DATE datatype has value like below : -
value in column : 10-NOV-2005 06:31:00
Then, You can Use TRUNC function in select query to convert your date-time value to only date like - DD/MM/YYYY or DD-MON-YYYY
select TRUNC(column_1) from table1;
result : 10-NOV-2005
You will see above result - Provided that NLS_DATE_FORMAT is set as like below :-
Alter session NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';
there is also extended usage like this:
WITH dates AS (
SELECT date'2015-01-01' d FROM dual union
SELECT date'2015-01-10' d FROM dual union
SELECT date'2015-02-01' d FROM dual union
SELECT timestamp'2015-03-03 23:45:00' d FROM dual union
SELECT timestamp'2015-04-11 12:34:56' d FROM dual
)
SELECT d "Original Date",
trunc(d) "Nearest Day, Time Removed",
trunc(d, 'ww') "Nearest Week",
trunc(d, 'iw') "Start of Week",
trunc(d, 'mm') "Start of Month",
trunc(d, 'year') "Start of Year"
FROM dates;
Oracle Offical Help Page
I have the query below where the date is hard-coded. My objective is to remove the harcoded date; the query should pull the data for the previous month when it runs.
select count(distinct switch_id)
from xx_new.xx_cti_call_details#appsread.prd.com
where dealer_name = 'XXXX'
and TRUNC(CREATION_DATE) BETWEEN '01-AUG-2012' AND '31-AUG-2012'
Should I use sysdate-15 function for that?
Modifying Ben's query little bit,
select count(distinct switch_id)
from xx_new.xx_cti_call_details#appsread.prd.com
where dealer_name = 'XXXX'
and creation_date between add_months(trunc(sysdate,'mm'),-1) and last_day(add_months(trunc(sysdate,'mm'),-1))
The trunc() function truncates a date to the specified time period; so trunc(sysdate,'mm') would return the beginning of the current month. You can then use the add_months() function to get the beginning of the previous month, something like this:
select count(distinct switch_id)
from xx_new.xx_cti_call_details#appsread.prd.com
where dealer_name = 'XXXX'
and creation_date >= add_months(trunc(sysdate,'mm'),-1)
and creation_date < trunc(sysdate, 'mm')
As a little side not you're not explicitly converting to a date in your original query. Always do this, either using a date literal, e.g. DATE 2012-08-31, or the to_date() function, for example to_date('2012-08-31','YYYY-MM-DD'). If you don't then you are bound to get this wrong at some point.
You would not use sysdate - 15 as this would provide the date 15 days before the current date, which does not seem to be what you are after. It would also include a time component as you are not using trunc().
Just as a little demonstration of what trunc(<date>,'mm') does:
select sysdate
, case when trunc(sysdate,'mm') > to_date('20120901 00:00:00','yyyymmdd hh24:mi:ss')
then 1 end as gt
, case when trunc(sysdate,'mm') < to_date('20120901 00:00:00','yyyymmdd hh24:mi:ss')
then 1 end as lt
, case when trunc(sysdate,'mm') = to_date('20120901 00:00:00','yyyymmdd hh24:mi:ss')
then 1 end as eq
from dual
;
SYSDATE GT LT EQ
----------------- ---------- ---------- ----------
20120911 19:58:51 1
Data for last month-
select count(distinct switch_id)
from xx_new.xx_cti_call_details#appsread.prd.com
where dealer_name = 'XXXX'
and to_char(CREATION_DATE,'MMYYYY') = to_char(add_months(trunc(sysdate),-1),'MMYYYY');
I believe this would also work:
select count(distinct switch_id)
from xx_new.xx_cti_call_details#appsread.prd.com
where
dealer_name = 'XXXX'
and (creation_date BETWEEN add_months(trunc(sysdate,'mm'),-1) and trunc(sysdate, 'mm'))
It has the advantage of using BETWEEN which is the way the OP used his date selection criteria.
It is working with me in Oracle sql developer
SELECT add_months(trunc(sysdate,'mm'), -1),
last_day(add_months(trunc(sysdate,'mm'), -1))
FROM dual
Getting last nth months data retrieve
SELECT * FROM TABLE_NAME
WHERE DATE_COLUMN BETWEEN '&STARTDATE' AND '&ENDDATE';