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>
Related
I have the following end of code on SQL (Oracle 19c)
WHERE DATE >= '20220101' AND DATE BETWEEN add_months(trunc(sysdate,'mm'),-1) AND last_day (add_months(trunc(sysdate,'mm'),-1)) AND DATE != 'None' AND QT_MOCK_DATA IS NOT NULL ORDER BY DATE DESC'
ERROR:
ORA-01861: literal does not match format string
Can someone help?
Thanks!
Column name certainly isn't date; it is reserved word, reserved for the datatype name. So, no - you don't have that code.
Next: it seems you are storing strings into that column (as you're comparing it to 'None', which is a string). That's a bad idea - dates should be stored as dates. Anything else brings problems (you hit one).
Then, you're comparing that string to date values. How come? The between part of the code returns date datatype values:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> SELECT ADD_MONTHS (TRUNC (SYSDATE, 'mm'), -1) val1,
2 LAST_DAY (ADD_MONTHS (TRUNC (SYSDATE, 'mm'), -1)) val2
3 FROM DUAL;
VAL1 VAL2
------------------- -------------------
01.06.2022 00:00:00 30.06.2022 00:00:00
SQL>
Maybe you wanted to apply the TO_CHAR function with appropriate format model?
SQL> SELECT TO_CHAR (ADD_MONTHS (TRUNC (SYSDATE, 'mm'), -1), 'yyyymmdd') val1,
2 TO_CHAR (LAST_DAY (ADD_MONTHS (TRUNC (SYSDATE, 'mm'), -1)),
3 'yyyymmdd') val2
4 FROM DUAL;
VAL1 VAL2
-------- --------
20220601 20220630
SQL>
Because, with it, query returns something.
Compare
your code
SQL> WITH test (date1, qt_mock_data) AS (SELECT '20220615', 'A' FROM DUAL)
2 SELECT *
3 FROM test
4 WHERE date1 >= '20220101'
5 AND date1 BETWEEN ADD_MONTHS (TRUNC (SYSDATE, 'mm'), -1)
6 AND LAST_DAY (ADD_MONTHS (TRUNC (SYSDATE, 'mm'), -1))
7 AND date1 != 'None'
8 AND qt_mock_data IS NOT NULL
9 ORDER BY date1 DESC;
WHERE date1 >= '20220101'
*
ERROR at line 4:
ORA-01861: literal does not match format string
to my code
SQL> WITH test (date1, qt_mock_data) AS (SELECT '20220615', 'A' FROM DUAL)
2 SELECT *
3 FROM test
4 WHERE date1 >= '20220101'
5 AND date1 BETWEEN TO_CHAR (ADD_MONTHS (TRUNC (SYSDATE, 'mm'), -1),
6 'yyyymmdd')
7 AND TO_CHAR (
8 LAST_DAY (ADD_MONTHS (TRUNC (SYSDATE, 'mm'), -1)),
9 'yyyymmdd')
10 AND date1 != 'None'
11 AND qt_mock_data IS NOT NULL
12 ORDER BY date1 DESC;
DATE1 Q
-------- -
20220615 A
SQL>
I don't know your DB's schema. Maybe try using date literal?
WHERE
DATE >= date '20220101'
AND DATE BETWEEN add_months(
trunc(sysdate, 'mm'),
-1
)
AND last_day (
add_months(
trunc(sysdate, 'mm'),
-1
)
)
AND DATE != 'None'
AND QT_MOCK_DATA IS NOT NULL
ORDER BY
DATE DESC
Do not store dates as string, use a DATE data type (and do not use reserved words such as DATE as an identifier).
However, since you are storing the values as dates then convert the value you are comparing to to a string using TO_CHAR:
WHERE "DATE" >= '20220101'
AND "DATE" >= TO_CHAR(add_months(trunc(sysdate,'mm'),-1), 'YYYYMMDD')
AND "DATE" < TO_CHAR(trunc(sysdate,'mm'), 'YYYYMMDD')
AND "DATE" != 'None'
AND QT_MOCK_DATA IS NOT NULL
ORDER BY "DATE" DESC
which can be simplified to:
WHERE "DATE" >= TO_CHAR(add_months(trunc(sysdate,'mm'),-1), 'YYYYMMDD')
AND "DATE" < TO_CHAR(trunc(sysdate,'mm'), 'YYYYMMDD')
AND QT_MOCK_DATA IS NOT NULL
ORDER BY "DATE" DESC
(Since the start of the range will, assuming SYSDATE is not set to a past dates, always be greater than 20220101 and if the date range is valid then != 'None' will always also be true.)
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 ...
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>
I have a report to be generated every hour from a sql query.
The request here is to generate data from yesterday's night 8.00 pm till
today's 12 pm noon . I am using the below query but not getting correct data:
select * from roster_report where job_start > TRUNC(SYSDATE-1) + 20/24 ;
Suppose today's date is 29th April, the report generated on 29th april 11:15 pm is showing data of 29th april 8:00 pm to 11:15 pm correctly.
the data gets incorrect after 00:00 am where it fetches data of 29th april itself and not 30th april.
The query will be used after every hour to generate report.
I am running this in oracle sql developer.
For previous day 8 PM
SELECT to_char(trunc(SYSDATE -1) + 20/24,'mm/dd/yyyy hh24:mi:ss AM') FROM dual;
For Today's 12 PM
SELECT to_char(trunc(SYSDATE) + 12/24,'mm/dd/yyyy hh24:mi:ss AM') FROM dual;
Sample Query
WITH DATA AS
( SELECT 'text' col, SYSDATE create_date FROM dual
)
select *
from tw_logtable
where tw_logtable.created_datetime
BETWEEN (TRUNC(sysdate -1) + 20/24) AND (TRUNC(sysdate) + 12/24)
Try
WHERE
job_start >= ( TRUNC(SYSDATE-1) + INTERVAL '20' HOUR )
AND job_start <= ( TRUNC(SYSDATE) + INTERVAL '12' HOUR )
You could just use > or < if that suits your requirement. BETWEEN can also be used for simplicity if all you want is inclusive range.
Here you have two ways:
SELECT *
FROM ROSTER_REPORT
WHERE JOB_START BETWEEN (TRUNC(SYSDATE - 1) + 20/24)
AND TRUNC(SYSDATE) + 12/24 ;
or
SELECT *
FROM ROSTER_REPORT
WHERE JOB_START BETWEEN TO_DATE(TO_CHAR(SYSDATE-1,'DD/MM/RRRR')||' 20:00','DD/MM/RRRR HH24:MI')
AND TO_DATE(TO_CHAR(SYSDATE ,'DD/MM/RRRR')||' 12:00','DD/MM/RRRR HH24:MI');
I have a query that accepts the date in the following format:
'31 AUG 2012'
I need the query to return the month as a number. For the above date, the returned value would be 08
I have tried the following:
EXTRACT(MONTH FROM DATE '31 AUG 2012')
TO_DATE('31 AUG 2012', 'MM')
TO_CHAR('31 AUG 2012', 'MM')
All of which give me the below errors respectively:
ORA-01861: literal does not match format string
ORA-01843: not a valid month
ORA-01722: invalid number
How can this be accomplished? Thanks
SELECT EXTRACT(MONTH FROM date '2012-08-31') FROM dual;
EXTRACT(MONTH FROM to_date('31 AUG 2012','DD MON YYYY'))
The date '' operator only accepts ISO format ...
date '2012-08-31'
Actually what you made mistake is you given only 'MM' as the format_mask instead you give 'DD/MM/YYYY', hope that would work as well
SELECT TO_CHAR(SYSDATE,'DD/MM/YYYY HH24:MI:SS') FROM AB;
Output: 26/03/2013 15:15:34
If you need only the month to be displayed try using this:
SELECT EXTRACT(MONTH FROM SYSDATE) FROM AB;
the above will display the MAR as 3(considering today's date)
You have to convert 08 to 8 because 08 is character field so i have used to_number() over to_char so that it will return exact number.
select to_number(to_char(to_date('31-AUG-2012','DD-MON-YYYY'),'MM')) from your_tbl;
select substr(to_date(sysdate,'dd-mon-yyyy'),4,3) from dual;