Convert 'DD-MM-YY' to 'Month Year' oracle - sql

I have a date a field that displays date in the format '19-JAN-20' I would like to display it as 'January 2019' using oracle database.
select '19-JAN-19' FROM DUAL

You need to use TO_CHAR to display in the required format:
select to_char(DATE '2019-01-20', 'Month YYYY') dt from dual;
DT
-------------
January 2019
select '19-JAN-19' FROM DUAL
There are couple of issues with that statement:
'19-JAN-19' is a string, not a date. Always use TO_DATE to explicitly convert it to date, or stick to ANSI date literal like I used in my demo.
Do not use two-digit YY representation for year, that's the reason Y2K bug was introduced. Always use YYYY.

Looks like a nested TO_CHAR, if "date" value you mentioned - 19-JAN-20 is stored as a string:
SQL> with test (col) as
2 (select '19-JAN-20' from dual)
3 select
4 to_char(
5 to_date(col, 'dd-mon-yy', 'nls_date_language = english'),
6 'fmMonth yyyy', 'nls_date_language = english'
7 ) result
8 from test;
RESULT
--------------
January 2020
SQL>
If your database speaks English, you can omit the NLS_DATE_LANGUAGE part (mine speaks Croatian so I included it).
However, if it is a date you'd just want to display differently, then:
(Just to avoid NLS_DATE_LANGUAGE in TO_DATE):
SQL> alter session set nls_date_language = 'english';
Session altered.
Default format in my database:
SQL> select sysdate from dual;
SYSDATE
--------
08.06.20
Alter session to desired format:
SQL> alter session set nls_date_format = 'fmMonth yyyy';
Session altered.
SQL> select sysdate from dual;
SYSDATE
--------------
June 2020
Or, apply TO_DATE function with desired format mask:
SQL> select to_char(sysdate, 'fmMonth yyyy') result from dual;
RESULT
--------------
June 2020
SQL>

Related

Data in Oracle apex

If I want to take data for today from 00:00 until currently hour how can I do it ???
I have this table
datetime
hourly
clientchannel
servicename
service_count
13_02_2022
9
*****
notification
2
Presuming that datetime column's datatype is DATE (should be), then
select *
from your_table
where datetime between trunc(sysdate) and trunc(sysdate, 'hh24')
because
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate as right_now,
2 trunc(sysdate) as midnight,
3 trunc(sysdate, 'hh24') this_hour
4 from dual;
RIGHT_NOW MIDNIGHT THIS_HOUR
------------------- ------------------- -------------------
01.03.2022 08:01:20 01.03.2022 00:00:00 01.03.2022 08:00:00
SQL>
If datetime's datatype is VARCHAR2 (bad choice), then you should first convert it to date, applying correct format model and hoping that there's no garbage in that column:
where to_date(datetime, 'dd_mm_yyyy') between ...

Oracle - Extract last month name from sysdate

