hour must be between 0 and 23 -oracle - oracle

I just want to concatenate sysdate with string. i'm passing sysdate in in_date variable. After executing the below query,
`select to_date('''||in_date||''' || '14:59:59', 'dd-mon-yyyy hh24:mi:ss') from dual;
`
i'm getting error ORA-1850:hour must be between 0 and 23
i'm using oracle 12c.
Thanks

Based on your own answer I assume the proper solution would be one of these:
SELECT TRUNC(SYSDATE) + INTERVAL '14:59:59' HOUR TO SECOND FROM dual;
SELECT TRUNC(SYSDATE) + 14/24 + 59/24/60 + 59/24/60/60 FROM dual;
SYSDATE is a DATE value, you should never call TO_DATE() on a value which is already a DATE. Implicitly Oracle is doing this:
SELECT
TO_DATE(''||
TO_CHAR(SYSDATE, SYS_CONTEXT('USERENV', 'NLS_DATE_FORMAT'))
||'' || ' 14:59:59', 'dd-mon-yy hh24:mi:ss')
FROM dual;
So, the result depends on current user session NLS_DATE_FORMAT (and in your case also on NLS_DATE_LANGUAGE) which may change at any time.

I've missed a space before time and also changes format 'yyyy' to 'yy'
select to_date(''||sysdate||'' || ' 14:59:59', 'dd-mon-yy hh24:mi:ss') from dual;
27-04-2020 14:59:59
Thanks..

Related

How to convert to_char ('DD/MM/YYYY HH24:MI') and to_date ('DD/MM/YYYY HH24:MI') using oracle or Plsql

select to_date((q.confirm_DATE || ' ' || q.confirm_time),'dd/mm/yyyy hh24:mi')
from qc_warning_record q
where q.warning_id = 125 ;
ineed show time
enter image description here
As per the info you provided it seems you are doing something similar.
Please find below a use case.
Create table qc_warning_record(id number,confirm_DATE DATE,confirm_time varchar2(10));
-----------------------------------
INSERT INTO qc_warning_record values(125,sysdate-3, '100012');
-------------------------------
select (to_char(q.confirm_DATE,'DD-MON-YYYY') || ' '|| to_char(to_date(q.confirm_time, 'hh24miss'),'hh24:mi:ss')) DATE_TIME
from qc_warning_record q where q.id = 125 ;
Assuming q.confirm_date is a date and q.confirm_time is a string (in format HH24:MI), and you need to create a value of date (date-time) data type, combining the date (truncated to midnight) from the first column with the time from the second, you could do this:
to_date( to_char(q.confirm_date, 'dd/mm/yyyy') || ' ' || q.confirm_time
, 'dd/mm/yyyy hh24:mi'
)
You won't be able to see time-of-day if you query dates and your NLS_DATE_FORMAT is set to dd-MON-rr. To change it, you must first run
alter session set nls_date_format='dd/mm/yyyy hh24:mi';

Oracle query not giving result for current_date

