sort characters in a varchar2
HelloI have two tables of data I want to align. The two paintings have names of places. The format of the names in the two tables is similar, but slightly different.
for example.
Table 1: The Museum of life
Table2: Life, Museum of
My idea was to sort all characters in the string of two (while dropping whatever it is not alphanumeric and case sensitive) to harmonize the two.
for example.
The Museum of life-> EEEFFILMMOSUU
Life, the Museum of the-> EEEFFILMMOSUU
Only, I can't figure out a way to do it in Oracle. I imagine it would be possible to write a PL/SQL script, but before going into this, I thought I'd see if there is a built-in way to do what I'm trying to do, or maybe someone else has a better idea to do what I'm trying to accomplish?
Thanks for any help and advice.
Strange, you cannot use my suggestion. Anyway, you don't need two functions. Just take advantage of associative arrays and eliminate the need for sorting:
SQL> CREATE OR REPLACE
2 FUNCTION ordered_characters(
3 p_str VARCHAR2
4 )
5 RETURN VARCHAR2
6 IS
7 TYPE char_tbl_type IS TABLE of VARCHAR2(1)
8 INDEX BY VARCHAR2(10);
9 v_char_tbl char_tbl_type;
10 v_char VARCHAR2(1);
11 v_result VARCHAR2(4000);
12 v_idx VARCHAR2(10);
13 BEGIN
14 FOR i IN 1..nvl(length(p_str),0) LOOP
15 v_char := substr(p_str,i,1);
16 v_char_tbl(v_char || to_char(i,'0000')) := v_char;
17 END LOOP;
18 v_idx := v_char_tbl.FIRST;
19 FOR i IN 1..nvl(length(p_str),0) LOOP
20 v_result := v_result || v_char_tbl(v_idx);
21 v_idx := v_char_tbl.NEXT(v_idx);
22 END LOOP;
23 RETURN v_result;
24 END;
25 /
Function created.
SQL> COLUMN ordered_characters FORMAT A10
SQL> SELECT ename,
2 ordered_characters(ename) ordered_characters
3 FROM emp
4 /
ENAME ORDERED_CH
---------- ----------
SMITH HIMST
ALLEN AELLN
WARD ADRW
JONES EJNOS
MARTIN AIMNRT
BLAKE ABEKL
CLARK ACKLR
SCOTT COSTT
KING GIKN
TURNER ENRRTU
ADAMS AADMS
ENAME ORDERED_CH
---------- ----------
JAMES AEJMS
FORD DFOR
MILLER EILLMR
14 rows selected.
SQL>
SY.
Tags: Database
Similar Questions
-
Hello everyone,I am a developer concerned on the following problem, I have in my recent copy of data.
I create a table using a CREATE TABLE AS SELECT OPTION in a 11.2 g database located on RedHat Linux operating system.
the table is on the 10.2 on Solaris database, operating system, the problem is that the varchar2 data type is changed in the target database.
for example
data:
Field1 varchar2 (2)
to the data
Field1 varchar2 (6)
all varchar2 fields are translated as I have explained before. (expanded 3 times of the original field size is)
data types: number, float, date of: no problem
I use a dblink in order to access the data and my Oracle client is 10.2.0 version.
Colud you help me to solve this problem,
I appreciate any idea that you should have.
Kind regards.
By default, if you declare a column VARCHAR2 (2), you are allocating 2 bytes of storage. In your data base ISO-8859-1, which is enough to store 2 characters, because each character requires 1 byte of storage. In your database to UTF-8, however, which may not be enough store a single character since UTF-8 is a variable-width character set and each character it will take between 1 and 3 bytes of storage.
If you declare a column VARCHAR2 (2 CHAR), on the other hand, you use the semantics of length in characters rather than the semantics of length in bytes. Which allocates space for 2 characters regardless of whether it requires 2 or 6 bytes of storage under the covers (subject to the same limit of 4000 bytes the maximum size of a VARCHAR2). As a general rule, if you develop a UTF-8 database, you want to be using character length semantics. Which may involve specifying way explicit semantics of length in characters on each VARCHAR2 column. It may involve making an ALTER SESSION set NLS_LENGTH_SEMANTICS to CHAR before executing the CREATE TABLE statement. Whatever it is, you'll probably want to do first your CREATE TABLE and then run an INSERT.
If you do not use the semantics of length in characters, you will need to increase the size of your columns in order to ensure that you have a sufficient space in the database to UTF-8 for all your data.
Justin
-
Hi all
I need to store more than 4000 space characters in a column varchar2 oracle database 10g.
can any one guide me how to store more than 4000 characters in a varchar2 column...
any help is deeply appreciated.user12284380 wrote:
Hi all
I need to store more than 4000 space characters in a column varchar2 oracle database 10g.can any one guide me how to store more than 4000 characters in a varchar2 column...
any help is deeply appreciated.
Yes, use a CLOB data type.
-
difference between the view and the procedure
Hi Master,
Today I received a request from my senior technical leader... !!
Q > what is the difference between the view and the procedure?
I got response like this...
A view represents the logic of one or more tables/views. It won't take no place in db... It is a virtual table, query will only store in db... useful
for safety... If the main table has obtained changes... automatically discovers also change... etc.
Proecudure is a db object will be performs a perticular action... It may or may not return values. ?
But somehow, he isn't happy... He expects more... .very close view of diff. b & w and procedure.
Please explain it to me...!
Concerning
AR
874273 wrote:
Hi Blu,
Thanks for your replies...! But we think if notice and procedure will be the same no matter what point of view? Both are the same? If Design view... We can create a stored procedure?
How a view and procedure may do the same thing? They are two different things with different objectives. You use the correct one depending on what you want to achieve; they are not interchangeable.
And another question...!
(Q) how you will store Japan, characters of China in a database table? will there be a new feature in Oracle 11 g?
(A) I use dbms_lob package? Japan/China characters are multi byte characters... so we can use the nclob data type?
is this correct... ?
No, it not there no novelty in Oracle 11 g for the storage of Japanese or Chinese characters, because there is already a feature for their storage in previous versions as well. It is determined by the parameters characterset and nationality that you use when you create the database.
Why would you choose to store them in a LOB using DBMS_LOB? Yes, they have multibyte characters, but if you have configured your database for the correct character sets, for example using UTF - 8, then you can store the characters in VARCHAR2, like any other language. The only difference is that the storage of 4000 bytes limit still applies, so you maybe isn't able to store 4000 multibyte characters in a varchar2 column 4000 single-byte, it can be as little as 1000 characters if they are all characters in 4 bytes. (Note: 12 c limit in SQL can be increased up to 32,767 characters, to harmonize it with variable varchar2 PL)
-
Hey,.
I have a small doubt not a large...
Forming the dynamic sql code.
-I have to train a SQL like this
Select * from t1
Union
Select * from t2
Union
Select * T3...
I could train sql but an additional union is added at the end.
I am unable to use rtrim my intervention is more than 4,000 characters.
declare
V_Sql Varchar2 (32767).
V_TBL VARCHAR2 (32767).
V_Sql1 Varchar2 (32767).
v_sql2 varchar2 (32767).
cursor c1 is select table_name from user_tables where table_name like '% t';
Start
for the l1 c1 loop
v_tbl: = l1.table_name;
v_sql: = ' Select * from '. v_tbl | "the union";
v_sql1: = v_sql1 | v_sql;
End loop;
Insert Into X values (V_Sql1);
commit;
end;
Thank youuser13309379 wrote:
I am unable to use rtrim my intervention is more than 4,000 characters.You do not want to use RTRIM anyway; It would remove any 'U,' n, ' I has or ' O that happened at the end of the name.
You want to use SUBSTR, and (at least in Oracle 10 and more) it works very well on VARCHAR2s more than 4000 characters:set serveroutput on declare V_Sql1 Varchar2 (32767) := RPAD ('X', 20000, 'Y'); begin dbms_output.put_line (LENGTH (v_sql1) || ' = LENGTH before'); v_sql1 := SUBSTR ( v_sql1 , 1 , LENGTH (v_sql1) - 7 ); dbms_output.put_line (LENGTH (v_sql1) || ' = LENGTH after'); end;
produces this output:
20000 = LENGTH before 19993 = LENGTH after
-
Sort problem: ORDER BY the numbers all first instead of characters first
Hello
I need help with the problem of the following sort:
create table test01 (sorting_column varchar2 (30));
insert into values test01 ('Test123');
insert into values test01 ("Test987");
insert into values test01 ("123Test");
insert into values test01 ("987Test");
insert into test01 values (string 'test');
Select sorting_column from test01 arrested by sorting_column;
SORTING_COLUMN
------------------------------
Test string
Test123
Test987
123Test
987Test
This is not the order that I need. I really wish it were (numbers before characters):
123Test
987Test
Test123
Test987
Test string
Can anyone help?
Thanks in advance,
MarcSELECT sorting_column FROM test01 ORDER BY nlssort(sorting_column, 'NLS_SORT=BINARY') ;
Can be set at the level of the session as well:
ALTER SESSION SET NLS_SORT = BINARY;
Published by: odie_63 on June 20, 2011 17:45
-
Hi all
I ran into this somewhat misleading error message, a DDL, an institution which, when I run a DML statement. What I try to do is, I set everything first NLS_LANGUAGE = NLS_SORT "AMERICAN" = "GENERIC_M" values for the session and then retrieve the data from a table, you place your order by a column called description_value (varchar2 (4000)).
Like this
ALTER SESSION SET NLS_LANGUAGE = NLS_SORT "AMERICAN" = "GENERIC_M."
Select * from tableA by description_value
The light of the foregoing, I execute via Java, but I am able to run roughly in the same error message in TOAD/PL-SQL developer. The error message is as below,
ORA-00910: specified length too long for its data type
_ Some points:
1. I get this problem only when there are more than 500 record characters with the length of the column description_value. Anything less than 500 characters, I don't get the error message.
2. when I put the NLS_SORT 'BINARY' or 'FRENCH', I do not run on this issue. The query runs correctly.
3. I never had this problem in 10 g. This question arose in 11.
I did a bit of google but din't find any specific reference to anyone else having this problem. Please share your thoughts and comments on this and please let me know if you need more information about it.
Published by: user13405584 on December 2, 2010 04:20Hello
I was too quick to research. It is indeed wrong. You should read ""Bug 9723547: THE LIMITATION OF the FUNCTION NLSSORT '. " This bug describes the behavior of the NLSSORT and there are bugs in mentioned to correct a wrong implementation of a bug sooner. Because you do not give your exact version of Oracle, take a look and see a patch that you need to correct the false correction of the bug.
Besides in this bug, it is also clear, documentation was not correct. This is now fixed in the Guide of globalization: kinds of Linguistics [url http://download.oracle.com/docs/cd/E11882_01/server.112/e10729/ch5lingsort.htm#insertedID6], under classification keys. There is the claim that a sort on a kind of multi-langual can be on about 500 characters.
Read the bug and solutions (patches), and I hope that this becomes clearer.
Herald tiomela
http://htendam.WordPress.com -
Region - Sorting Varchar2 as report number
I have reports that contain a column with several decimal places in it. Due to several decimal places I use Varchar2 data type.
The problem is that users want to sort on this column and its sort numbers as characters. I don't think I can use the "order by lpad" solution that I had in a normal query because I got an error when I tried
Example of how APEX sorts - HIGH to LOW
3.0.1
14.0.0
13.9.9
How do I sort - HIGH to LOW
14.0.0
13.9.9
3.0.1
Has anyone experience this problem?I'm not sure what is the problem, but try this, add another ip_address column (with alias IP_ADDRESS2) and make a hidden column and do the IP_ADDRESS_SORT to use #IP_ADDRESS2 # instead of #IP_ADDRESS #...
Sam
-
Sort numerically a varchar2 container field '-' and tanks
I have a Field1 varchar2 (20) that contains values such as
2-14', 3-9', '1-1-2 T'
They should be sorted in the order
' 1 1-2 T '
2-14'
3-9'
I tried to use lpad (field1, 22, '0') but he left buffers at a given fixed length for the whole area and it does not work.
There is no fixed grammar for this field and it is possible that in the future some unexpected tank can appear in the field. For example, "[1-2]" "
But the query needs to work even if the sort is not correct.
Can someone please help?select field1,field2,field3...from table tab1, tab2...where ... order by field1;
Thank you.Hello
Sorry, I don't understand the sorting rules.
It looks like
(a) the string is divided into a number of secondary channels by hyphens.
(b) sorting through secondary channels in the order, i.e. the substring n matter only if there is equality of strings 1 through n - 1.
(c) strings composed entirely of numbers preceded by secondary channels who have other characters
(d) secondary digital channels are classified as numbers, that is, '1' and '01' are the same, but comes from '1' before ' 03 "
(e) other substrings are classified as stringsUnder these rules, the two identical channels ' 1 - 1 - R 'would come after the strings that start with 1-01 - 1-'.
Other than that, the following query is what you asked:WITH cntr AS ( SELECT LEVEL AS n FROM dual CONNECT BY LEVEL <= 1 + ( SELECT MAX ( LENGTH (txt) - LENGTH ( REPLACE ( txt , '-' ) ) ) FROM table_x ) ) , got_sub_txt AS ( SELECT id , txt , n , REGEXP_SUBSTR ( txt , '[^-]+' , 1 , n ) AS sub_txt FROM table_x JOIN cntr ON n <= 1 + LENGTH (txt) - LENGTH (REPLACE (txt, '-')) ) , got_sort_key AS ( SELECT got_sub_txt.* , SYS_CONNECT_BY_PATH ( CASE WHEN REGEXP_LIKE ( sub_txt , '[^0-9]' ) THEN '2' || RPAD (sub_txt, 10) -- Magic Number ELSE '1' || LPAD (sub_txt, 10, '0') -- Magic Number END , '-' ) AS sort_key FROM got_sub_txt WHERE CONNECT_BY_ISLEAF = 1 START WITH n = 1 CONNECT BY n = PRIOR n + 1 AND id = PRIOR id ) SELECT id , txt FROM got_sort_key ORDER BY sort_key;
This requires that each substring will be (at most) 10 characters long. That's what the 'magic number' 10 in got_sort_key of the subquery. You can change this to whole m that you like.
The only limit is the number of subkeys is that they must all fit in a VARCHAR2 4000 characters, so the limit is 4000 / (m + 2) (e.g., 333 when m = 10).For this solution, you need a unique identifier (id in the example above) for each line. You don't need to display this identifier in the SELECT clause.
If txt itself is unique, it can be used as the identifier. -
SQL for sort a column containing digital/characters/alphanumeric characters.
I have a table with the column can contain numbers, characters, and a combination of numbers and characters. Here is an example of the table. In this regard, I would like to sort the table in the following way.
(a) sort first numbers in numerical order and then would sort the others in the order of the characters.
Example: -.
RUN SQL
SQL > select * from temp;
ID
---------------
A
22
46
CC
BBB
46jkb
1
10
100
one
AFTER CORRECTION
SQL > < MAGIC SQL statement >
ID
---------------
1
10
22
46
46jkb
100
one
A
BBB
CC
I tried asciistr, to_char and to_number and many other combinations. However, no luck :-(. Any help will be much appreciated.
SQL statement - to create and insert data in the table-
create table temp (id varchar2 (15));
insert into temporary values ('A');
insert into temporary values ('22');
insert into temporary values ('46');
insert into temporary values ("CC");
insert into temporary values ("BBB");
insert into temporary values ('46jkb');
insert into temporary values ('1');
insert into temporary values ('10');
insert into temporary values ('100');
insert into temporary values ('a');
commit;with t as ( select 'A' str from dual union all select '22' str from dual union all select '46' str from dual union all select 'CC' str from dual union all select 'BBB' str from dual union all select '46jkb' str from dual union all select '1' str from dual union all select '10' str from dual union all select '100' str from dual union all select 'a' str from dual ) select str from t order by lpad(regexp_substr(str,'^\d+'),max(length(regexp_substr(str,'^\d+'))) over(),'0') || upper(regexp_replace(str,'^\d+')) / STR ----- 1 10 22 46 46jkb 100 A a BBB CC 10 rows selected. SQL>
SY.
-
Internal sorting/replacing characters
I have a column of scientific names that looks like this:
Canis lupus
Savin ARGeo (haguel)
Plumeria algers
I want to remove all the brackets and their content, so the finished product looks like this:
Canis lupus
ARGeo savin
Plumeria algers
However, I would first sort the column so that all the scientific names that includes parentheses are grouped. Then I can create a new column and a checkbox to indicate the values that previously contained in parentheses.
So I was just wondering if there is a way to 1) sort columns according to the different values of the special characters contain and 2) remove all parentheses and all that they contain. And I would add that the brackets attach all "haguel." There are a lot of different words between brackets.
Thank you.
You can do without sorting by following this example:
In the column A is the original name
B1 = SIERREUR (FIND ("(", A1), 0 "))
It's shorthand dethrone select cell B1 and type (or copy and paste it here) the formula:
SIERREUR = (FIND ("(", A1), 0 "))
C1 = IFERROR (Find (",") ", A1, start-pos), 0)
D1 = if (AND (C1 > 0 > 0, B1), (A1, B1−1) LEFT & RIGHT (A1, LEN (A1) −C1), A1)
Select cells B1 to D1, copy
Select cells B1 at the end of column D, dough
D column now contains the "corrected" names Select copy of column d.
now select the destination and use the menu item "Edition > paste formula results.
-
Sort by or find the word long in a field of varchar2
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi PL/SQL Release 10.2.0.4.0 - Production CORE 10.2.0.4.0 Production TNS for HPUX: Version 10.2.0.4.0 - Production NLSRTL Version 10.2.0.4.0 - Production
I would like to choose or order by the length of the longer term in the fielddrop table event; create table event (event_title_nm varchar2(1000)); SET DEFINE OFF; Insert into EVENT (EVENT_TITLE_NM) Values ('CREATING A NEW CLASS WORD FOR INDICATOR, - _IN'); Insert into EVENT (EVENT_TITLE_NM) Values ('Unknown Title'); Insert into EVENT (EVENT_TITLE_NM) Values ('MASAL REASON TEXT'); Insert into EVENT (EVENT_TITLE_NM) Values ('Note/Attachment Modifications and Amendments'); Insert into EVENT (EVENT_TITLE_NM) Values ('Installation (replaces change request 66)'); Insert into EVENT (EVENT_TITLE_NM) Values ('Unknown Title'); Insert into EVENT (EVENT_TITLE_NM) Values ('Installation (replaces change request 66)'); Insert into EVENT (EVENT_TITLE_NM) Values ('List Item Identifier Change'); Insert into EVENT (EVENT_TITLE_NM) Values ('USER_ID in WORKFLOW_TASK_ASSIGNMENT'); COMMIT;
Word defined as characters continued.
so my production for the first two returned
because WORKFLOW_TASK_ASSIGNMENT is the largest wordevent_title_nm USER_ID in WORKFLOW_TASK_ASSIGNMENT Note/Attachment Modifications and Amendments
and Note/attachment is the second large.
the reason why I was trying to find thiis is that I display this value in a web page and I need to understand about the longest
continuous word because the css will not end a text in the middle of a Word. so, it ends up pushing my div set below where I want it to go.
Published by: pollywog on October 25, 2011 08:43You can order by a scalar subquery returns the max length:
SQL> select * 2 from event e 3 order by ( 4 select max(length(regexp_substr(e.event_title_nm, '\S+', 1, level))) 5 from dual 6 connect by level <= length(regexp_replace(e.event_title_nm, '\S+'))+1 7 ) desc 8 ; EVENT_TITLE_NM -------------------------------------------------------------------------------- USER_ID in WORKFLOW_TASK_ASSIGNMENT Note/Attachment Modifications and Amendments Installation (replaces change request 66) Installation (replaces change request 66) CREATING A NEW CLASS WORD FOR INDICATOR, - _IN List Item Identifier Change Unknown Title Unknown Title MASAL REASON TEXT 9 rows selected
-
Problem copying 4000 characters of CLOB in VARCHAR2 (4000 TANK)
I'm migrating data from a table where it is stored in a CLOB to another table where I said a column of VARCHAR2(4000 CHAR).
I stated a cursor in the form:
(a) select columna, columnb, substr(clob_column,1,4000) of...
(b) select columna, columnb, dbms_lob.substr(clob_column,4000,1) of...
I then loop and EXTRACT the value of substr in a variable that is declared as VARCHAR2 (4000 TANK)
With the cursor version 'a', using the regular subtr function, I get: ORA-01461: can bind to a LONG value only for insert into a LONG column
With the slider 'b' version, by using the dbms_lob.substr function, I get: ORA-06502: PL/SQL: digital or value error: character string buffer too small
I use Oracle 10.2.0.4.0
I don't know yet what to watch next?Machinery says:
However, the database uses NLS_CHARACTERSET UTF8 and NLS_NCHAR_CHARACTERSET = AL16UTF16, which are multibyte so I was expecting the declared with semantic CHAR VARCHAR2 to be multibyte.
And it is. But in Oracle SQL VARCHAR2, as I already mentioned, must not exceed 4000 bytes. So even if you have declared it as 4000 characters, you'll get an error when the total length in bytes is greater than 4000. And since, according to the character set, a character can be 1 to 4 bytes, Oracle takes the best cases and allows you to declare it as max 4000 characters. How many characters will fit in eseccion is another question.
SY.
-
Sort of a system of numbering both Alpha and numeric characters
I have a database of about 370 products we manufacture. The numbering system is 3 digit design followed by the character alpha code 1-6 letters. For example:
014A6LP (for our company, that means that number 14, while the A6 refers to a particular size and the LP of the design is the type of product).
014FAP (same design, different product type)
014A2LPBX (again, same design, different product type)
I tried to sort my table such as numbers and text. I tried to add custom formats that include leading zeros. I even tried to let go the zeros (even if I really need them). I don't like to work.
The result I want is this:
001A6LP
002A6LP
002FAP
003A2FPBX
003A6LP
003NBF
etc, etc.
what I get instead is this: as you can see it my list starts with 012 instead of 001. As it continues, 100 comes before 010, etc.
I look forward to your ideas and help.
Thank you
Josh
Hi Josh,.
You can paste your table in a post (as opposed to a screenshot)? In this way we can try the actual values you have.
SG
-
Example of
create table test (id primary number, data key blob);
I want to display like
Select code, data of the test;
ID data
1 character 4000
1 character 4000
1 character 4000
.
.
and soon.
Please help me on this
BLOB stores binary data. What is the point of converted them to a varchar2? If the LOB will store the character data, then the appropriate data type would be CLOB. Said that, assuming that you will use CLOB here is the answer to your question.
drop table temp purge;
create table temp
(
integer ID
clob c_lob
);
declare
CLOB var;
Start
DBMS_LOB.CREATETEMPORARY (var, true);
because me in 1.32 loop
DBMS_LOB. WriteAppend (var, 1000, rpad ('x', 1000, 'x'));
end loop;
insert into temp (id, c_lob) values (1, var);
insert into temp (id, c_lob) values (2, var);
commit;
end;
/
Select id
rno
, length (dbms_lob.substr (c_lob, 4000, offset)) c_lob_var_len
, dbms_lob.substr (c_lob, 4000, offset) c_lob_var
de)
Select id
, ((level-1) * 4000) + 1 offset
c_lob
level NWR
temp
connect
by level<=>=>
and prior id = id
and dbms_random.value (prior) is not null
);
Maybe you are looking for
-
Hi, new and first post so I'm not sure if this is the appropriate place, I hope that someone will tell me otherwise. Have computer laptop Pavilion G72 9 months with OS Win 7. I was wondering if I can uninstall HP Support Assistant on it (not really
-
Installed the last PM 6.66.3. When you click on the battery PM icon to start the PM it appears only as another red icon in the field of activity. Impossible to implement... Just found out that the same happends with Rescue and Recovery 2.7.003 Any cl
-
Backup: WARNING PC Tools Simple backup cannot recover EFS encrypted files
And no warnings are given. Also in the manual is not clear! I am diapointed.
-
WHAT DO NEED ME FOR MY SANSA FUZE POWER DOWNLOAD MUSIC
I know that I need the Sansa Media Converter... but I was not able to download from the Site of Sansa because whenever I would try to and after I click Run and everything a small window box would pop up and tell me I didn't have any internet connecti
-
Wireless switch and keyboard suddenly doesn't work for many stop button
I own a lenovo g470, I have problem with my wireless, turn and can not be turn on by using the shortcut key, and my keyboard does not work properly. I just uninstall some games and all of a sudden the button, g, h, esc, arrow and left alt is unusable