date time filter in sql - sql

I have a report to be generated every hour from a sql query.
The request here is to generate data from yesterday's night 8.00 pm till
today's 12 pm noon . I am using the below query but not getting correct data:
select * from roster_report where job_start > TRUNC(SYSDATE-1) + 20/24 ;
Suppose today's date is 29th April, the report generated on 29th april 11:15 pm is showing data of 29th april 8:00 pm to 11:15 pm correctly.
the data gets incorrect after 00:00 am where it fetches data of 29th april itself and not 30th april.
The query will be used after every hour to generate report.
I am running this in oracle sql developer.

For previous day 8 PM
SELECT to_char(trunc(SYSDATE -1) + 20/24,'mm/dd/yyyy hh24:mi:ss AM') FROM dual;
For Today's 12 PM
SELECT to_char(trunc(SYSDATE) + 12/24,'mm/dd/yyyy hh24:mi:ss AM') FROM dual;
Sample Query
WITH DATA AS
( SELECT 'text' col, SYSDATE create_date FROM dual
)
select *
from tw_logtable
where tw_logtable.created_datetime
BETWEEN (TRUNC(sysdate -1) + 20/24) AND (TRUNC(sysdate) + 12/24)

Try
WHERE
job_start >= ( TRUNC(SYSDATE-1) + INTERVAL '20' HOUR )
AND job_start <= ( TRUNC(SYSDATE) + INTERVAL '12' HOUR )
You could just use > or < if that suits your requirement. BETWEEN can also be used for simplicity if all you want is inclusive range.

Here you have two ways:
SELECT *
FROM ROSTER_REPORT
WHERE JOB_START BETWEEN (TRUNC(SYSDATE - 1) + 20/24)
AND TRUNC(SYSDATE) + 12/24 ;
or
SELECT *
FROM ROSTER_REPORT
WHERE JOB_START BETWEEN TO_DATE(TO_CHAR(SYSDATE-1,'DD/MM/RRRR')||' 20:00','DD/MM/RRRR HH24:MI')
AND TO_DATE(TO_CHAR(SYSDATE ,'DD/MM/RRRR')||' 12:00','DD/MM/RRRR HH24:MI');

Related

Oracle SQL - hour slabs

I have the following data:
CUSTOMER_ORDERS:
CUST_ORDER_NO CREATE_DATETIME
WEB904204965 17-SEP-21 03.12.45.719000000 PM
WEB904204966 17-SEP-21 03.16.45.719000000 PM
I want to check the number of orders in every hour, like:
COUNT SLAB
2 1-2 PM
4 2-3 PM
How to write the query for this?
You can trunc the date/time or extract just the hour component, then GROUP BY that expression.
Here's a test case with sample SQL:
Working Test Case with a little data
SELECT COUNT(*) AS n
, to_char(dt, 'HH') AS slab
FROM test
GROUP BY to_char(dt, 'HH')
;
Result:
We could have used 'HH24' to show the 24 hour value or 'HH AM' to show the 'hours plus AM/PM' indicator.
SELECT to_char(current_date, 'HH24') AS slab FROM dual;
SELECT to_char(current_date, 'HH AM') AS slab FROM dual;

last day/time of month