What is the query in Oracle to fetch the data for current_date
the column end_date is like the following
end_date
27-10-16 03:35:00.000000000 PM
23-11-16 11:15:00.000000000 AM
02-11-16 03:00:00.000000000 PM
08-11-16 09:00:00.000000000 AM
Like I am running the following query as
Select * from table1
where end_date < TO_DATE('2017-04-11 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
it is running successfully, but when i replace the query with the current date ... it is not giving the results
Select * from table1
where end_date < TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS')
could someone tell me what is the cause the second query is not giving results.
CURRENT_DATE returns date. There is no need to use TO_DATE. The below query should be enough.
Select * from table1
where end_date < current_date;
If you run the below query you'll understand what went wrong for you. Year becomes 0011.
SELECT TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
Please note that CURRENT_DATE returns the current date in the session time zone. SYSDATE returns the current date and time set for the operating system on which the database resides. This means that CURRENT_DATE and SYSDATE can return different results. You can have a look at this
The query worked like this :
Select * from table1
where trunc(end_date) < trunc(sysdate)
Trunc is used to compare the both dates and it fetch the results.
CURRENT_DATE is already a DATE value. You can format the output using to_char if you want.
end_date < CURRENT_DATE should do the job. Or you can set the nls parameter accordingly for a better readability.
If you are comparing only date, without timestamp, you can go with trunc()

Alter a datetime in Oracle and set time to 9 am

i have 28-APR-2016 10:05:07 date as parameter in stored procedure. This may be the current time also as string date.
i need to set the time to 9 am to check the shift start timing.
SELECT TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS') FROM dual;
I am new to oracle. Help is appreciated.
If you want the date with 9:00 a.m., then you can do:
SELECT TRUNC(TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS')) + 9/24.0
FROM dual;
You can also use:
SELECT TRUNC(TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS')) + INTERVAL '9' HOUR
FROM dual;
I'm just old-fashioned so I tend to use the first method.

concatenating sysdate with fixed time in oracle

I want to concatenate sysdate with my own time. Below is the query i have tried, but I am getting year as 0016 instead of 2016.
Are there any other ways to get the result like below?
Query:
select to_date(sysdate || ' 02:50:00','dd-mon-yyyy hh24:mi:ss') as MyTime from dual
Output:
MYTIME
3/12/0016 02:50:00 AM
One way
Convert SYSDATE to a string
Append your fixed time element
Convert back to a date
Put it altogether like this:
to_date(to_char(sysdate,'YYYY-MM-DD')||' 02:50:00','YYYY-MM-DD HH24:MI:SS' )
Alternative way: use arithmetic.
Strip the real time from SYSDATE
Add the number of seconds ((60*60*2)+(50*60)/(60*60*24)
Include your workings or not:
trunc(sysdate) + ( 10200 / 86400)
There is a third way: use an INTERVAL (basically a variant of the second way).
SQL> select trunc(sysdate) + to_dsinterval('0 02:50:00') as mytime
2 from dual
3 /
MYTIME
------
2016-03-12 02:50:00
SQL>
Try this
SELECT TO_DATE(TO_CHAR(SYSDATE, 'DD-MON-YYYY') || ' 08:00:00', 'DD-MON-YYYY HH:MI:SS') FROM DUAL;
Just change SYSDATE to TO_CHAR(SYSDATE, 'DD-MON-YYYY')
I did this:
SELECT TO_CHAR(
TO_DATE(TO_CHAR(SYSDATE, 'DD-MON-YYYY') || ' 08:00:00', 'DD-MON-YYYY HH:MI:SS')
,'DD-MON-YYYY HH:MI:SS') from dual;
and got
12-MAR-2016 08:00:00
as a result.

Oracle date format problem

I have the following strange problem in Oracle
(Please keep in mind that I have little experience in SQL and even less in Oracle).
If I do this:
SELECT TO_CHAR(sysdate, 'YYYY-MM-DD HH24:MI') FROM dual
I get this:
2010-12-02 18:39
All fine there.
However, if I do this:
UPDATE favorite_item
SET favorite_item.last_used_date = TO_DATE(sysdate, 'YYYY-MM-DD HH24:MI')
WHERE favorite_item.favorite_item_id = 1
I get this in my database:
10-DEC-02
Which is the 10th of December '02 which is not correct
If I do this to confirm:
SELECT TO_CHAR(favorite_item.last_used_date, 'YYYY-MM-DD HH24:MI') AS last_used_date
FROM favorite_item
WHERE favorite_item.favorite_item_id = 1
I get this:
0002-12-10 00:00
Which is completely wrong.
What am I doing wrong? I feel that the date setting is not working correctly.
Thanks in advance for your help.
Don't use TO_DATE() on sysdate; sysdate is already a date.
UPDATE favorite_item
SET favorite_item.last_used_date = sysdate
WHERE favorite_item.favorite_item_id = 1`
The problem is using the to_date() function on anything other than a string.
As to why you are getting the wrong results, there is an internal conversion that happens when you use to_date on a date. Since to_date actually takes input as a string, your date is initially converted into a string (according to your NLS_DATE_FORMAT setting) and then converted back to a date. Hence the mismatch.
SQL> select sysdate from dual;
SYSDATE
---------
02-DEC-10
SQL> select to_date(sysdate,'YYYY-MM-DD') from dual;
TO_DATE(S
---------
10-DEC-02
--- This is because, the above string is actually executed as
SQL> select to_date(
to_char('02-DEC-10','YYYY-MM-DD') from dual;
TO_DATE('
---------
10-DEC-02
SQL> select to_date(
2 /* implicit conversion... dd-mon-yy' is my session's NLS_DATE_FORMAT */
3 to_char(sysdate,'dd-mon-yy'),
4 'YYYY-MM-DD')
5 from dual;
TO_DATE(/
---------
10-DEC-02
sysdate returns a date, so converting it to a date using to_date(sysdate, ...) is redundant/not necessary. You're getting that odd result because the date is being cast to a string by the to_date function using the Oracle default of "DD-MON-YY" and then back into a date using your supplied format, "YYYY-MM-DD". Since the formats don't match, Oracle is interpreting the year as the day and the day as the year. This works correctly (but, again, is redundant):
select to_date(sysdate, 'DD-MON-YY') from dual;

Resources