string comparing query with chinese chars - Oracle Database - sql

I'm having trouble executing a simple query such as the following
select * from table_name where variabe_name like '在职'
the problem is the chinese chars. Those chars are in the table (I just copied them after doing a select * from table, so the displaying of the chinese chars works just fine) but it doesn't seem to work. When it execute the query, it returns 0 rows.
I' ve also tried
select * from table_name where variabe_name like '%在职%'
and
select * from table_name where variabe_name = '在职'
But that doesn't work either.
Any clue of what the problem might be?
Thnaks a lot

==> Found solution: put 'N' before the chinese characters, so that they are interpreted as Unicode. Like: where field like N'罐'

SQL> create table mytbl (data_col varchar2(200));
Table created
SQL> insert into mytbl values('在职');
1 row inserted.
SQL> commit;
Commit complete.
SQL> select * from mytbl where data_col like '%在职%';
DATA_COL
-----------
在职
SQL> SELECT * FROM nls_database_parameters where parameter='NLS_CHARACTERSET';
PARAMETER VALUE
------------------------------ ----------------------------------------
NLS_CHARACTERSET AL32UTF8
Your NLS_CHARACTERSET should be set to AL32UTF8. So try
SQL> ALTER SESSION SET NLS_CHARACTERSET = 'AL32UTF8';
Also make sure that parameter NLS_NCHAR_CHARACTERSET is set to UTF8.
SQL> ALTER SESSION SET NLS_NCHAR_CHARACTERSET = 'UTF8';

Related

not able to export table into csv format

I am trying to export table into csv format as below:
SQL> desc test;
Name Null? Type
----------------------------------------- -------- ----------------------------
DN NUMBER(10)
DISCONNECT_DATE DATE
SQL> select DN ,DISCONNECT_DATE from test into OUTFILE '/tmp/data.csv';
select DN ,DISCONNECT_DATE from test into OUTFILE '/tmp/data.csv'
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
could you please anyone help me to resolved above problem.
I got the answer if we are using sql plus then we need use spool to get data into csv format. Below is the steps ...(don't forget to spool off after execution of query. )
SQL> set colsep ,
SQL> set headsep off
SQL> set pagesize 0
SQL> set trimspool on
SQL> spool /tmp/data.csv
SQL> select * from test;
-----------------(we cant placed the data)
10 rows selected.
SQL> spool off

Number formatting in SQL

I have a number that needs to be formatted like this:
Thousands need to be separated with .
Decimals need to be separated with ,
For example, number 1,234,567.89 needs to look like 1.234.567,89.
Is there any way that I can do this with a simple sql function or I have to make my own function?
Use to_char() together with the specification that you want to use , as the decimal separator and . for the thousands separator (which is not the default in Oracle)
select to_char(1234567.89, '9G999G999G999D00', 'NLS_NUMERIC_CHARACTERS = '',.''')
from dual;
Results in: 1.234.567,89
Details about format models: http://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements004.htm#SQLRF00211
Details about the to_char() function: http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions201.htm#SQLRF51882
You can alternatively also work with the session territory setting.
create table mytest (field1 number);
insert into mytest values (1234567.89);
alter session set NLS_TERRITORY=GERMANY;
select field1, to_char(field1,'9G999G999G999D00') from mytest;
alter session set NLS_TERRITORY=AMERICA;
select field1, to_char(field1,'9G999G999G999D00') from mytest;
Output:
Table created.
1 row created.
Session altered.
FIELD1 TO_CHAR(FIELD1,'9G999G999G999D00')
---------- ----------------------------------
1234567,89 1.234.567,89
1 row selected.
Session altered.
FIELD1 TO_CHAR(FIELD1,'9G999G999G999D00')
---------- ----------------------------------
1234567.89 1,234,567.89
1 row selected.

Error to define a long column in oracle

I have a problem with a simple script in sql:
SQL> alter table myTable modify myColumn default 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
SP2-0027: Input is too long (> 2499 characters) - line ignored
Split the 4000-character long string literal into two parts after 2000 characters and concatenate the two strings together:
SQL> alter table myTable modify myColumn default 'XXXXXX...........XXXXXX' ||
2 'XXXXXX.............XXXXXX';
(I've omitted most of the Xs here for clarity.)
However, if you genuinely have 4000 Xs , it's simpler to write
SQL> alter table myTable modify myColumn default rpad('X', 4000, 'X');

Invalid Sequence name in Oracle db

Recently , I am facing an issue with sequence in Oracle.
alter sequence seq_name increment by 100
will give me an error "Invalid sequence name"
However, if I changed it to
alter sequence "seq_name" increment by 100
It will work perfectly fine. Anyone is able to explain the rational behind this?
Thanks
Sebastian
ps. I am using rails with oci8 to create my oracle tables.
Your sequence was created with case-sensitive name (using quatation marks), so you can refer to it only with strict name - in quotation marks. If you want to refer to it without such problems just create sequence not using quotation marks. Examples below (with table name):
SQL> create table "t1"(c int);
Table created.
SQL> select * from t1;
select * from t1
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from "t1";
no rows selected
SQL> select * from "T1";
select * from "T1"
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> create table t2(c int);
Table created.
SQL> select * from t2;
no rows selected
SQL> select * from T2;
no rows selected
SQL> select * from "t2";
select * from "t2"
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from "T2"; -- name without quatation marks is uppercase by default
no rows selected
Sequence was created as lowercase. Like this:
CREATE SEQUENCE "seq_name" MINVALUE 1 MAXVALUE 999999999999999 INCREMENT BY 2 START WITH 1 CACHE 20 NOCYCLE NOKEEP NOSCALE GLOBAL ;
Example:
select * from user_sequences where sequence_name='SEQ_NAME'; --No rows selected
select * from user_sequences where sequence_name='seq_name'; -- 1 row
If you create with name : "SEQ_name".
select * from user_sequences where sequence_name='SEQ_name'; -- 1 row
Because,
when you create an object (with object name enclosed with double quotes) it will store/create as it is. Without double quotes, it would be uppercase.

Is it possible to update data inside a CLOB using SQL?

I have a table having one clob column which has XML data in it.
Say I want to replace XYZ with ABC in the clob column.
Is it possible using sqlplus?
Why not try it ?
SQL> create table nnn(c1 clob);
Table created.
SQL> insert into nnn values ('text ABC end');
1 row created.
SQL> select * from nnn;
C1
-------------------------------------------------
text ABC end
SQL> update nnn set c1=replace(c1,'ABC','XYZ');
1 row updated.
SQL> select * from nnn;
C1
-------------------------------------------------
text XYZ end
SQL>
"i have new line in the column. any
advice?"
Newlines are characters; if you want to amend text which contains them you need to include them in the search string. You can do this using the CHR() which takes an ASCII value as an argument. The precise codes you need to include vary according to OS. Because I ran this example on MS Windows I needed to pass both linefeed (ASCII=10) and carriage return (ASCII=13).
SQL> select * from t42
2 /
TXT
--------------------------------------------------------------------------------
<ABC> ABCD
</ABC>
SQL> update t42 set txt=replace(txt,'ABCD'||chr(10)||chr(13), 'APC woz here')
2 /
1 row updated.
SQL> select * from t42
2 /
TXT
--------------------------------------------------------------------------------
<ABC> APC woz here </ABC>
SQL>
Incidentally, if you are storing XML text it might be worthwhile using the XMLType datatype for the column instead of CLOB. It comes with a lot of useful functionality.
Yes, it's possible with one REPLACE() function. Try:
update nnn set c1 = REPLACE(c1,'ABC>','XYZ>')