oracle between clause inclusive format HH24:MI - sql

My query looks something like:
select *
from mytable
where date_field between to_date(#from#, 'YYYY/MM/DD HH24:MI')
and to_date(#to#, 'YYYY/MM/DD HH24:MI')
As an example:
if from = 2012/07/18 00:00 and
to = 2012/07/18 00:09
will this include records with timestamp 2012/07/18 00:09:01 to 2012/07/18 00:09:59?
or should I change the statement to:
select *
from mytable
where date_field >= to_date(#from#, 'YYYY/MM/DD HH24:MI')
< to_date(#to#, 'YYYY/MM/DD HH24:MI')
here substituting from : 2012/07/18 00:00 & to: 2012/07/18 00:10 should give me all records with timestamp between midnight & 9M59S past midnight, which is what I want.

The between clause accepts both the interval bounds.
I suggest the second option to you
select *
from mytable
where date_field >= to_date(#from#, 'YYYY/MM/DD HH24:MI')
< to_date(#to#, 'YYYY/MM/DD HH24:MI')
You may find this article interesting.

The date conversion is going to convert the values into dates, which contain all date elements. You have not specified seconds in the strings, so these will become 0.
In other words, the range ":01" - ":59" is not included.
Since you are working with strings and the strings have date elements in the proper order for comparison, why not do string compares instead:
where to_char(datefield, 'YYYY/MM/DD HH24:MI') between #from# and #to#
I think this does exactly what you want, without fiddling around with date arithmetic.
You can also change the statement as you propose, by incrementing the #to# column and using "<" instead of between.

You could do something like this:
SELECT *
FROM mytable
WHERE date_field between to_date(#from#, 'YYYY/MM/DD HH24:MI')
AND to_date(#to#||':59', 'YYYY/MM/DD HH24:MI:SS')

Ignoring the date portion of the DATE elements, since both 00:09:01 and 00:09:59 come after your "to" time of 00:09:00, no, this query will not include those records.
If you want to include those records, you will need to extend your "to" time to 00:10:00 or TRUNC your records's timestamps to the nearest minute.
Edit:
If your from and to are only accurate to the minute, I'd do this:
SELECT *
FROM mytable
WHERE date_field >= to_date(#from#, 'YYYY/MM/DD HH24:MI')
AND date_field < to_date(#to#, 'YYYY/MM/DD HH24:MI') + 1/24/60/60 /* 1 minute */
And make sure you use bind variable for from and to. Is this ColdFusion? If so, use cfqueryparam.

Related

How to add hours to date in 24 hours format

I would like to add, for example, 8 hours to the enddate in a 24 hour format.
I tried adding + 8/24, 'DD-MM-YYYY HH24:MI:SS' on the first line but this gives an error.
This is my query thus far.
SELECT to_char(IN_ENDDATE, 'DD-MM-YYYY HH24:MI:SS')
INTO IN_END_DATE_STRING
FROM DUAL;
Your first line converts a date to a string. You cannot then add 8/24 to it. Do the addition before the conversion:
SELECT to_char(IN_ENDDATE + 8/24.0, 'DD-MM-YYYY HH24:MI:SS')
INTO IN_END_DATE_STRING
FROM DUAL;
IN_ENDDATE really does need to be a date type to allow +8/24 to work. If it's a timestamp, add it as an interval:
IN_ENDDATE + INTERVAL '8' HOUR
This form might be safer to use for a couple of reasons:
it works on both date and timestamps
it's more readable
If IN_ENDDATE is a non-date type (eg varchar) then your query works without the +8/24 because it is being successfully implicitly converted from varchar to date, before being passed to to_char. In this case either be explicit about your conversion:
SELECT to_char(to_date(IN_ENDDATE, 'YYMMDD WHATEVER') + 8/24.0, 'DD-MM-YYYY HH24:MI:SS')
INTO IN_END_DATE_STRING
FROM DUAL
SELECT to_char(to_date(IN_ENDDATE, 'YYMMDD WHATEVER') + INTERVAL '8' HOUR, 'DD-MM-YYYY HH24:MI:SS')
INTO IN_END_DATE_STRING
FROM DUAL
Or set your IN_ENDDATE parameter to really be a date type

CAST(CURRENT_TIMESTAMP AS TIME)

i am trying to work with timestamps and manipulate the data out of them so i can measure different things in our database.
currently i have a column that holds a "DATE" but in fact contains a whole stamp 'DD/MM/YYYY HH24:MI:SS'
i want to be able to grab all entries where "DTIME" between "09:00" and "09:15" however have not been able to cast it correctly.
If i was to output the column without any conversion it would look like this
SELECT ia.DTIME3
FROM ISIS_AUDIT ia
WHERE ia.DTIME3 like to_date('24/01/2019', 'DD/MM/YYYY');
OUTPUT: 24/JAN/19
if i was to convert it to_char,
SELECT to_char(ia.DTIME3, 'DD/MM/YYYY HH24:MI:SS')
FROM ISIS_AUDIT ia
WHERE ia.DTIME3 like to_date('24/01/2019', 'DD/MM/YYYY');
OUTPUT: 24/01/2019 07:10:52
I want to be able to take this DTIME3 and find entries between the times but CAST and CONVERT to TIME doesnt work.
This is my working option but it outputs the date still and i dont want to have to specify the date so it can be run across any day of the week.
WHERE ia.DTIME3 between to_date('24/01/2019 09:00:00', 'DD/MM/YYYY HH24:MI:SS') and to_date('24/01/2019 09:15:00', 'DD/MM/YYYY HH24:MI:SS');
OUTPUT: 24/01/2019 09:00:01
currently i have a column that holds a "DATE" but in fact contains a whole stamp 'DD/MM/YYYY HH24:MI:SS'
That's what Oracle DATE datatype does : storing a date and time (without fractional seconds, that belong to the TIMESTAMP datatypes). There is no specific format in Oracle for date only (without time).
To filter on the time of the day, you can use the TO_CHAR() function to convert your date to a string that represents its time, and that you can compare :
TO_CHAR(ia.DTIME3, 'hh24:mi') BETWEEN '09:00' AND '09:14'
You can also CAST the date to a timestamp and use the EXTRACT() function :
EXTRACT(HOUR FROM CAST(ia.DTIME3 AS TIMESTAMP)) = 9
AND EXTRACT(MINUTE FROM CAST(ia.DTIME3 AS TIMESTAMP)) < 15
Oracle has DATE and TIMESTAMP data types; both have year, month, day, hour, minute, second components (TIMESTAMP also has fractional seconds). Oracle does not have a TIME data type.
Instead, use TRUNC() to truncate the time component to midnight and add an interval literal:
SELECT *
FROM ISIS_AUDIT
WHERE DTIME BETWEEN TRUNC( DTIME ) + INTERVAL '09:00' HOUR TO MINUTE
AND TRUNC( DTIME ) + INTERVAL '09:15' HOUR TO MINUTE;
and ia.DTIME1 not between to_date('&&S_DATE 08:50:00', 'DD/MM/YYYY HH24:MI:SS') and to_date('&&S_DATE 09:20:00', 'DD/MM/YYYY HH24:MI:SS')
and ia.DTIME1 not between to_date('&&S_DATE 11:50:00', 'DD/MM/YYYY HH24:MI:SS') and to_date('&&S_DATE 12:40:00', 'DD/MM/YYYY HH24:MI:SS')
and ia.DTIME3 not between to_date('&&S_DATE 08:50:00', 'DD/MM/YYYY HH24:MI:SS') and to_date('&&S_DATE 09:20:00', 'DD/MM/YYYY HH24:MI:SS')
and ia.DTIME3 not between to_date('&&S_DATE 11:50:00', 'DD/MM/YYYY HH24:MI:SS') and to_date('&&S_DATE 12:40:00', 'DD/MM/YYYY HH24:MI:SS')
i was trying to do this way however i noticed if i have a start time of 08:30 and a finish time of 09:30 then it falls between 09:00 and 09:15 so i want to exclude it from the table. the current WHERE clause im using is quite specific and will only exclude if the timestamp contains the values specified.
Really it would want to read "DTIME1 to DTIME3 not between 09:00 and 09:15"

Convert Number into Date format in oracle

I have number column in oracle database which stores a timestamp. I want to convert this into a DATE and I have no clue on how to do it.
Below is what I am looking for, please suggest.
The value 1465484486246 should be converted to 2016/06/09 15:01:26,246 GMT
You can use NUMTODSINTERVAL along with to_date to achieve what you want:
TO_CHAR(TO_DATE('1970/01/01 00:00:00', 'YYYY/MM/DD HH24:MI:SS') + NUMTODSINTERVAL(col / 1000,'SECOND'),
'YYYY/MM/DD HH24:MI:SS')
Here I assume that your timestamp column is called col. The timestamp 1465484486246 you gave us is in milliseconds, which is why I used col / 1000 in NUMTODSINTERVAL.

Why does this fail Q

I am running an SQL statement with this to create a field and I get the desired result:
TO_CHAR(TRUNC(CURRENT_DATE - 8/24,'hh24'), 'DD-Mon-yyyy hh24:mi') AS "DATEVAR"
When I add this to my where statement in the SQL, I get results with the correct time period but not in the MMDDYY scope. What gives?
WHERE
TO_CHAR(MOPACTIVITY.MOPEND, 'dd-mon-yyyy hh24:mi:') < TO_CHAR(TRUNC(CURRENT_DATE - 8/24,'hh24'), 'DD-Mon-yyyy hh24:mi')
You genius is greatly appreciated.
Respectfully,
Jonathan Morningstar
You are comparing dd-mon-yyyy using "<". Of course the range is off. If you want to compare these as strings, then use yyyy-mm-dd:
WHERE TO_CHAR(MOPACTIVITY.MOPEND, 'yyyy-mm-dd hh24:mi:') < TO_CHAR(TRUNC(CURRENT_DATE - 8/24,'hh24'), 'yyyy-mm-dd hh24:mi')
Wouldn't it be easier to compare these as dates?
where MOPACTIVITY < TRUNC(CURRENT_DATE - 8/24)
to_char returns strings, so you are comparing strings, not date. So they are compared alphabetically.

Oracle SQL : timestamps in where clause

I need to look up rows within a particular time frame.
select *
from TableA
where startdate >= '12-01-2012 21:24:00'
and startdate <= '12-01-2012 21:25:33'
I.e.: I need to look up rows with timestamp precision of SECONDS. How do I achieve this?
FYI: The startdate column is of type TIMESTAMP.
to_timestamp()
You need to use to_timestamp() to convert your string to a proper timestamp value:
to_timestamp('12-01-2012 21:24:00', 'dd-mm-yyyy hh24:mi:ss')
to_date()
If your column is of type DATE (which also supports seconds), you need to use to_date()
to_date('12-01-2012 21:24:00', 'dd-mm-yyyy hh24:mi:ss')
Example
To get this into a where condition use the following:
select *
from TableA
where startdate >= to_timestamp('12-01-2012 21:24:00', 'dd-mm-yyyy hh24:mi:ss')
and startdate <= to_timestamp('12-01-2012 21:25:33', 'dd-mm-yyyy hh24:mi:ss')
Note
You never need to use to_timestamp() on a column that is of type timestamp.
For everyone coming to this thread with fractional seconds in your timestamp use:
to_timestamp('2018-11-03 12:35:20.419000', 'YYYY-MM-DD HH24:MI:SS.FF')