Get hour data from date column in oracle - sql

How can I select data between two date with hours. My oracle column date's format is:10.04.2006 19:10:37 I can select between twou days with:
select date from table where date between '1/1/2011' and '2/1/2011
but I will select between hours which hours are in date column i.e:
select date from table where date between '1/1/2011 22:00'and '2/1/2011 21:00' what can I do

You can use to_date to specify a custom date format:
where date between to_date('1/1/2011 22:00', 'DD/MM/yyyy HH24:MI')
and to_date('2/1/2011 21:00', 'DD/MM/yyyy HH24:MI')

This is the way I usually do.
where date between date'2011-1-1'+22/24 and date'2011-1-2'+21/24
On the column
TRUNC(date_col)+hours/24

Related

I need help understanding why am I am not getting any results with below query

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'

Error in timestamp datatype

When I run the below query,
select timestamp '01-01-2017 00:00' log_time from dual
I get this error
ORA-01847: day of month must be between 1 and last day of month.
If it is not valid, How to declare timestamp datatype in oracle?
If you are using ANSI timestamp literals, the value must be formatted in the ISO style yyyy-mm-dd hh24:mi:ss:
select timestamp '2017-01-01 00:00:00' log_time
from dual;
Alternatively use Oracle's to_timestamp() function if you want to keep that format:
select to_timestamp('01-01-2017 00:00', 'dd-mm-yyyy hh24:mi') log_time
from dual

SQL: Convert Date to Null when Date is certian range from Today

I am working with an Oracle Datebase and I want to convert a date to null when it is more than a certain number of months from today.
The date format I am using is dd-mm-yy
So lets say you have this table:
WITH TEST_TABLE as (select '1' as num, '10-FEB-16' from dual
union(select '2', '10-FEB-30' from dual)
union(select '3', '10-FEB-50' from dual)
union(select '4', '10-FEB-17' from dual))
I know I can use: SELECT SYSDATE FROM DUAL; to get todays date but I am not sure how to compare this by months.
How would I change the dates to null when they are greater than say 24 months from today?
Something like this:
update test_table
set dte = (case when add_months(dte, -12) < sysdate then dte end);
Note: You should be storing a date as a date data type in the database. The output format (by default) is the DD-MMM-YY format. But that is just the date formatted for output.

Oracle query time on date fields

I have a column of type DATE stored data are contains date and time . I can see value when i do
select CAST(MSG_DT AS TIMESTAMP) from table;
this is the output
17-MAR-08 15:38:59,000000000
I have to select the row using
Only date
select CAST(MSG_DT AS TIMESTAMP) from
MWRB_RECEIVE where
MSG_DT >= TO_DATE( '2000-02-03' ,'YYYY-MM-DD')
and
MSG_DT <= TO_DATE( '2010-02-03' ,'YYYY-MM-DD')
Only time (eg: every message between 12:00:11 and 23:02:55)
In DB2 i can do
SELECT *
FROM TABLE
WHERE DATE(INS_TMS) = '2014-02-18'
SELECT *
FROM TABLE
WHERE TIME(INS_TMS) > '09.55.00'
In ORACLE I can't see the equivalent.
Try this:
SELECT *
FROM your_table
WHERE TO_CHAR (start_date, 'yyyy-mm-dd') = '2014-10-06'
AND TO_CHAR (start_date, 'hh24:mi:ss') > '10:00:00'
Why are you casting the column value to a TIMESTAMP when the column in the database is a DATE type? The fractional part of the seconds will always be 0, as DATE only has resolution to the seconds value. You need to add the hours,minutes, and seconds format specifier to the query:
select MSG_DT from
MWRB_RECEIVE
where MSG_DT between TO_DATE( '2000-02-03 12:00:11' ,'YYYY-MM-DD HH24:MI:SS') AND
TO_DATE( '2010-02-03 23:02:55' ,'YYYY-MM-DD HH24:MI:SS')
There is no need to split date and hour, you can have it in a single where clause
where field > to_date('20121212 12:12:12, 'YYYYMMDD HH24:MI:SS')
Check for your reference oracle to_date() as it seems the only thing you need

Oracle Timestamp Conversion with Dates

Assuming this has a simple solution, but I can't find it.
I'm trying to do some logic on a DATE field in Oracle. My desire is to take a DATE field and subtract X hours from it.
For instance: SELECT A.MyDATE - 100 Hours from dual;
however, I need a result in a timestamp format 'YYYY-MM-DD hh:mm'.
I've tried CAST(A.MyDATE as TIMESTAMP) - NUMTODSINTERVAL(100/24,'day') however it didn't work.
I found out that the issue is that the MyDATE field when cast to a timestamp still contained some residual time elements. How can I reset these??
Thanks!
You can just do this with subtraction:
select a.MyDate - 100.0/24
To convert to varchar:
select to_char(a.MyDate - 100.0/24, 'YYYY-MM-DD')
And, if you want to get rid of that pesky time on the date:
select trunc(a.MyDate - 100.0/24) as JustTheDate
The formats and dates in my example can be changed to any other formats and dates:
SELECT To_Timestamp(To_Char(Sysdate - INTERVAL '100' HOUR, 'MM/DD/YYYY HH24:MI'), 'MM/DD/YYYY HH24:MI')
FROM dual
/
Output:
2/4/2013 10:18:00.000000000 AM
To remove time element add Trunc() to any of your dates...:
SELECT Trunc(To_Timestamp(To_Char(Sysdate - INTERVAL '100' HOUR, 'MM/DD/YYYY HH24:MI'), 'MM/DD/YYYY HH24:MI'))
FROM dual
/
Output: 2/4/2013
Conversion/Casting - when using other dates in place of sysdate then add formats as in my other examples:
SELECT CAST(SYSDATE AS TIMESTAMP) - INTERVAL '100' HOUR FROM dual
/
Output: 2/4/2013 10:26:35.000000000 AM
SELECT start_date tstamp_to_date, CAST(start_date AS timestamp) date_to_tstamp FROM
(
SELECT to_date(to_char(to_timestamp ('2013-02-07 10:07:47.000' , 'YYYY-MM-DD HH24:MI:SS.FF'),'DD-MON-YYYY HH24:MI:SS'), 'DD-MON-YYYY HH24:MI:SS') start_date
FROM dual
)
/
Output:
tstamp_to_date date_to_tstamp
-------------------------------------------------------
2/7/2013 10:07:47 AM 2/7/2013 10:07:47.000000 AM
In Oracle, a DATE always has a day and a time component. Depending on the tool you are using and your session's NLS_DATE_FORMAT, it is entirely possible that the tool may not display the time component when you look at the data. But that is simply a display question, it has no impact on the actual data.
If you want to subtract 100 hours from midnight on the day that MyDate represents
SELECT TRUNC(MyDate) - interval '100' hour
FROM dual
This will return a DATE. If you want to return a string in a particular format
SELECT TO_CHAR( TRUNC(MyDate) - interval '100' hour, 'YYYY-MM-DD hh:mi am' )
FROM dual
Note that I'm assuming that there was a typo in your question. I assume that you want to display the minutes after the hour (mi) rather than the month (mm).
I am trying to fetch the records which is older than 30 days (from Mod_date) and I am using the below query and it is returning all the data and I want only 30 days old data.
Sample :- Mod_date 03-NOV-12 12.00.00.000000000 AM
Query :-
select Mod_date from fil_cnfact where Mod_date <= sysdate -30 order by Mod_date asc ;