SCN to TimeStamp - wrong expression? - oracle

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

Related

Custom Exception Handling on Select Query

I'v been looking around, and not able to find anything helpful in my specific situation. I'm attempting to debug a query that I have limited access to. I cannot access the database itself, but I can query the database.
The query is something along the lines of
SELECT col1, col2, col3 FROM table WHERE col1 = TRUNC(:varibleName)
I am fairly sure the :variableName is being sent as a date+time, and col1 is a date column. However, I get the error, "Expected Date, got Number".
What I would like to do is see what the value of :variableName is when being run in this query. I have been attempting to find out by using custom exception handling but it doesn't seem like I can run a simple query.
Is there any way I can see the value of the variable when it causes an error?
You may use TO_DATE and pass variable in a particular format.
SELECT col1, col2, col3
FROM table WHERE col1 = TRUNC(:to_date(:varibleName,'yyyy-mm-dd'))
Now, you can pass a string like 2019-09-01 , 2019-07-20 etc.
Add-on to Kaushik,
Query:
To replicate the error:
Error:ORA-00932: inconsistent datatypes: expected DATE got NUMBER
with cte as (select trunc(sysdate) as dt, sysdate-1 as yst_dt from dual)
select * from cte where dt=TRUNC(2019-07-18) ;
By using To_Date format:
with cte as (select trunc(sysdate) as dt, sysdate-1 as yst_dt from dual)
select * from cte where dt=TRUNC(to_date('2019-07-18','yyyy-mm-dd')) ;

Convert Numeric format dates to dates in Oracle

This is how my Date column looks like
RPT_DT (Data Type is Number)
20180131
20180130
20180129
I wanna extract month out of these dates(either Month or mm), and I tried below
select extract(month from to_date(Rpt_dt))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)
I am getting the error "Not a valid month"
if there is not any particular reason to have a double conversion I would suggest you to handle the problem with this simple query:
select substr(to_char(RPT_DT),5,2)from THE_TABLE
this query should be more performant since it make only one conversion. in your sample you transform:
a number to a date
then a date to a char
the char again in date
finally you extract the month
let me know if it help
r.
try this,
SELECT EXTRACT(MONTH FROM TO_DATE(rpt_dt, 'YYYYMMDD'))
FROM TABLE_NAME;
and I believe you need to modify your query as you did not put the format 'MM/DD/YYYY',
select extract(month from to_date(Rpt_dt, 'MM/DD/YYYY'))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)
This back-and-forth conversion is useless. Try this
select
extract(month from to_date(RPT_DT,'yyyymmdd'))
from TABLE_NAME;

to_date function with sysdate

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.

Short test statements in PL-SQL?

In SQL server I can print out the value of something with a select statement.
SELECT 'xyz'
SELECT GetDate()
Can I do something similar in Oracle without adding FROM <tablename>?
This is the purpose of the dual table. Oracle supplies the Dual in every database and it's accessible, by default, to everyone that connects. It's a single-row, single column table that is useful for testing expressions and pseuducolumns against. Example
SELECT 'xyz' from dual;
SQL> select user,sysdate,lower(user) loweruser, 10*1023 from dual;
USER SYSDATE LOWERUSER 10*1023
---------- ---------- ---------- ----------
NKODNER 22-NOV-11 nkodner 10230
There is a dual dummy table in Oracle, so try:
SELECT GetDate() FROM dual
You can't. You must use DUAL fictious table
To get current system date, you would type
SELECT SYSDATE FROM DUAL

What does hh23 mean on oracle

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.

Resources