concatenating sysdate with fixed time in oracle - 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.

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';

How to calculate the difference of HH:MM:SS between two dates in oracle sql?

I have a table abc as:
-- start_time |end_time | total_time_taken
-- 27.05.2020 00:52:48 |27.05.2020 02:08:33 |
I want to set the value of total_time_taken as the difference of end_time-start_time. in the format "HH:MM:SS".I searched the similar topic but didnot find the exact answer.
My expected output is like : 01:44:12 (HH:MM:SS)
So,i tried :
SELECT To_Char(end_time,'HH24:MM:SS'),To_Char(start_time,'HH24:MM:SS'),
To_Char(end_time,'HH24:MM:SS')-To_Char(start_time,'HH24:MM:SS') FROM abc;
The datatypes of start_time,end_time,total_time_taken is DATE.Please help me to find the solution.
If you cast those dates as timestamps, you can easily subtract them and see relatively nice result:
SQL> with test (st, et) as
2 (select to_date('27.05.2020 00:52:48', 'dd.mm.yyyy hh24:mi:ss'),
3 to_date('27.05.2020 02:08:33', 'dd.mm.yyyy hh24:mi:ss')
4 from dual
5 )
6 select cast(et as timestamp) - cast(st as timestamp) diff
7 from test;
DIFF
--------------------------------------------------------------------------
+000000000 01:15:45.000000
SQL>
If you want to format it as you wanted (note that mm format mask is for months; mi is for minutes), then you could do some extracting - again from timestamp (won't work for date):
SQL> with test (st, et) as
2 (select to_date('27.05.2020 00:52:48', 'dd.mm.yyyy hh24:mi:ss'),
3 to_date('27.05.2020 02:08:33', 'dd.mm.yyyy hh24:mi:ss')
4 from dual
5 ),
6 diff as
7 (select cast(et as timestamp) - cast(st as timestamp) diff
8 from test
9 )
10 select extract(hour from diff) ||':'||
11 extract(minute from diff) ||':'||
12 extract(second from diff) diff
13 from diff;
DIFF
-------------------------------------------------------------------------
1:15:45
SQL>
You can further make it pretty (e.g. two digits for hours, using LPAD function). Or, you can even write your own function which will actually work on difference of DATE datatype values, do some calculations (using trunc function, subtractions, whatnot), but the above looks pretty elegant if compared to a home-made function.
The answer by Littlefoot is perfectly fine. This answer is just to show there is more than one way to get the result.
First, we can subtract one date from another and get the difference in days, then convert that difference to an interval.
with test (st, et) as
(select to_date('27.05.2020 00:52:48', 'dd.mm.yyyy hh24:mi:ss'),
to_date('27.05.2020 02:08:33', 'dd.mm.yyyy hh24:mi:ss')
from dual
)
select numtodsinterval(et-st, 'day') diff
from test;
Then, since we can't control interval formatting directly, we can add DIFF to an arbitrary date and then use built-in date formatting.
with test (st, et) as
(select to_date('27.05.2020 00:52:48', 'dd.mm.yyyy hh24:mi:ss'),
to_date('27.05.2020 02:08:33', 'dd.mm.yyyy hh24:mi:ss')
from dual
)
select to_char(date '1-1-1' + numtodsinterval(et-st, 'day'), 'hh24:mi:ss') diff
from test;
DIFF
--------
01:15:45

hour must be between 0 and 23 -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..

TO_DATE ISSUE IN CONVERSION

I tried to execute the below query but it's throwing me error :
SELECT TO_DATE(
TIMESTAMP '1970-01-01 00:00:00' + numtodsinterval(1511421211, 'second')
,'DD-MM-YYYY HH24:MI:SS')
FROM dual
Error : ORA-01830: date format picture ends before converting entire input string
The TO_DATE( datestring, format_model ) function takes strings as arguments.
Your query:
SELECT TO_DATE(
TIMESTAMP '1970-01-01 00:00:00' + numtodsinterval(1511421211, 'second')
,'DD-MM-YYYY HH24:MI:SS'
)
FROM dual
Is passing a TIMESTAMP and a string so Oracle has to perform an implicit conversion from TIMESTAMP to a string so your function is effectively:
SELECT TO_DATE(
TO_CHAR(
TIMESTAMP '1970-01-01 00:00:00' + numtodsinterval(1511421211, 'second'),
(
SELECT value
FROM NLS_SESSION_PARAMETERS
WHERE parameter = 'NLS_TIMESTAMP_FORMAT'
)
),
'DD-MM-YYYY HH24:MI:SS'
)
FROM dual
If the NLS_TIMESTAMP_FORMAT session paramter does not match your format model 'DD-MM-YYYY HH24:MI:SS' then an exception will be raised.
You could change the NLS_TIMESTAMP_FORMAT parameter - but this is a session parameter that is set per user and each user can change it at any time during their session so this should NOT be the solution.
Instead, you can just use a DATE literal instead of a TIMESTAMP literal:
SELECT DATE '1970-01-01' + NUMTODSINTERVAL (1511421211, 'second')
FROM DUAL
Or, if you want to use a timestamp then you can use the CAST function:
SELECT CAST(
TIMESTAMP '1970-01-01 00:00:00' + NUMTODSINTERVAL (1511421211, 'second')
AS DATE
)
FROM DUAL
Is this what you are expecting?
select to_char(DATE '1970-01-01' + NUMTODSINTERVAL (1511421211, 'second'), 'yyyy/mm/dd hh24:mi:ss') from dual;
The syntax appear somewhat incorrect to me. Try this:
SELECT TO_DATE ('1970-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
+ NUMTODSINTERVAL (1511421211, 'second')
FROM DUAL;
Edit:
As #a_horse.. said "Oracle DATE always contains a time",so if timestamp is not visible then you just need to see your NLS_DATE_FORMAT in table V$NLS_PARAMETERS. In your case its simply set to
NLS_DATE_FORMAT = 'DD-MM-YYYY';
So you need to alter the session first to get the timestamp in SQLPLUS. See below:
SQL> alter session set NLS_DATE_FORMAT = 'mm-dd-yyyy HH24:mi:ss';
Session altered.
SQL> SELECT TO_DATE ('1970-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS') + NUMTODSINTERVAL (15114212
11, 'second') FROM DUAL;
TO_DATE('1970-01-01
-------------------
11-23-2017 07:13:31

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.

Resources