I read a lot of tutorial which uses hh23 and hh24 interchangeably. to do to_char on oracle. Is the hh23 a legacy syntax? I tried doing it on simple query and it causes an error.
select to_char(sysdate, 'hh23'), to_char(sysdate, 'hh24') from dual
I'm trying to find a reference to this but there is none. Or is the tutorial just written wrong? For example on http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:59412348055.
I think it's just a typo.
SQL> select to_char(sysdate, 'hh23:mi:ss') from dual
2 /
select to_char(sysdate, 'hh23:mi:ss') from dual
*
ERROR at line 1:
ORA-01821: date format not recognized
SQL> select to_char(sysdate, 'hh24:mi:ss') from dual
2 /
TO_CHAR(
--------
11:25:21
SQL>
It is just a typing error in untested code. 'hh23' will always give an error.
Related
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..
I have SCN:
SELECT TIMESTAMP_TO_SCN(SYSTIMESTAMP) SCN FROM DUAL;
I can convert it to time stamp:
SELECT SCN_TO_TIMESTAMP(6480157) FROM DUAL;
When I want to mix this two select Im getting error:
SELECT SCN_TO_TIMESTAMP(SELECT TIMESTAMP_TO_SCN(SYSTIMESTAMP) FROM DUAL) FROM DUAL;
ORA-00936: missing expression
Please use
SELECT SCN_TO_TIMESTAMP(TIMESTAMP_TO_SCN(SYSTIMESTAMP)) FROM DUAL;
#F.Madsen has the correct and simplest answer, but just to illustrate, you can get to the result following your logic:
SELECT SCN_TO_TIMESTAMP(SCN) FROM
(
SELECT (TIMESTAMP_TO_SCN(SYSTIMESTAMP)) SCN FROM DUAL
);
This question already has an answer here:
TO_DATE function time zone parse error
(1 answer)
Closed 4 years ago.
I try to parse a time-string on a Oracle 12. Problem is the TZH I think.
SQL> select to_char(current_timestamp, 'DD.MM.YYYY HH24:MI TZH') from dual;
TO_CHAR(CURRENT_TIME
--------------------
05.07.2018 16:55 +02
But it is not working the other direction...
SQL> select TO_DATE('05.07.2018 16:53 +02', 'DD.MM.YYYY HH24:MI TZH') from dual;
select TO_DATE('05.07.2018 16:53 +02', 'DD.MM.YYYY HH24:MI TZH') from dual
*
FEHLER in Zeile 1:
ORA-01821: Datumsformat nicht erkannt
It does not like the format. Also not working if I remove the +.
As was mentioned in the comments, TO_DATE does not work with a time zone format mask. Instead, use TO_TIMESTAMP_TZ, which does accept this:
SELECT TO_TIMESTAMP_TZ('05.07.2018 16:53 +02:00', 'DD.MM.YYYY HH24:MI TZH:TZM')
FROM dual;
Demo
It's not surprising that this is a source of confusion, because much of the documentation I saw online for TO_DATE mentions the TZH and TZM format masks, despite that they crash the function if used.
select TO_CHAR(to_date(sysdate, 'DD-MON-YYYY'), 'DAY') FROM DUAL;
When I run this query the output was : SUNDAY. But we know today is Tuesday(1-1-2013).
And
then changed the query as
select TO_CHAR(to_date('01-JAN-2013', 'DD-MON-YYYY'), 'DAY') FROM DUAL;
answer was :TUESDAY.
then Changed query as
select TO_CHAR(to_date(sysdate+1, 'DD-MON-YYYY'), 'DAY') FROM DUAL;
answer is :MONDAY.
When I using the sysdate why it is show SUNDAY as output?
I am new in oracle db. Please help me.
use this:
select TO_CHAR(sysdate, 'DAY') FROM DUAL;
you are using this :
to_date(sysdate, 'DD-MON-YYYY')
which is giving you date=1/1/0013 which is sunday
Please refer the documentation for sysdate here. Sysdate is already a date data type.
Your example query is inappropriate as to_date function takes first parameter as String not date.
Try the simple query below:
select TO_CHAR(sysdate, 'DAY') FROM DUAL;
This should return TUESDAY as output.
To_date is used to convert a strin to date. As sysdate is already a date, one must not add add to_date.
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;