Check queries output type - oracle

I'm working with Oracle DB and faced with a problem. I cannot say for sure what the type will value have once the output is generated.
I have the following simple query:
select 1+'1234' from dual;
For me it's clear that the output "1235" will present a simple number. But how can I check that it is not converted into text afterwards for example? Is there any kind of "type_of" function which will help me with that?
Thank you in advance for your help!

You can use the dump() function:
select dump(1+'1234') from dual;
which will give you the datatype, as well as the byte values used to represent tha value, which you don’t seem to be interested in here.
Typ=2 Len=3: 194,13,36
where type 2 is NUMBER.

I think a way to find the return type would be to create a table using that query.
for eg.
create table temp_table_name1 as select 1+'1234' a from dual;
desc temp_table_name1;
Should give the type.
If you are worried about the return type then you can manually set it using
select Cast(1+'1234' As Varchar2(30)) a from dual;
OR
select Cast(1+'1234' As Number(4)) a from dual;

Related

replace text within Oracle variable that is a CLOB

I have an Oracle procedure that accepts a parameter (v_MessageText) to update a column called message_text on a table. The column message_text is a CLOB. I would like to be able to replace text within the input so if a user inputs testABC it will output testXYZ into the column message_text. At first, it seemed simple. I tried running the code below within my procedure but I am getting an error:
SELECT REPLACE (v_MessageText, 'ABC', 'XYZ') from dual;
The error Oracle gives me is: "PLS-00428: an INTO clause is expected in this select statement". Why? What am I doing wrong here?
Never mind - very simple! Issue is fixed by using this SQL instead:
SELECT REPLACE (v_MessageText, 'ABC', 'XYZ') into v_MessageText from dual;

How to get information about a User-Defined Type?

In simplicity, PL/SQL generally follow the following:
DECLARE
Variable declaration
BEGIN
Program Execution
EXCEPTION
Exception handling
END;
I am quite new to PL/SQL and i am looking at the variable declaration section where i would like to find out more information on SALES_PRODUCT_TY_LIST.
Is there a table i may look up to check on information on SALES_PRODUCT_TY_LIST, such as checking out table column information from all_tab_cols view?
CREATE OR REPLACE PROCEDURE GET_DISCOUNTS
(
v_have_list SALES_PRODUCT_TY_LIST
)
IS
QUERY VARCHAR(5000);
...
Thanks.
The Oracle database has an extensive data dictionary (what some other DBMS products call the INFORMATION SCHEMA). You can find all the views here. Alas, the revised ToC structure makes it harder to find something in the 11g documentation unless you already know what you're looking for, so use the index instead. 8-)
Anyway, the views you need to query are ALL_TYPES and ALL_TYPE_ATTRS.
This seems to be user defined collection type. You can find some information about it querying all_types/user_types view:
select * from user_types where type_name = 'SALES_PRODUCT_TY_LIST'
The definition of the type can be found for example by querying all_source/user_source view:
select text from user_source where name = 'SALES_PRODUCT_TY_LIST' order by line
Try this to get DDL:
SELECT dbms_log.substr(dbms_metadata.get_ddl('TYPE', 'SALES_PRODUCT_TY_LIST'), 32767,1)
FROM DUAL;
see: http://www.myoracleguide.com/s/gen_schema.htm
Ok i found something:
select *
from all_objects
where object_name like 'SALES%';

Good way to deal with comma separated values in oracle

I am getting passed comma separated values to a stored procedure in oracle. I want to treat these values as a table so that I can use them in a query like:
select * from tabl_a where column_b in (<csv values passed in>)
What is the best way to do this in 11g?
Right now we are looping through these one by one and inserting them into a gtt which I think is inefficient.
Any pointers?
This solves exactly same problem
Ask Tom
Oracle does not come with a built-in tokenizer. But it is possible to roll our own, using SQL Types and PL/SQL. I have posted a sample solution in this other SO thread.
That would enable a solution like this:
select * from tabl_a
where column_b in ( select *
from table (str_to_number_tokens (<csv values passed in>)))
/
In 11g you can use the "occurrence" parameter of REGEXP_SUBSTR to select the values directly in SQL:
select regexp_substr(<csv values passed in>,'[^,]+',1,level) val
from dual
connect by level < regexp_count(<csv values passed in>,',')+2;
But since regexp_substr is somewhat expensive I am not sure if it is the most effective in terms of being the fastest.

number format in oracle

Hai,
i have a problem with number format.i'm using oracle.
I have a number field in database.But when i retreive it i need to be seen as floating point number
For example:
while retreiveing,now i got the result as 200 DR (DR for Debit,it is given manually).
Now i need to get the result as 200.00 DR as the result.
How can i solve this?Can any one help me?
You can use the TO_CHAR function to explicitely format data:
SQL> SELECT to_char(200.00, 'fm999G999G990D00') FROM dual;
TO_CHAR(200.00,'FM999G999G990D
------------------------------
200.00
Use TO_CHAR like this:
to_char(number,'999999.99')
For example:
SQL> select to_char(1234.5678,'999999.99')||' DR' as display from dual;
DISPLAY
-------------
1234.57 DR
The presence of a debit implies the need for a credit. In Oracle SQL we can use the SIGN() function to tell whether a number is positive or negative...
SQL> select to_char(abs(amt), 'fm999g999d00')||' '
2 ||case when sign(amt) = -1 then 'DR' else 'CR' end as fmt_amt
3 from transactions
4 order by txn_ts, txn_id
5 /
FMT_AMT
--------------
200.00 CR
200.00 DR
788.67 CR
788.67 DR
SQL>
The answers here that suggested TO_CHAR are correct, but if you're calling this SQL from the application code:
Get the number without formatting it with the SQL and then use your programming language to format it. For example, in Java, use the DecimalFormat class. In other words, leave formatting for the application specific code, not for the SQL.
The additional characters can be specified as part of the conversion by enclosing them in double-quotes, which might make things a little more simple:
To_Char(amount,'fm999G999G990D00" DR"')

How do you convert SYS_GUID() to varchar?

In oracle 10g, how do you convert SYS_GUID() to varchar? I am trying something like:
select USER_GUID from user where email = 'user#example.com'
Which returns the RAW byte[]. Is it possible to use a function to convert the RAW to VARCHAR2 in the SQL statement?
Don't forget to use HEXTORAW(varchar2) when comparing this value to the RAW columns.
There is no implicit convesion from VARCHAR2 to RAW. That means that this clause:
WHERE raw_column = :varchar_value
will be impicitly converted into:
WHERE RAWTOHEX(raw_column) = :varchar_value
, thus making indices on raw_column unusable.
Use:
WHERE raw_column = HEXTORAW(:varchar_value)
instead.
Use RAWTOHEX(USER_GUID).
select RAWTOHEX(USER_GUID) from user where email = 'user#example.com'
Please don't mod-1 if I'm wrong. I'm going from memory so this a disclaimer to verify.
TO_CHAR is actually different between SQL and PL/SQL.
In SQL TO_CHAR does not take a raw as you have found out.
In PL/SQL To_CHAR will take a raw value.
So if you're in a procedure anyways, sometimes it easier to use a variable, but if you're just using SQL, go with the other answers here.
select CAST (USER_GUID AS VARCHAR2(100)) from user where email = 'user#example.com'

Resources