Is it possible to turn the current time stamp to a whole number?
Example: If sysdate returns 1/19/2022 5:36:49 PM can I turn that to 1/19/2022 5PM since it falls in the 5PM range.
Here is my query
Select FACILITY, TRK_ID, LOT_DTTM, IN_QTY
from TRK_ID_LOT
WHERE facility in 'DP1DM5'
and trk_id like ('AE%')
and lot_dttm > sysdate - 1
EXAMPLE:
Truncate it to hours:
SQL> select trunc(to_date('1/19/2022 5:36:49 PM', 'mm/dd/yyyy hh:mi:ss pm'), 'hh') res
2 from dual;
RES
----------------------
01/19/2022 05:00:00 PM
SQL>
If you want to update rows, do so using the same function:
update your_table set
date_column = trunc(date_column, 'hh');
Related
Using query #1 below I get the the following results
select DATE, count (DATE) from TABLE1
group by DATE
DATE
COUNT(DATE)
6/6/2022
6856
6/6/2022 2:06:10 PM
78895
6/6/2022 2:06:11 PM
90230
6/6/2022 2:06:12 PM
95693
6/6/2022 2:06:13 PM
94352
6/6/2022 2:06:14 PM
9101
6/27/2022
6854
6/27/2022 7:36:58 PM
6422
Using the above results, I am now trying to write a query which will only isolate the 6/27/2022 dates, which are the two line items at the bottom of my results. I've tried using a to_char function as well as Like function but all variations of the queries that I write result in "No Records". In other words, the query runs but I get no results.
Select * from TABLE where to_char(DATE) like '%06/27/2022%'
Can someone help me with this.
Thank you,
If your column is a DATE data type and you want all the values from one day then:
Select *
from table_name
where date_column >= DATE '2022-06-28'
and date_column < DATE '2022-06-29'
If you want all the values at a particular instant then:
Select *
from table_name
where date_column = DATE '2022-06-28' + INTERVAL '14:06:10' HOUR TO SECOND
or
Select *
from table_name
where date_column = TIMESTAMP '2022-06-28 14:06:10';
or
Select *
from table_name
where TO_CHAR(date_column, 'YYYY-MM-DD HH24:MI:SS') = '2022-06-28 14:06:10';
If your column is a string data type then use the TO_DATE function to convert it to a date data type and then use one of the queries above.
For example:
Select *
from table_name
where TO_DATE(string_column, 'MM/DD/YYYY HH12:MI:SS AM') >= DATE '2022-06-28'
and TO_DATE(string_column, 'MM/DD/YYYY HH12:MI:SS AM') < DATE '2022-06-29'
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.
I am trying to calculate the difference between 2 datetime values where non-work hours are ignored. Originally it just looked at the difference and calculated it as minutes however It needs to count only hours between 9am and 8pm Monday to Friday and 9am - 1pm Saturday, ignoring all other times. I am on an oracle 10g system.
my code as it currently stands is as follows:
begin
debug.debug('sp_access');
update cl_case b
set time_to_sp_access =
(
select (x.date_created-e.date_created)*1440
from cl_case c, eventlog e, eventlog x
where c.id=e.case_id
and x.case_id=e.case_id
and b.id=e.case_id
and e.id=
( select min(id) from eventlog mini
where mini.case_id=e.case_id
and mini.cl_code in ('AAAA','BBBB','CCCC','DDDD')
)
and x.id=
( select min(id) from eventlog minix
where minix.case_id=e.case_id
and minix.cl_code in ('EEEE','FFF','GGG','HHHH','JJJJ','KKKK','LLLL')
)
)
where id in
( select unique case_id
from eventlog elog
where elog.sptime_needs_setting ='Y'
);
commit;
end sp_access;
How can I get this to count time between specified hours?
thanks
You could use a CASE expression in the WHERE clause. Since there are two datetime values, you need to use two case expressions.
For example, the CASE expression would evaluate as:
SQL> SELECT
2 CASE
3 WHEN TO_CHAR(SYSDATE, 'DY') BETWEEN '1' AND '5'
4 THEN TO_DATE(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY')
5 ||' 08:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
6 ELSE TO_DATE(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY')
7 ||' 01:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
8 END my_time
9 FROM dual;
MY_TIME
----------------------
11/24/2015 01:00:00 pm
The above example check the DAY for SYSDATE, and depending on it returns a datetime value.
using the above example, since you have two different datetime values to be compared as a date range condition, you will need two CASE expressions in your WHERE clause.
WHERE date_column
BETWEEN
CASE
WHEN TO_CHAR(date_column, 'DY') BETWEEN '1' AND '5'
THEN
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 09:00:00 AM', 'MM/DD/YYYY HH:MI:SS PM')
ELSE
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 09:00:00 AM', 'MM/DD/YYYY HH:MI:SS PM')
END
AND
CASE
WHEN TO_CHAR(date_column, 'DY') BETWEEN '1' AND '5'
THEN
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 08:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
ELSE
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 01:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
END
My requirement is to convert 15 minute time intervals into HH:MI am/pm format using SQL. Giving an example:
IF interval = 0 => time = 12:00 am
IF interval = 15 => time = 12:15 am
IF interval = 30 => time = 12:30 am
IF interval = 45 => time = 12:45 am
IF interval = 60 => time = 01:00 am
and so on.,
So far, I've only managed to get the hourly intervals converted to HH:MI a/p format i.e. 12:00a, 01:00a etc., Here's a sample of the query that I'm using against my custom table.
START_TIME is the column containing the 15 minute interval entries.
select START_TIME/15,
case when (mod(START_TIME/15,4) = 0) then
case when (mod(START_TIME/15, 48) = 0) then (decode(START_TIME/15,0,'12:00 am',48,'12:00 pm'))
else (
case when (START_TIME/15 < 48) then (START_TIME/ 60 || 'am')
else (START_TIME / 60 - 12 || 'pm')
end
)
end
else ' '
end from MY_TABLE;
Can someone help me get it to work for all 15 min intervals? Thanks.
Try letting oracle do the time adding for you.
By default adding one to a date adds a single day. You want to add the time in your start_time column.
We will start with a date with no time component using TRUNC.
SELECT TRUNC(SYSDATE) FROM DUAL;
Then add 1/(60*24) * START_TIME minutes to this date and print out only the time portion of the date.
select TO_CHAR (TRUNC(SYSDATE) + (1/(60*24) * START_TIME), 'HH:MI AM')
from MY_TABLE
Maybe I don't understand your problem. Oracle interval types already do this.
The purpose of intervals in Oracle are to store time offsets and to allow you to do what you seem to be asking. An interval can be added to a base date/timestamp to produce a new date/timestamp and formatted in any format use date/time formatting functions with TO_CHAR and TO_DATE.
create table intervals(base_time timestamp, offset_interval interval);
SQL> desc intervals
Name Null? Type
----------------------------------------- -------- ----------------------------
BASE_TIME TIMESTAMP(6)
OFFSET_INTERVAL INTERVAL DAY(2) TO SECOND(6)
SQL> insert into intervals values(sysdate, interval '15' minute);
1 row created.
SQL> insert into intervals values(sysdate, interval '30' minute);
1 row created.
SQL> insert into intervals values(sysdate, interval '45' minute);
1 row created.
SQL> insert into intervals values(sysdate, interval '60' minute);
1 row created.
SQL> insert into intervals values(sysdate, interval '90' minute);
1 row created.
SQL> insert into intervals values(sysdate, interval '120' minute);
1 row created.
And then add the base timestamp to the interval, and format with TO_CHAR()
SQL> select to_char(base_time + offset_interval, 'HH:MI AM') from intervals;
TO_CHAR(
--------
03:45 PM
04:00 PM
04:15 PM
04:30 PM
05:00 PM
05:30 PM
6 rows selected.
I need a query output like the below table;
This is a primary entry to a table and these records will be modified by a third party program which I have no control. Can anyone suggest a good sample?
ID | DATEIN | DATEOUT | STATUS
1 02.02.2014 00:00:00 02.02.2014 23:59:59 1
2 03.02.2014 00:00:00 03.02.2014 23:59:59 0
I tried
SELECT To_Char(To_Date(SYSDATE), 'dd-MM-yyyy hh:mm:ss PM'),
To_Char(date_add(To_Date(SYSDATE +1), INTERVAL -1 SECOND), 'dd-MM-yyyy hh:mm:ss PM')
FROM dual
but this query throws an error ORA-00907: missing right parenthesis.
There is no need for PM if you want it to be in 24-hour format. And pay attention to the mask for minutes, it is mi, not mm as in your query. Also as already mentioned no need to convert SYSDATE to date as it is already of that datatype:
SELECT to_char(to_date(SYSDATE), 'dd-mm-yyyy HH24:mi:ss') date_in,
to_char(to_date(SYSDATE + 1) - INTERVAL '1' SECOND, 'dd-mm-yyyy HH24:mi:ss') date_out
FROM dual;
DATE_IN DATE_OUT
------------------- -------------------
11-03-2014 00:00:00 11-03-2014 23:59:59
You can do away with DATE_ADD and TO_DATE functions (SYSDATE is already a DATE, no need of conversion ) , and also use mi to show minute instead of mm which is format specifier for month as in:
SELECT To_Char(SYSDATE, 'dd-MM-yyyy hh:mi:ss PM'),
To_Char((SYSDATE + 1) + INTERVAL '-1' SECOND, 'dd-MM-yyyy hh:mi:ss PM')
FROM dual
I am not clear what you are trying to achieve from the above query but if parenthesis is your only problem then you gotta hit the query:
SELECT To_Char(To_Date((SYSDATE), 'dd-MM-yyyy hh:mm:ss PM')),
To_Char(date_add(To_Date(SYSDATE +1), INTERVAL -1 SECOND), 'dd-MM-yyyy hh:mm:ss PM')
FROM dual