I have a table with TIMESTAMP datatype and I need to compare month value to select all table values. Example the created is the field and wanted to get all the rows which is created between November & December of any year. Tried with below query and it don't work.
select * from table_name where TO_CHAR(created_time, 'mon') in ('nov','dec')
Use EXTRACT:
SELECT *
FROM table_name
WHERE EXTRACT( MONTH from created_time ) IN ( 11, 12 )
Or, you can use TO_CHAR( created_time, 'MM' ) to get the numeric month value (and not worry about language settings as you would have to with the MON format model):
SELECT *
FROM table_name
WHERE TO_CHAR( created_time, 'MM' ) IN ( '11', '12' )
db<>fiddle
Well, there are several ways to achieve this.
1.Using the nlsparam of to_char function
The 'nlsparam' argument specifies the language in which month and day names and abbreviations are returned.
Example
SQL> create table t ( c1 timestamp ) ;
Table created.
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss.rr' ;
Session altered.
SQL> insert into t values ( systimestamp - 30 ) ;
1 row created.
SQL> insert into t values ( systimestamp ) ;
1 row created.
SQL> select * from t ;
C1
---------------------------------------------------------------------------
30.07.2020 09:29:35.20
29.08.2020 09:29:42.20
SQL> select value from nls_database_parameters where parameter='NLS_LANGUAGE' ;
VALUE
--------------------------------------------------------------------------------
AMERICAN
SQL> select c1 , to_char(c1, 'mon' , 'NLS_DATE_LANGUAGE = american') as mon from t ;
C1
---------------------------------------------------------------------------
MON
------------
30.07.2020 09:29:35
jul
29.08.2020 09:29:42
aug
2.However, to avoid depending in the language, you can use extract and compare the number of the month which is the same in any language. In this case you need to convert the timestamp to a date, but before you need to set the nls_timestamp_format to the specific format.
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select c1 , extract(month from to_date(c1,'dd.mm.yyyy hh24:mi:ss')) from t ;
C1
---------------------------------------------------------------------------
EXTRACT(MONTHFROMTO_DATE(C1,'DD.MM.YYYYHH24:MI:SS'))
----------------------------------------------------
30.07.2020 09:29:35
7
29.08.2020 09:29:42
8
Related
Hi everyone I wanna ask u about how I can bring data last 24 hours into bar charts, is there any methods to make it please
I have this table without data
datetime
clientchannel
servicename
service_count
13_02_2022 9:35
*****
notification
2
It is a WHERE clause you need, I presume. Something like this:
select ...
from your_table
where datetime >= sysdate - 1;
Why? Because - when you subtract a number from DATE datatype value in Oracle - it subtracts that many days.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate right_now,
2 sysdate - 1 yesterday
3 from dual;
RIGHT_NOW YESTERDAY
------------------- -------------------
13.02.2022 11:01:34 12.02.2022 11:01:34
SQL>
If you store date values as strings (which means that DATETIME column is declared as e.g. VARCHAR2(20), and that's really bad idea), then you first have to convert it to a valid date datatype value - use TO_DATE function with appropriate format mask:
where to_date(datetime, 'dd_mm_yyyy hh24:mi') >= sysdate - 1
[EDIT] If you want to go 60 minutes back, then subtract that many minutes:
SQL> select sysdate right_now,
2 sysdate - interval '60' minute an_hour_ago
3 from dual;
RIGHT_NOW AN_HOUR_AGO
------------------- -------------------
14.02.2022 07:09:30 14.02.2022 06:09:30
SQL>
I am working on a set of tables for an assignment related to airports,
CREATE TABLE FLIGHT
(FlightCode VARCHAR2(6),
AirplaneSerialNum VARCHAR2(6) NOT NULL,
RemCapacity NUMBER(3),
FlightDate DATE NOT NULL,
RouteNum NUMBER(5) NOT NULL,
FlightDeparture DATE,
FlightArrival DATE,
CONSTRAINT FlightPK PRIMARY KEY (FlightCode),
CONSTRAINT RouteFK FOREIGN KEY (RouteNum) REFERENCES ROUTE (RouteNum));
Where FlightDeparture and FlightArrival are ideally referenced in the 24H format or 12H format (HHMM), how should I go about writing it, and how can I make a column in a separate table referencing the difference (to 2dp.) in number of hours between FlightArrival and FlightDeparture?
e.g. in another table that references FlightCode in FLIGHT as a FK
HoursInFlight NUMBER(10,2)
where HoursInFlight = FlightArrival - FlightDeparture
DATE datatype looks OK; it stores values in Oracle's internal format, so it is up to you how will you display them to users. For example:
Inserting:
SQL> create table flight (flight_date date);
Table created.
SQL> insert into flight (flight_date) values (sysdate);
1 row created.
SQL> insert into flight (flight_date) values (to_date('18.07.2021 16:58', 'dd.mm.yyyy hh24:Mi'));
1 row created.
Selecting (various options):
SQL> select * from flight;
FLIGHT_D
--------
18.07.21
18.07.21
SQL> select to_char(flight_date, 'dd-mon-yy hh:mi am', 'nls_date_language = english')) val from flight;
select to_char(flight_date, 'dd-mon-yy hh:mi am', 'nls_date_language = english')) val from flight
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected
SQL> select to_char(flight_date, 'dd-mon-yy hh:mi am', 'nls_date_language = english') val from flight;
VAL
---------------------------
18-jul-21 04:58 PM
18-jul-21 04:58 PM
SQL> select to_char(flight_date, 'dd/mm/yyyy hh24:mi:ss') val from flight;
VAL
-------------------
18/07/2021 16:58:36
18/07/2021 16:58:00
SQL> alter session set nls_Date_Format = 'yyyy-mm-dd hh:mi am';
Session altered.
SQL> select * from flight;
FLIGHT_DATE
-------------------
2021-07-18 04:58 PM
2021-07-18 04:58 PM
SQL>
The difference of two DATE values is number of days between them, so - if you want to "convert" them to hours, multiply it by 24 (as there are 24 hours in a day):
SQL> select to_date('18.07.2021 17:02', 'dd.mm.yyyy hh24:mi') - to_date('18.07.2021 10:44', 'dd.mm.yyyy hh24:mi') diff
2 from dual;
DIFF
----------
,2625
SQL> select 0.2625 * 24 hours from dual;
HOURS
----------
6,3
SQL>
How can I convert YYYYMM (stored as a number in oracle database) to MMM-YYYY
For e.g.
For 202001 - I want to convert into "Jan-2020".
Please help.
You can use TO_CHAR and TO_DATE as the following:
SQL> WITH YOUR_DATE ( DT ) AS ( -- your data
2 SELECT 202001 FROM DUAL
3 )
4 -- your query
5 SELECT TO_CHAR(TO_DATE(DT,'YYYYMM'), 'Mon-YYYY') AS RESULT FROM YOUR_DATE;
RESULT
--------
Jan-2020
SQL>
Cheers!!
I want to migrate a table which contains some columns with dates. The issue is my dates are often in dd/mm/yyyyy HH24:MM:YYYY format. But sometimes it appears that the format is only dd/mm/yyyy, or blank.
I guess that's why I'm getting ORA-01830 when I'm trying to migrate the datas.
I tried
CASE
WHEN TO_DATE(MYDATE,'DD/MM/YYYY')
then TO_DATE(MYDATE,'DD/MM/YYYY 00:00:00')
END AS MYDATE
But I'm not sure if it is possible to test the date format (and ofcourse it's not working).
Thank you
TO_DATE cannot test date format, but you can do it. If Lalit's answer would not be enough, try something like
select
case when my_date like '__/__/__' then to_date(my_date, 'dd/mm/yy')
when my_date like '__-__-__' then to_date(my_date, 'dd-mm-yy')
...
end
So you have the data type issue. DATE is stored as string literal. As you have mentioned that the date model has the DD/MM/YYYY part same, just that the time portion is either missing for some rows or the entire value is NULL.
For example, let's say your table have the values like -
SQL> WITH dates AS(
2 SELECT 1 num, '29/12/2014 16:38:57' dt FROM dual UNION ALL
3 SELECT 2, '29/12/2014' FROM dual UNION ALL
4 SELECT 3, NULL FROM dual
5 )
6 SELECT num, dt
7 FROM dates
8 /
NUM DT
---------- -------------------
1 29/12/2014 16:38:57
2 29/12/2014
3
SQL>
TO_DATE with proper format model should do the trick.
Let's stick to a format model first.
SQL> alter session set nls_date_format='dd/mm/yyyy hh24:mi:ss';
Session altered.
Now, let's use TO_DATE to explicitly convert the string literal to date.
SQL> WITH dates AS(
2 SELECT 1 num, '29/12/2014 16:38:57' dt FROM dual UNION ALL
3 SELECT 2, '29/12/2014' FROM dual UNION ALL
4 SELECT 3, NULL FROM dual
5 )
6 SELECT num, to_date(dt, 'dd/mm/yyyy hh24:mi:ss') dt
7 FROM dates
8 /
NUM DT
---------- -------------------
1 29/12/2014 16:38:57
2 29/12/2014 00:00:00
3
SQL>
in my select query i have the following
substr(to_date(NEXT_ARRIVAL_DATE, 'yyyy-mm-dd'), 0, 10)
Which yields:
08-JUN-11
What i need is it to yield:
2011-06-08
EDIT:
The data was coming in wrong. sorry. The below workds fine
to_char(to_date(NEXT_ARRIVAL_DATE, 'mm/dd/yyyy'), 'YYYY-MM-DD') || ' 00:000:00'
Your DATE_NEXT_ARRIVAL column obviously has a date datatype.
SQL> create table t23 (next_arrival_date date)
2 /
Table created.
SQL> insert into t23 values (sysdate+7)
2 /
1 row created.
SQL> select to_date(next_arrival_date, 'YYYY-MM-DD')
2 from t23
3 /
TO_DATE(N
---------
11-JUN-08
SQL>
If you want to display the date in a different format you need to use TO_CHAR() i.e. convert it to a string:
SQL> select to_char(next_arrival_date, 'YYYY-MM-DD')
2 from t23
3 /
TO_CHAR(NE
----------
2011-06-08
SQL>
If you have to do this for a whole bunch of dates, you might want to change the session settings instead....
SQL> alter session set nls_date_format='YYYY-MM-DD'
2 /
Session altered.
SQL> select sysdate, next_arrival_date
2 from t23
3 /
SYSDATE NEXT_ARRIV
---------- ----------
2011-06-01 2011-06-08
SQL>
in Oracle you can convert a DATE column to string with
to_char(NEXT_ARRIVAL_DATE, 'yyyy-mm-dd')
but it looks like the value of NEXT_ARRIVAL_DATE is a string in the required format.
so you can just do select NEXT_ARRIVAL_DATE from ...