Escolar Documentos
Profissional Documentos
Cultura Documentos
in length (bigger version of the VARCHAR2 datatype). Note that a table can only have one LONG column.
How to find the number of characters and length of a long data type PL SQL Program..
How can I handle apostrophes and single quotes in strings? As you know, single quotes start and terminate strings in SQL.
3 quotes at the start of the string and two in the middle of the string.
There's Henry
In Oracle, I want to know if a string value is numeric only. How can I do this?
Answer: To test a string for numeric characters, you could use a combination of the length
function, trim function, and translate function built into Oracle. You can use the following command: LENGTH(TRIM(TRANSLATE(string1, ' +-.0123456789', ' '))) string1 is the string value that you are testing This function will return a null value if string1 is numeric. It will return a value "greater than 0" if string1 contains any non-numeric characters. For example, LENGTH(TRIM(TRANSLATE('123b', ' +-.0123456789',' '))); LENGTH(TRIM(TRANSLATE('a123b', ' +-.0123456789',' '))); LENGTH(TRIM(TRANSLATE('1256.54', ' +-.0123456789',' '))); LENGTH(TRIM(TRANSLATE ('-56', ' +-.0123456789',' '))); would return 1 would return 2 would return null would return null
Translate Function
The syntax for the translate function is: translate( string1, string_to_replace, replacement_string ) string1 is the string to replace a sequence of characters with another set of characters. string_to_replace is the string that will be searched for in string1. replacement_string - All characters in the string_to_replace will be replaced with the corresponding character in the replacement_string.
How will i find out all the queries and the resources consumed by the queries
What is the difference between an "empty" value and a "null" value? When I select those fields that are "empty" versus "null", I get two different result sets.
select * from suppliers where supplier_name = ''; When you run this statement, you'd expect to retrieve the row that you inserted above. But instead, this statement will not retrieve any records at all. Now, try retrieving all supplier_ids that contain a null value: select * from suppliers where supplier_name is null; When you run this statement, you will retrieve both rows. This is because Oracle has now changed its rules so that empty strings behave as null values. It is also important to note that the null value is unique in that you can not use the usual operands (=, <, >, etc) on a null value. Instead, you must use the IS NULL and IS NOT NULL conditions.
Question: I have a string that contains the full path to a file. How can I extract the
directory path from the string in Oracle? For example, if I have a full file path as follows: 'c:\windows\temp\example.xls' I want to retrieve the following: 'c:\windows\temp'
Answer: You can create a custom function in Oracle that will retrieve the directory path
from the string value. The following function called get_dir_path will extract the directory path. It will work with both Windows and UNIX system file paths. CREATE or REPLACE function get_dir_path (p_path IN VARCHAR2) RETURN varchar2 IS v_dir VARCHAR2(1500); BEGIN -- Parse string for UNIX system IF instr(p_path,'/') > 0 THEN v_dir := substr(p_path,1,(instr(p_path,'/',-1,1)-1)); -- Parse string for Windows system ELSIF instr(p_path,'\') > 0 THEN v_dir := substr(p_path,1,(instr(p_path,'\',-1,1)-1));
-- If no slashes were found, return the original string ELSE v_dir := p_path; END IF; RETURN v_dir; END; Once the above function has been created, you can reference this function in your SQL statement. For example, SELECT get_dir_path('c:\windows\temp\example.xls') FROM dual; This SQL statement would return 'c:\windows\temp'.
Question: In Oracle, how can I retrieve the total elapsed time in minutes between two
dates?
Answer: To retrieve the total elapsed time in minutes, you can execute the following SQL:
select (endingDateTime - startingDateTime) * 1440 from table_name; Since taking the difference between two dates in Oracle returns the difference in fractional days, you need to multiply the result by 1440 to translate your result into elapsed minutes. 24 * 60 = 1440 24 hours in a day * 60 minutes in an hour
Question: In PSQL, I want to declare a cursor within cursor. The second cursor should
use a value from the first cursor in the "where clause". How can I do this?
cursor get_columns is select distinct col.column_name from all_tab_columns col where col.owner = v_owner and col.table_name = v_table_name; begin -- Open first cursor open get_tables; loop fetch get_tables into v_owner, v_table_name; -- Open second cursor open get_columns; loop fetch get_columns into v_column_name; end loop; close get_columns; end loop; close get_tables; EXCEPTION WHEN OTHERS THEN raise_application_error(-20001,'An error was encountered '||SQLCODE||' -ERROR- '||SQLERRM); end MULTIPLE_CURSORS_PROC; The trick to declaring a cursor within a cursor is that you need to continue to open and close the second cursor each time a new record is retrieved from the first cursor. That way, the second cursor will use the new variable values from the first cursor.
What is the advantages and disadvantages of package a compared to the stored procedure.
For Example
parameter_1= "Chianti" parameter_2= "10"
parameter_3= wasn't entered by the user but I have to use in the select statement. And this is my problem. How to initialize this parameter to get all rows for column3? SELECT * FROM WHERE column1 AND column2 = AND column3 = wine = parameter_1 parameter_2 parameter_3;
Answer: To solve your problem, you will need to output a dynamic PLSQL cursor in
Oracle. Let's take a look at how we can do this. We've divided this process into 3 steps.
We've made this table definition very simple, for demonstration purposes.
create or replace procedure find_wine2 (col1_in in varchar2, col2_in in varchar2, col3_in in varchar2, c1 out winepkg.wine_type) as BEGIN /* all columns were entered */ IF (length(col1_in) > 0) and (length(col2_in) > 0) and (length(col3_in) > 0) THEN OPEN c1 FOR select * from wine where wine.col1 = col1_in and wine.col2 = col2_in and wine.col3 = col3_in; /* col1 and col2 were entered */ ELSIF (length(col1_in) > 0) and (length(col2_in) > 0) and (length(col3_in) = 0) THEN OPEN c1 FOR select * from wine where wine.col1 = col1_in and wine.col2 = col2_in; /* col1 and col3 were entered */ ELSIF (length(col1_in) > 0) and (length(col2_in) = 0) and (length(col3_in) > 0) THEN OPEN c1 FOR select * from wine where wine.col1 = col1_in wine.col3 = col3_in; /* col2 and col3 where entered */ ELSIF (length(col1_in) = 0) and (length(col2_in) > 0) and (length(col3_in) > 0) THEN OPEN c1 FOR select * from wine where wine.col2 = col2_in and wine.col3 = col3_in; /* col1 was entered */ ELSIF (length(col1_in) > 0) and (length(col2_in) = 0) and (length(col3_in) = 0) THEN
OPEN c1 FOR select * from wine where wine.col1 = col1_in; /* col2 was entered */ ELSIF (length(col1_in) = 0) and (length(col2_in) > 0) and (length(col3_in) = 0) THEN OPEN c1 FOR select * from wine where wine.col2 = col2_in; /* col3 was entered */ ELSIF (length(col1_in) = 0) and (length(col2_in) = 0) and (length(col3_in) > 0) THEN OPEN c1 FOR select * from wine where wine.col3 = col3_in; END IF; END find_wine2;
Resolution
The options are to resolve the compilation/authorization errors, disable the trigger, or drop the trigger. You can also try running the following command to check for errors on the trigger: show errors trigger trigger_name; Replace trigger_name with the name of your trigger.
Question: How can I retrieve the third highest salary amount from a salary table?
Answer: To retrieve the third highest salary from a salary table, you could run the following
query: (please note that the subquery is sorted in descending order) SELECT salary_amount FROM (select salary2.*, rownum rnum from (select * from salary ORDER BY salary_amount DESC) salary2 where rownum <= 3 ) WHERE rnum >= 3; If you wanted to retrieve all fields from the salary table for the third highest salary, you could run the following query: (please note that the subquery is sorted in descending order) SELECT * FROM (select salary2.*, rownum rnum from (select * from salary ORDER BY salary_amount DESC) salary2 where rownum <= 3 ) WHERE rnum >= 3;
Question: I am trying to find the average time between two dates, using PLSQL.
For example: If I wanted the average time between May 1 and May 3, I should get May 2.
Answer: To find the average time between two dates, you could try the following:
select to_date(date1, 'yyyy/mm/dd') + ((to_date(date2, 'yyyy/mm/dd') - to_date(date1, 'yyyy/mm/dd')) /2 ) from dual; This will calculate the elapsed time between date1 and date2. Then it takes half of the elapsed time and adds it to date1. This should give you the average date. For example, if you wanted to find the average date between May 1 and May 3, you would do the following: select to_date('2003/05/01', 'yyyy/mm/dd') + ((to_date('2003/05/03', 'yyyy/mm/dd') - to_date('2003/05/01', 'yyyy/mm/dd')) / 2) from dual;
Question: I am trying to find the average time between two dates, using PLSQL.
For example: If I wanted the average time between May 1 and May 3, I should get May 2.
Answer: To find the average time between two dates, you could try the following:
select to_date(date1, 'yyyy/mm/dd') + ((to_date(date2, 'yyyy/mm/dd') - to_date(date1, 'yyyy/mm/dd')) /2 ) from dual; This will calculate the elapsed time between date1 and date2. Then it takes half of the elapsed time and adds it to date1. This should give you the average date. For example, if you wanted to find the average date between May 1 and May 3, you would do the following: select to_date('2003/05/01', 'yyyy/mm/dd') + ((to_date('2003/05/03', 'yyyy/mm/dd') - to_date('2003/05/01', 'yyyy/mm/dd')) / 2) from dual;
Answer: You can retrieve the instance name using the sys_context function.
To retrieve the Oracle instance name, you execute the following SQL statement: select sys_context('USERENV','DB_NAME') as Instance from dual; It should return something like this: INSTANCE ------------------------------------------------------------------------------------DBPROD
Question: How do I determine if a table has a primary key and if it has one, how do I
determine what columns are in the primary key?
Answer: You can retrieve primary key information with the following SQL statement:
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner FROM all_constraints cons, all_cons_columns cols WHERE cons.constraint_type = 'P' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner ORDER BY cols.table_name, cols.position; If you knew the table name that you were looking for, you could modify the SQL as follows:
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner FROM all_constraints cons, all_cons_columns cols WHERE cols.table_name = 'TABLE_NAME' AND cons.constraint_type = 'P' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner ORDER BY cols.table_name, cols.position; Make sure to type the table_name in uppercase, as Oracle stores all table names in uppercase. Let's quickly explain the output from this query. table_name is the name of the table (stored in uppercase). column_name is the name of the column that is a part of the primary key. (also stored in uppercase) position is the position in the primary key. A primary key can contain more than one column, so understanding the order of the columns in the primary key is very important. status indicates whether the primary key is currently enabled or disabled. owner indicates the schema that owns the table.
Question: When you use a GROUP BY clause with one or more columns, will the results
be in the sorted order of GROUP BY columns (by default) or shall we use ORDER BY clause?
Answer: You may get lucky and find that your result set is sorted in the order of the
GROUP BY columns, but we recommend always using the ORDER BY clause whenever sorting is required.
Example #1
SELECT department, depart_head, SUM(sales) as "Total sales" FROM order_details GROUP BY department, depart_head ORDER BY department; This example would sort your results by department, in ascending order.
Example #2
SELECT department, depart_head, SUM(sales) as "Total sales" FROM order_details GROUP BY department, depart_head ORDER BY department desc, depart_head; This example would first sort your results by department in descending order, then depart_head in ascending order.
Question: I'm working in an SQL prompt (like SQLPlus). After doing a few DML
operations, I execute a ROLLBACK in which cases it rolls back to the proper save point area. However, if I execute a DDL in the middle of the DML operations, I don't ROLLBACK to the same save point. The DDL's are not related to the table that I'm executing the DML's for. How come Oracle rolls back to a different save point when I execute a DDL?
Answer: First, let's explain some of the terminology that you used in your question.
DML stands for "Data Manipulation Language". A DML operation includes SELECT, INSERT, UPDATE, and DELETE statements. DDL stands for "Data Definition Language". A DDL operation includes CREATE TABLE, CREATE INDEX, among other operations. The Rollback statement undoes all changes for the current session up to the savepoint specified. When you execute a DDL operation, there is an implied commit after the DDL. The save point is then changed to the position following the DDL operation. Therefore, any rollbacks will only undo changes to the new save point - which is after the last DDL operation.