How do I get the last day of the month to be 11:59:59 pm? Right now it is showing 05/31/2019 12:00 AM. If I just do sysdate it just shows the time I ran it. Thanks.
LAST_DAY(TRUNC(SYSDATE,'MM'))
Try adding one day to the last day of the current month, then subtracting one minute:
SELECT
LAST_DAY(TRUNC(SYSDATE,'MM')) + INTERVAL '1' DAY - INTERVAL '1' SECOND
FROM dual;
Or, if you want to also see the time component, use CURRENT_TIMESTAMP in place of SYSDATE:
SELECT
LAST_DAY(TRUNC(CURRENT_TIMESTAMP,'MM')) + INTERVAL '1' DAY - INTERVAL '1' SECOND
FROM dual;
This outputted:
30.06.2019 23:59:59
The oldfashioned way would be to subtract a second (that's what 1 / (24*60*60) represents as there are 24 hours in a day, 60 minutes in an hour and 60 seconds in a minute) from the first day of the next month (and that's what trunc(add_months(sysdate, 1)) does):
SQL> select sysdate today,
2 trunc(add_months(sysdate, 1)) - 1/(24*60*60) result
3 from dual;
TODAY RESULT
---------------------- ----------------------
06/01/2019 07:52:40 AM 06/30/2019 11:59:59 PM
SQL>
I would use:
SELECT TRUNC(SYSDATE, 'MM') + INTERVAL '1' MONTH - INTERVAL '1' SECOND
FROM dual;
(This has one less step than Tim's solution.)
Or in the older-fashioned method:
SELECT ADD_MONTHS(TRUNC(SYSDATE, 'MM'), 1) - 1/(24*60*60)
If you convert this to a timestamp, you will see the time value:
SELECT CAST(TRUNC(sysdate, 'MM') + INTERVAL '1' MONTH - INTERVAL '1' SECOND as TIMESTAMP)
FROM dual;

SQL query data between dates, efficiency

I'm trying to search through a fairly large (56m+ row) table using an SQL query. The complication to just being able to do some quick SQL query like this:
Select *COLUMNS*
From *Table*
Where *Conditions* And
LOG_ENTRY_TIMESTAMP between {StartDate} and {EndDate}
is that I need to pull the 23:00 - 24:00 hour from the day before {StartDate} without pulling the rest of the data from that date. {StartDate} and {EndDate} are user entered fields in a DATE format. LOG_ENTRY_TIMESTAMP is a TIMESTAMP data type.
Is there a more time-efficient way of doing this than having to do something like:
TRUNC(CAST(LOG_ENTRY_TIMESTAMP AS DATE), 'HH') BETWEEN {StartDate}-1/24 and {EndDate}+23/24
Data will look like:
ITEM LOG_ENTRY_TIMESTAMP
---- ----------------------------------
A 2/12/2018 10:02:19.214528 AM -0500
B 2/14/2018 11:02:19.224528 PM -0500
C 2/16/2018 01:02:19.412528 AM -0500
D 2/16/2018 11:02:19.412528 PM -0500
And if I search from {StartDate} = 2/15/2018 through {EndDate} = 2/16/2018, I want to capture B & C.
I would suggest:
Where *Conditions* And
LOG_ENTRY_TIMESTAMP between {StartDate} - 1/24 and {EndDate}
I need data from 11pm the night before until 11pm tonight
if I search from {StartDate} = 2/15/2018 through {EndDate} = 2/16/2018, I want to capture B & C.
Assuming LOG_ENTRY_TIMESTAMP is indexed you can utilise that with:
Where *Conditions* And
LOG_ENTRY_TIMESTAMP >= {StartDate} - 1/24 and
LOG_ENTRY_TIMESTAMP < {EndDate} + 23/24
Again assuming that the variables are actually dates with time set to midnight, {StartDate} - 1/24 gives you 23:00 on the day before that start date, and {EndDate} + 23/24 gives you 23:00 on the end date.
With your sample data in a CTE and the filter dates as date literals:
with your_table (item, log_entry_timestamp) as (
select 'A', to_timestamp_tz('2/12/2018 10:02:19.214528 AM -0500',
'MM/DD/YYYY HH:MI:SS.FF6 AM TZHTZM') from dual
union all select 'B', to_timestamp_tz('2/14/2018 11:02:19.224528 PM -0500',
'MM/DD/YYYY HH:MI:SS.FF6 AM TZHTZM') from dual
union all select 'C', to_timestamp_tz('2/16/2018 01:02:19.412528 AM -0500',
'MM/DD/YYYY HH:MI:SS.FF6 AM TZHTZM') from dual
union all select 'D', to_timestamp_tz('2/16/2018 11:02:19.412528 PM -0500',
'MM/DD/YYYY HH:MI:SS.FF6 AM TZHTZM') from dual
)
select *
from your_table
where LOG_ENTRY_TIMESTAMP >= date '2018-02-15' - 1/24
and LOG_ENTRY_TIMESTAMP < date '2018-02-16' + 23/24;
I LOG_ENTRY_TIMESTAMP
- ---------------------------------
B 2018-02-14 23:02:19.224528 -05:00
C 2018-02-16 01:02:19.412528 -05:00
But you need to verify what the actual data types are for the values being used for the variables, and whether any time zone conversion is being done, which could affect the range of values you actually match.

Oracle Add 1 hour in SQL

I am just trying to add 1 hour to a value, it is kind of complicated on where and why i am doing this but basically i just need to query something like this
select DATE_ADD(hh,1,'2014-10-15 03:30:00 pm') from dual
I keep reading old articles that say to use dateAdd or date_add but I keep getting invalid identifier errors.
select sysdate + 1/24 from dual;
sysdate is a function without arguments which returns DATE type
+ 1/24 adds 1 hour to a date
select to_char(to_date('2014-10-15 03:30:00 pm', 'YYYY-MM-DD HH:MI:SS pm') + 1/24, 'YYYY-MM-DD HH:MI:SS pm') from dual;
Use an interval:
select some_date_column + interval '1' hour
from your_table;
You can use INTERVAL type or just add calculated number value - "1" is equal "1 day".
first way:
select date_column + INTERVAL '0 01:00:00' DAY TO SECOND from dual;
second way:
select date_column + 1/24 from dual;
First way is more convenient when you need to add a complicated value - for example, "1 day 3 hours 25 minutes 49 seconds".
See also: http://www.oracle-base.com/articles/misc/oracle-dates-timestamps-and-intervals.php
Also you have to remember that oracle have two interval types - DAY TO SECOND and YEAR TO MONTH.
As for me, one interval type would be better, but I hope people in oracle knows, what they do ;)
Old way:
SELECT DATE_COLUMN + 1 is adding a day
SELECT DATE_COLUMN + N /24 to add hour(s) - N being number of hours
SELECT DATE_COLUMN + N /1440 to add minute(s) - N being number of minutes
SELECT DATE_COLUMN + N /86400 to add second(s) - N being number of seconds
Using INTERVAL:
SELECT DATE_COLUMN + INTERVAL 'N' HOUR or MINUTE or SECOND - N being a number of hours or minutes or seconds.
To add/subtract from a DATE, you have 2 options :
Method #1 :
The easiest way is to use + and - to add/subtract days, hours, minutes, seconds, etc.. from a DATE, and ADD_MONTHS() function to add/subtract months and years from a DATE. Why ? That's because from days, you can get hours and any smaller unit (1 hour = 1/24 days), (1 minute = 1/1440 days), etc... But you cannot get months and years, as that depends on the month and year themselves, hence ADD_MONTHS() and no add_years(), because from months, you can get years (1 year = 12 months).
Let's try them :
SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints current date: 19-OCT-2019 20:42:02
SELECT TO_CHAR((SYSDATE + 1/24), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 hour: 19-OCT-2019 21:42:02
SELECT TO_CHAR((SYSDATE + 1/1440), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 minute: 19-OCT-2019 20:43:02
SELECT TO_CHAR((SYSDATE + 1/86400), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 second: 19-OCT-2019 20:42:03
-- Same goes for subtraction.
SELECT SYSDATE FROM dual; -- prints current date: 19-OCT-19
SELECT ADD_MONTHS(SYSDATE, 1) FROM dual; -- prints date + 1 month: 19-NOV-19
SELECT ADD_MONTHS(SYSDATE, 12) FROM dual; -- prints date + 1 year: 19-OCT-20
SELECT ADD_MONTHS(SYSDATE, -3) FROM dual; -- prints date - 3 months: 19-JUL-19
Method #2 : Using INTERVALs, you can or subtract an interval (duration) from a date easily. More than that, you can combine to add or subtract multiple units at once (e.g 5 hours and 6 minutes, etc..)
Examples :
SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints current date: 19-OCT-2019 21:34:15
SELECT TO_CHAR((SYSDATE + INTERVAL '1' HOUR), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 hour: 19-OCT-2019 22:34:15
SELECT TO_CHAR((SYSDATE + INTERVAL '1' MINUTE), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 minute: 19-OCT-2019 21:35:15
SELECT TO_CHAR((SYSDATE + INTERVAL '1' SECOND), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 second: 19-OCT-2019 21:34:16
SELECT TO_CHAR((SYSDATE + INTERVAL '01:05:00' HOUR TO SECOND), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 hour and 5 minutes: 19-OCT-2019 22:39:15
SELECT TO_CHAR((SYSDATE + INTERVAL '3 01' DAY TO HOUR), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 3 days and 1 hour: 22-OCT-2019 22:34:15
SELECT TO_CHAR((SYSDATE - INTERVAL '10-3' YEAR TO MONTH), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date - 10 years and 3 months: 19-JUL-2009 21:34:15
The calculation is simple
if you want to add 1 hour in the date .
every day have 24 hour , you can add .
select sysdate + 1/24 from dual;
if you want 1 day to add
select sysdate + 24/24 from dual;
or
select sysdate + 1 from dual;
same as for 2, 3 , 4 day
For static date you have the answer below.

Postgresql generate_series of months

I'm trying to generate a series in PostgreSQL with the generate_series function. I need a series of months starting from Jan 2008 until current month + 12 (a year out). I'm using and restricted to PostgreSQL 8.3.14 (so I don't have the timestamp series options in 8.4).
I know how to get a series of days like:
select generate_series(0,365) + date '2008-01-01'
But I am not sure how to do months.
select DATE '2008-01-01' + (interval '1' month * generate_series(0,11))
Edit
If you need to calculate the number dynamically, the following could help:
select DATE '2008-01-01' + (interval '1' month * generate_series(0,month_count::int))
from (
select extract(year from diff) * 12 + extract(month from diff) + 12 as month_count
from (
select age(current_timestamp, TIMESTAMP '2008-01-01 00:00:00') as diff
) td
) t
This calculates the number of months since 2008-01-01 and then adds 12 on top of it.
But I agree with Scott: you should put this into a set returning function, so that you can do something like select * from calc_months(DATE '2008-01-01')
You can interval generate_series like this:
SELECT date '2014-02-01' + interval '1' month * s.a AS date
FROM generate_series(0,3,1) AS s(a);
Which would result in:
date
---------------------
2014-02-01 00:00:00
2014-03-01 00:00:00
2014-04-01 00:00:00
2014-05-01 00:00:00
(4 rows)
You can also join in other tables this way:
SELECT date '2014-02-01' + interval '1' month * s.a AS date, t.date, t.id
FROM generate_series(0,3,1) AS s(a)
LEFT JOIN <other table> t ON t.date=date '2014-02-01' + interval '1' month * s.a;
You can interval generate_series like this:
SELECT TO_CHAR(months, 'YYYY-MM') AS "dateMonth"
FROM generate_series(
'2008-01-01' :: DATE,
'2008-06-01' :: DATE ,
'1 month'
) AS months
Which would result in:
dateMonth
-----------
2008-01
2008-02
2008-03
2008-04
2008-05
2008-06
(6 rows)
Well, if you only need months, you could do:
select extract(month from days)
from(
select generate_series(0,365) + date'2008-01-01' as days
)dates
group by 1
order by 1;
and just parse that into a date string...
But since you know you'll end up with months 1,2,..,12, why not just go with select generate_series(1,12);?
In the generated_series() you can define the step, which is one month in your case. So, dynamically you can define the starting date (i.e. 2008-01-01), the ending date (i.e. 2008-01-01 + 12 months) and the step (i.e. 1 month).
SELECT generate_series('2008-01-01', '2008-01-01'::date + interval '12 month', '1 month')::date AS generated_dates
and you get
1/1/2008
2/1/2008
3/1/2008
4/1/2008
5/1/2008
6/1/2008
7/1/2008
8/1/2008
9/1/2008
10/1/2008
11/1/2008
12/1/2008
1/1/2009