If the date in my record is '04-NOV-2021', for ex, I want to show 'October'.
select extract(MONTH FROM add_months(sysdate,-1)) from dual;
That code will give me the '10' for October, but how do I format it as 'October'?
One option is to use to_char with the right nls_language.
SQL> select to_char(sysdate,'Month', 'nls_language=''english''' ) from dual ;
TO_CHAR(SYSDATE,'MONTH','NLS_LANGUAG
------------------------------------
November
SQL>
In your case
SQL> select to_char(to_date(to_char(add_months(sysdate,-1))),'Month', 'nls_language=''english''' ) from dual ;
TO_CHAR(TO_DATE(TO_CHAR(ADD_MONTHS(S
------------------------------------
October
Or
SQL> select to_char(add_months(sysdate,-1), 'fmMonth', 'nls_language="english"') from dual ;
TO_CHAR(ADD_MONTHS(SYSDATE,-1),'FMMO
------------------------------------
October
SQL>

spring data oracle - non-numeric character was found

I have the table disponibility with this columns.
I created the below query
#Query(nativeQuery = true, value = "select count(*) from disponibility d WHERE d.fk_room = ?1 "
+ "AND ((d.starthour < ?2 AND ?2 < d.endhour) OR (d.starthour < ?3 AND ?3 < d.endhour)) "
+ "AND ((d.startdate < TO_DATE(?4, 'dd/MM/yyyy') AND TO_DATE(?4, 'dd/MM/yyyy') < d.enddate) OR (d.startdate < TO_DATE(?5, 'dd/MM/yyyy') AND TO_DATE(?5, 'dd/MM/yyyy') < d.enddate))")
int checkUnicityRoom(String roomId, int startHour, int endHour, Date startDate, Date endDate);
return this error
ORA-01858: a non-numeric character was found where a numeric was expected
You entered a date value which isn't really a date. See the demonstration:
This is OK:
SQL> select to_date('15/02/2021', 'dd/mm/yyyy') today from dual;
TODAY
----------------
15.02.2021 00:00
But this isn't (note ab in the place of the month):
SQL> select to_date('15/ab/2021', 'dd/mm/yyyy') today from dual;
select to_date('15/ab/2021', 'dd/mm/yyyy') today from dual
*
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
SQL>
I don't know which values you used, but - check what you're passing to the TO_DATE function.
As you commented that value you use is Wed Apr 11 01:00:00 CET 2018, it is obviously different from dd/MM/yyyy format mask you used along with the TO_DATE function. These two must match.
In your case:
SQL> select to_timestamp_tz('Wed Apr 11 01:00:00 CET 2018', 'Dy Mon dd hh24:mi:ss TZR yyyy') datum
2 from dual;
DATUM
---------------------------------------------------------------------------
11.04.18 01:00:00,000000000 CET
SQL>
As table column's datatype is DATE you should
SQL> select cast(to_timestamp_tz('Wed Apr 11 01:00:00 CET 2018', 'Dy Mon dd hh24:mi:ss TZR yyyy') as date) datum
2 from dual;
DATUM
----------------
11.04.2018 01:00
SQL>

Formatting a date in Oracle to show month and day

I have the date as below
SELECT TO_CHAR(SYSDATE, 'DDD') FROM dual;
result for above query is : 117
I need to convert this 117 to month and day. anyone knows how to perform this in PL SQL
the expected result is 04-27
Change 'DDD' to 'MM-DD'.
Try this:
SELECT TO_CHAR(SYSDATE, 'MM-DD')
FROM dual;
OUTPUT:
04-27
Demo:
http://sqlfiddle.com/#!4/622055/7
EDITED:
DDD shows Number of Days from the first of a year.
For Example:
SELECT TO_CHAR(TO_DATE('2018-01-01','YYYY-MM-DD'), 'DDD')
FROM dual;
gives output 001 since it is the First Day of year 2018.
So to break 117 you need the year.
Then you can use this query.
SELECT TO_CHAR(TO_DATE('2018-01-01','YYYY-MM-DD') + column_name - 1, 'MM-DD')
FROM dual;
Example Query:
SELECT TO_CHAR(TO_DATE('2018-01-01','YYYY-MM-DD') + 117 - 1, 'MM-DD')
FROM dual;
OUTPUT:
04-27
Demo:
http://sqlfiddle.com/#!4/622055/37
If you have "117" and want to convert it to a date, you'll have to use a little bit (but really - just a little bit) of arithmetics. As 'DDD' represents number of days since the 1st of current year, add it to ... well the 1st of current year and apply appropriate format mask to convert it to date. I subtracted "1" as you want to get yesterday.
For example:
SQL> select to_char(sysdate, 'ddd') from dual;
TO_
---
117
SQL> select to_date(trunc(sysdate, 'yyyy') + 117, 'dd.mm.rrrr') - 1 resul
2 from dual;
RESULT
----------
27.04.2018
SQL>
Or, using the format you specified (MM-DD):
SQL> select to_char(trunc(sysdate, 'yyyy') + 117 - 1, 'mm-dd') result
2 from dual;
RESUL
-----
04-27
SQL>

How to use round and trunc to get dd:mm:yy:hh

I am using oracle 11g and I have normal timestamps (starttime) which produce an output as follows:
23.09.14 05:15:00,000000000
Now I want an output like
23.09.14 05
Also ok would be:
23.09.14 05:00:00,000000000
but when I use something like
round(starttime, 'HH') or trunc(starttime ,'HH24') I always get
23.09.14
with no hours at all.
Looking around here at stackoverflow I found
substr(TO_CHAR(starttime),0,LENGTH(TO_CHAR(starttime))-13)
which produces the correct output as char but when I want to sort dates it wont work because it sorts alphabetically. (so for example, 1.3., 1.4, 1.5.... instead of 1.3., 2.3., 3.3,...),
Any idea how I can get a timestamp which is rounded to the full hour?
I will have to use the statement in a group by clause. The complete statement would look like:
select round(starttime, 'HH24'), sum(counter) from wmsconsolidationorderdwct group by round(starttime, 'HH24') order by round(starttime, 'HH24') desc;
So I cannot display the rounded time and sort by the full timestamp since this would violate the group by clause.
This will truncate to the hour:
trunc(SYSTIMESTAMP) + extract(hour from SYSTIMESTAMP)/24
Edit:
I just tried it and
SELECT TRUNC(SYSTIMESTAMP ,'HH24') FROM DUAL;
returns the correct result.
Fiddle
If your purpose is to display, then use TO_CHAR with desired format model.
For example,
SQL> SELECT TO_CHAR(SYSTIMESTAMP, 'DD.MM.YY HH24') FROM dual;
TO_CHAR(SYS
-----------
28.05.15 15
SQL>
If your purpose is to do date arithmetic then you need to leave the data type as date.
For example,
SQL> alter session set nls_date_format='DD-MM-YYYY HH24:MI:SS'
2 /
Session altered.
SQL> SELECT TRUNC(SYSTIMESTAMP ,'HH24') FROM DUAL
2 /
TRUNC(SYSTIMESTAMP,
-------------------
28-05-2015 15:00:00
SQL>
If you have a timestamp object:
select my_number from(
SELECT TO_NUMBER(TO_CHAR(TO_DATE (TO_CHAR (SYSTIMESTAMP, 'DD.MM.YY HH24:MI'),
'DD.MM.YY HH24:MI'
),'HH24')) AS my_number
FROM DUAL)
order by 1
This could be simplified to:
select my_number from(
SELECT TO_NUMBER(TO_CHAR(SYSTIMESTAMP, 'HH24')) AS my_number
FROM DUAL)
order by 1