oracle9i

Remove all characters after a specific character in PL/SQL

女生的网名这么多〃 提交于 2019-12-04 07:54:00
How do I get a substring from this example value: john.abc_1234 I want it to return john.abc .So basically we need to remove all the information after _ . More examples: 1234_abc You can use SUBSTR and INSTR : select substr('john.abc_1234', 1, instr('john.abc_1234', '_') -1) from dual Warning: This is only guaranteed to work if your string actually has an underscore in it Update Additionally, if you are running from Oracle 10g on, you could take the Regex path, which would more powerfully handle exceptions. Here are some links on how to do it in Oracle: http://psoug.org/reference/regexp.html

how to display data in descending order without using ORDER BY CLAUSE in Oracle

旧时模样 提交于 2019-12-04 07:13:28
问题 I am new in oracle and I have one question i.e.: how to display data in descending order without using ORDER BY CLAUSE in Oracle. whether it is in sql or pl/sql. 回答1: It is not possible to reliably retrieve sorted results without explicitly using ORDER BY... if you cannot use ORDER BY, you would need to organize the code in whichever programming language you're using to pull the data with which is ridiculous. 回答2: Avoid ORDER BY, use a hierarchical query with flat results, and ORDER SIBLINGS!

Greatest not null column

Deadly 提交于 2019-12-03 23:36:41
I need to update a row with a formula based on the largest value of two DATETIME columns. I would normally do this: GREATEST(date_one, date_two) However, both columns are allowed to be NULL. I need the greatest date even when the other is NULL (of course, I expect NULL when both are NULL) and GREATEST() returns NULL when one of the columns is NULL. This seems to work: GREATEST(COALESCE(date_one, date_two), COALESCE(date_two, date_one)) But I wonder... am I missing a more straightforward method? COALESCE(GREATEST(date_one, date_two), date_one, date_two) My solution for multiple columns is:

Why does a connect by expression in a FOR loop, execute only once?

核能气质少年 提交于 2019-12-03 09:47:19
I just found what I think is somewhat unexpected behavior in PLSQL vs SQL in Oracle. If I run this query on SQLDeveloper I get 5 results: select level lvl from dual connect by level <=5; But if i run this statement in SQLDeveloper: declare w_counter number :=0; begin for REC in (select level lvl from dual connect by level <=5) loop w_counter := w_counter+1; end loop; dbms_output.put_line('W_COUNTER: '|| w_counter); end; The variable w_counter finishes with value 1 (weird) but the weirdest part is that if I encapsulate the query in a subquery... something like: declare w_counter number :=0;

Using distinct on a column and doing order by on another column gives an error

妖精的绣舞 提交于 2019-12-03 05:58:30
I have a table: abc_test with columns n_num, k_str. This query doesnt work: select distinct(n_num) from abc_test order by(k_str) But this one works: select n_num from abc_test order by(k_str) How do DISTINCT and ORDER BY keywords work internally that output of both the queries is changed? Abhishek Bhandari As far as i understood from your question . distinct :- means select a distinct(all selected values should be unique). order By :- simply means to order the selected rows as per your requirement . The problem in your first query is For example : I have a table ID name 01 a 02 b 03 c 04 d 04

Defining a Character Set for a column For oracle database tables

拈花ヽ惹草 提交于 2019-12-03 02:30:29
I am running following query in SQL*Plus CREATE TABLE tbl_audit_trail ( id NUMBER(11) NOT NULL, old_value varchar2(255) NOT NULL, new_value varchar2(255) NOT NULL, action varchar2(20) CHARACTER SET latin1 NOT NULL, model varchar2(255) CHARACTER SET latin1 NOT NULL, field varchar2(64) CHARACTER SET latin1 NOT NULL, stamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, user_id NUMBER(11) NOT NULL, model_id varchar2(65) CHARACTER SET latin1 NOT NULL, PRIMARY KEY (id), KEY idx_action (action) ); I am getting following error: action varchar2(20) CHARACTER SET latin1 NOT NULL, * ERROR at line 5: ORA

Searching data from a table using stored procedure in oracle by passing tablename as a parameter

有些话、适合烂在心里 提交于 2019-12-02 22:13:31
问题 This procedure is not working properly. create or replace procedure bank_search_sp ( p_tablename in varchar2, p_searchname in varchar2, p_bankcode out varchar2, p_bankname out varchar2, p_dist_code out number ) as v_tem varchar2(5000); begin v_tem :='select bankcode,bankname,dist_code from ' || UPPER (p_tablename) || ' where bankname like '''|| p_searchname||''; execute immediate v_tem into p_bankcode,p_bankname,p_dist_code using p_searchname ; commit; end bank_search_sp; the Procedure is

Substring between characters in Oracle 9i

£可爱£侵袭症+ 提交于 2019-12-02 14:11:54
问题 In later versions I can use this regexp_substr : SELECT ID, regexp_substr(ID, '[^.]+', 1, 2) DATA 1, regexp_substr(ID, '[^.]+', 1, 3) DATA 2 FROM employees Table: Employees ID -------------------------- 2017.1.3001-ABC.01.01 2017.2.3002-ABCD.02.02 2017.303.3003-ABC.03.03 2017.404.3004-ABCD.04.04 Expected output: ID DATA 1 DATA 2 ------------------------ ------ --------- 2017.1.3001-ABC.01.01 1 3001-ABC 2017.2.3002-ABCD.02.02 2 3002-ABCD 2017.303.3003-ABC.03.03 303 3003-ABC 2017.404.3004-ABCD

Searching data from a table using stored procedure in oracle by passing tablename as a parameter

大兔子大兔子 提交于 2019-12-02 09:56:53
This procedure is not working properly. create or replace procedure bank_search_sp ( p_tablename in varchar2, p_searchname in varchar2, p_bankcode out varchar2, p_bankname out varchar2, p_dist_code out number ) as v_tem varchar2(5000); begin v_tem :='select bankcode,bankname,dist_code from ' || UPPER (p_tablename) || ' where bankname like '''|| p_searchname||''; execute immediate v_tem into p_bankcode,p_bankname,p_dist_code using p_searchname ; commit; end bank_search_sp; the Procedure is getting created but i dont know what actually happens when it was executed ,This is the error shown ORA

Oracle syntax error [duplicate]

左心房为你撑大大i 提交于 2019-12-01 16:28:21
This question already has an answer here: How do I limit the number of rows returned by an Oracle query after ordering? 16 answers I got the following error in Oracle: SELECT * FROM abcd WHERE name LIKE 'a%' LIMIT 10 * ERROR at line 1: ORA-00933: SQL command not properly ended What is the problem with the command? Oracle doesn't support the limit clause. That's a MySQL/Postgres thing. There are alternatives, although they're often a lot more involved http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html The simplest equivalent is: select * from abcd where name like 'a%' and