Getting sum of the data from yesterday's - sql

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

Related

select query help update

I have the below select query for daily data, how do I amend to make it pull the full month of data
select *
from BAU_SDT_INV_USER
where trunc(CHECKED_DATE) = to_date('2018/12/18', 'YYYY/MM/DD')
If you want the full month in Oracle:
where checked_date >= date '2018-12-01' and
checked_date < date '2019-01-01'
Maybe something like this?
select * from BAU_SDT_INV_USER
where CHECKED_DATE >= to_date('2018/12/01', 'YYYY/MM/DD')
and CHECKED_DATE < to_date('2019/01/01', 'YYYY/MM/DD')
If performance is not a key point for your query, just trunc by month on both ends of the comparison :
select *
from BAU_SDT_INV_USER
where trunc(CHECKED_DATE, 'mm') = trunc( to_date('2018/12/18', 'YYYY/MM/DD'), 'mm')
select *
from BAU_SDT_INV_USER
where trunc(CHECKED_DATE) BETWEEN to_date('2018/12/01', 'YYYY/MM/DD')
AND to_date('2018/12/31', 'YYYY/MM/DD')
More Index Friendly approach would be
select *
from BAU_SDT_INV_USER
where CHECKED_DATE between ADD_MONTHS(sysdate, -1) and sysdate
EDIT Please note that between clause is inclusive. you can also use
select *
from BAU_SDT_INV_USER
where CHECKED_DATE >= ADD_MONTHS(to_date(:inputDate, 'YYYY/MM/DD'), -1)
and CHECKED_DATE < TO_DATE(:input_Date,'YYYY/MM/DD');

to get all data from start date and end date

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.

Error with selecting sysdate between time range

I want to fetch the data for sysdate-1 '00:00:00' between '23:59:59' time range. I have tried, but i am getting error.
This is my query..
select id_value, tn, LINE_TYPE_ID, ACCOUNT_TYPE_ID, PORT_ID, CURRENT_STATUS_ID, ERROR_CODE, ERROR_DESC, CRM_APP_CODE
from np_tn_dtls a
join np_port_req_dtls b on A.NP_TXN_ID=B.NP_TXN_ID
left join np_subscriber_dtls c on a.NP_TXN_ID=C.NP_TXN_ID
left join np_subscriber_id_dtls d on D.SUBSCRIBER_ID=C.SUBSCRIBER_ID
where (b.REQUEST_START_TIME between sysdate-1 '00:00:00'
AND sysdate '23:59:59') and (b.port_type_id != 2);
Please suggest me.
This expression is wrong:
between sysdate-1 '00:00:00'
AND sysdate '23:59:59'
Use trunc to round the date, so you can write this:
between trunc(sysdate-1) AND trunc(sysdate)
However, between is up to and including, so to prevent getting also request that happened exactly at midnight today, you can better write it like this:
b.REQUEST_START_TIME >= trunc(sysdate-1) AND
b.REQUEST_START_TIME < trunc(sysdate)
sysdate '23:59:59' is not a valid construction
use
b.REQUEST_START_TIME >= trunc(sysdate-1)
AND b.REQUEST_START_TIME < trunc(sysdate)

get data on particular timings of the day in a month in SQL

How can I retrieve data on time range where tickets issued between the times of 6am - 9:15am for the past month.
I tried this but was wrong.Its in oracle and to_char is not function name in SQL. how can i do that in sql.
select *
from [ICPS].[dbo].[tickets]
where t_date_time_issued > sysdate - 30
and to_char(t_date_time_issed, 'hh24:mi:ss') >= '06:00:00'
and to_char(t_date_time_issued, 'hh24:mi:ss') <= '09:15:00'
Assuming that you want it in SQL-Server("Its in oracle and to_char is not function name in SQL"), you can cast the datetme to time:
SELECT *
FROM [dbo].[tickets]
WHERE t_date_time_issued > DATEADD(mm, - 30, GetDate())
AND Cast(t_date_time_issued as TIME) between '06:00' and '09:10'
Demo
You can use to_timestamp() in stead of to_char. Details about to_timestamp() is: http://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_2118.htm#OLADM696
Hope will help!
try this:
SELECT
*
FROM
[ICPS].[DBO].[tickets]
WHERE
t_date_time_issued BETWEEN add_months(TRUNC(SYSDATE, 'MM'), - 1) AND (
TRUNC(SYSDATE, 'MM') - (1/24/60/60))
AND TO_CHAR(t_date_time_issed, 'HH24:MI:SS') >= '06:00:00'
AND TO_CHAR(t_date_time_issed, 'HH24:MI:SS') <= '09:15:00'
SELECT *
FROM [ICPS].[dbo].[tickets]
WHERE convert (datetime,t_date_time_issued,101)
between convert(datetime,'2013/11/01',101) and convert (datetime,'2013/12/04',101)
AND datepart(hour,t_date_time_issued) between '06' and '09'

How do I get the current year using SQL on Oracle?

I need to add the current year as a variable in an SQL statement, how can I retrieve the current year using SQL?
i.e.
BETWEEN
TO_DATE('01/01/**currentYear** 00:00:00', 'DD/MM/YYYY HH24:MI:SS')
AND
TO_DATE('31/12/**currentYear** 23:59:59', 'DD/MM/YYYY HH24:MI:SS')
Using to_char:
select to_char(sysdate, 'YYYY') from dual;
In your example you can use something like:
BETWEEN trunc(sysdate, 'YEAR')
AND add_months(trunc(sysdate, 'YEAR'), 12)-1/24/60/60;
The comparison values are exactly what you request:
select trunc(sysdate, 'YEAR') begin_year
, add_months(trunc(sysdate, 'YEAR'), 12)-1/24/60/60 last_second_year
from dual;
BEGIN_YEAR LAST_SECOND_YEAR
----------- ----------------
01/01/2009 31/12/2009
Another option is:
SELECT *
FROM TABLE
WHERE EXTRACT( YEAR FROM date_field) = EXTRACT(YEAR FROM sysdate)
Use extract(datetime) function it's so easy, simple.
It returns year, month, day, minute, second
Example:
select extract(year from sysdate) from dual;
Yet another option would be:
SELECT * FROM mytable
WHERE TRUNC(mydate, 'YEAR') = TRUNC(SYSDATE, 'YEAR');
Since we are doing this one to death - you don't have to specify a year:
select * from demo
where somedate between to_date('01/01 00:00:00', 'DD/MM HH24:MI:SS')
and to_date('31/12 23:59:59', 'DD/MM HH24:MI:SS');
However the accepted answer by FerranB makes more sense if you want to specify all date values that fall within the current year.
Why not use YEAR function?
SELECT * FROM table WHERE YEAR(date_field)=YEAR(SYSDATE);
To display the current system date in oracle-sql
select sysdate from dual;