How to insert regex with special characters like “&,?.#:;” in oracle database? - sql

I have a regex value that I want to insert in oracle database table column but I have some problems that the value doesn't inserted correctly in the database
this is the value that I want to insert :
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)?]', 1);
the result of this insert is :
valid_value
+--------------------------------------+
REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)]
I lose the "?" character can some one help me on this how to insert regex value in a table column.
I start the script using a batch file
set DB_CREATE_ROOT="%~dp0"
set SQL_INIT_CONF_DIR=%DB_CREATE_ROOT%\Scripts\configuration\
for /r %SQL_INIT_CONF_DIR% %%F in (*init.sql) do (
ECHO %DATE% %TIME% "%%F" >> %LOG_NAME%
sqlplus -L Test_APP/welkom#//localhost:1521/xe #"%%F" >> %LOG_NAME% 2>&1
)
thanks in advance

You can use CHR function for each of the special characters if you find difficulties with direct inserts.
Thanks

For the records, none of the characters mentioned have a special meaning inside SQL strings thus they don't need any special treatment:
SQL> SELECT '&,?.#:;' AS are_we_special
2 FROM DUAL;
ARE_WE_
-------
&,?.#:;
(online demo)
... being & the only possible exception (“SET DEFINE OFF” in Oracle Database) and only in the context of SQL*Plus and SQL Developer—in which case you'd be getting a Enter value for xxxxx prompt.
Whatever problem the OP had, CHR() is just a workaround for his specific undisclosed issue.

This may help you
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)'?']', 1);

Try this:
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+) ?]', 1);
See demo:
In SQLPLUS:
SQL> /
COL1
---------------------------------------------
REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+) ?]
Now question is which client you are using. In SQLPLUS, its working as shown above.

Related

SQL Plus : insert into numeric values with single quotes around them gives error

A colleague of mine has created an export with INSERT INTO queries using Oracle SQL Developer. This export has put single quotes around every value, not just VARCHARs.
So let's say we have a simple table containing these columns (among others):
SOME_TEXT VARCHAR2(256 BYTE)
SOME_NUMBER NUMBER(15,2)
The export has an INSERT INTO like this:
Insert into MY_TABLE (SOME_TEXT,SOME_NUMBER) values ('test text','123,45');
You may note two things about the decimal value ('123,45'):
It has single quotes around it
It has a comma instead of a dot (the database of my colleague is Dutch, and mine is English)
The second is easy to fix, I can insert it in Oracle SQL Developer like this:
-- dot as thousand separator, comma as decimal point
alter SESSION set NLS_NUMERIC_CHARACTERS = '.,';
Insert into MY_TABLE (SOME_TEXT,SOME_NUMBER) values ('test text','123,45');
-- All other insert queries
alter SESSION set NLS_NUMERIC_CHARACTERS = ',.';
Which works fine.
However, some of the .sql files are very big, and can't be opened in SQL Developer. So instead I want to use SQL Plus to execute the .sql files containing the insert-statements.
However, even when NLS_NUMERIC_CHARACTERS is changed, I'm currently getting the following error in SQL Plus due to the single quotes around the numeric value:
SQL> #"C:\my\path\test_insert_statement.sql"
values ('test text','123,45')
*
ERROR at line 2:
ORA-01722: invalid number
I see two possible solutions, but I don't know if either is possible:
Is there a way to allow single quotes around numeric values in SQL Plus for Oracle databases?
Or is there a way to export a database without single quotes for numeric values in Oracle SQL Developer (then I can let my colleague generate another .sql file)?
You can use single quotes around a numeric value. The error has occurred due to the decimal point character. All you need to change the decimal point character which can be done, as you have shown, as given below.
SQL> create table tbl1(
SOME_TEXT VARCHAR2(256 BYTE),
SOME_NUMBER NUMBER(15,2)
); 2 3 4
Table created.
SQL> alter SESSION set NLS_NUMERIC_CHARACTERS = ',.';
Session altered.
SQL> Insert into tbl1 (SOME_TEXT,SOME_NUMBER) values ('test text','123,45');
1 row created.
SQL> select * from tbl1;
SOME_TEXT SOME_NUMBER
------------- -----------
test text 123,45

How to insert french number format in oracle SQL table

I am facing the issue to insert the french number in a number field of oracle.I am using SQL Developer IDE. When i insert the number(say 3,4) its says invalid number. Specifically,
I don't want to replace the value 3,4 to 3.4.
I tried with changing the NLS Setting also (using command
Alter session set NLS_NUMERIC_CHARACTERS=',';
If I use this command I am able to insert directly in editor but insert command is not working due to comma, Oracle assume that its another value.
Any help would be appreciated.
You can't and you don't need to.
The number format for SQL literals requires the . for the decimal separator.
In the column itself the decimal separator isn't stored at all. You just need to change the display format of the number. This is ideally done in your application, not on SQL level. But if you require this in the SQL output, use to_char() to format your numbers:
select to_char(your_number_column, '9999D99')
from your_table;
The D in the format mask will be replaced with the decimal separator defined by the current session's NLS settings.
A dot . and , are returned literally:
select to_char(your_number_column, '9999,99')
from your_table;
More details on the to_char() format mask can be found in the manual: https://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements004.htm#BABIGFBA
You application/ide may be performing an implicit conversion of character data into number, which uses the session default nls_numeric_characters.
You can force the insert command to perform an explicit conversion as follows using to_number:
SQL> alter session set nls_language = ENGLISH;
Session altered
SQL> alter session set nls_numeric_characters = ',.';
Session altered
SQL> create table t1 (num_col number);
Table created
SQL> insert into t1 (num_col) values ('3.4');
insert into t1 (num_col) values ('3.4')
ORA-01722: invalid number
SQL> -- this is equivalent to:
SQL> insert into t1 (num_col) values (to_number('12345,678', '999999D999', 'NLS_NUMERIC_CHARACTERS=.,'));
insert into t1 (num_col) values (to_number('12345,678', '999999D999', 'NLS_NUMERIC_CHARACTERS=.,'))
ORA-01722: invalid number
SQL> -- now converting explictly
SQL> insert into t1 (num_col) values (to_number('12.345,678', '999999D999', 'NLS_NUMERIC_CHARACTERS=,.'));
1 row inserted
SQL> insert into t1 (num_col) values (to_number('12345,678', '999999D999', 'NLS_NUMERIC_CHARACTERS=,.'));
1 row inserted
in NLS_NUMERIC_CHARACTERS the first character is the decimal separator and the second one is the thousands grouping marker.
The docs have more info:
https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_2117.htm

Oracle NLS_SORT not working via ALTER SESSION?

Using 'alter session set nls_sort' does not seem to be working for me. I am using SQLPlus v11.2.0.3.0 x64 and trying to apply the simple steps for 'Example 9-10 NLS_SORT Affects Linguistic Sort Order' found in the Oracle documentation at http://docs.oracle.com/cd/E18283_01/appdev.112/e10766/tdddg_globalization.htm#CACJEJIB
CREATE TABLE temp (name VARCHAR2(15));
INSERT INTO temp (name) VALUES ('laguna');
INSERT INTO temp (name) VALUES ('llama');
INSERT INTO temp (name) VALUES ('loco');
SELECT * FROM nls_session_parameters WHERE parameter = 'NLS_SORT';
Result: BINARY
SELECT * FROM temp ORDER BY name;
Result:
NAME
---------------
laguna
llama
loco
ALTER SESSION SET NLS_SORT=SPANISH_M;
SELECT * FROM nls_session_parameters WHERE parameter = 'NLS_SORT';
Result: SPANISH_M
SELECT * FROM temp ORDER BY name;
Results are the same:
NAME
---------------
laguna
llama
loco
According to the doco, the sort order above should have changed but it did not. But, if I apply the NLS_SORT as part of the query itself I get the correct resutls:
SELECT * FROM temp ORDER BY NLSSORT(name, 'NLS_SORT=SPANISH_M');
Result:
NAME
---------------
laguna
loco
llama
What am I missing here? Thx in advance.
This was resolved whe I uninstalled Oracle v11.2.0.3.0 and installed v12.1.0.1.0. Truth be told, I can't rule out that it may have just been a problem with our wrapper/configuration of the v11.2.0.3.0 Oracle installer or Oracle itself. Thx for all the post just the same.
The solution is:
1) Go to the REGISTRY (run regedit)
2) Find HKEY_LOCAL_MACHINE/Software/ORACLE
3) Change NLS_LANG
I changed it to
AMERICAN_AMERICA.AR8MSWIN1256
dont forget that you must change all the NLS_LANG in part 2.
now you can login to your database and test it.

How to do an insert with multiple rows in Informix SQL?

I want to insert multiple rows with a single insert statement.
The following code inserts one row, and works fine:
create temp table mytmptable
(external_id char(10),
int_id integer,
cost_amount decimal(10,2)
) with no log;
insert into mytmptable values
('7662', 232, 297.26);
select * from mytmptable;
I've tried changing the insert to this, but it gives a syntax error:
insert into mytmptable values
('7662', 232, 297.26),
('7662', 232, 297.26);
Is there a way to get it working, or do I need to run many inserts instead?
You could always do something like this:
insert into mytmptable
select *
from (
select '7662', 232, 297.26 from table(set{1})
union all
select '7662', 232, 297.26 from table(set{1})
)
Pretty sure that's standard SQL and would work on Informix (the derived table is necessary for Informix to accept UNION ALL in INSERT .. SELECT statements).
As you found, you can't use multiple lists of values in a single INSERT statement with Informix.
The simplest solution is to use multiple INSERT statements each with a single list of values.
If you're using an API such as ESQL/C and you are concerned about performance, then you can create an INSERT cursor and use that repeatedly. This saves up the inserts until a buffer is full, or you flush or close the cursor:
$ PREPARE p FROM "INSERT INTO mytmptable VALUES(?, ?, ?)";
$ DECLARE c CURSOR FOR p;
$ OPEN c;
while (...there's more data to process...)
{
$PUT c USING :v1, :v2, :v3;
}
$ CLOSE c;
The variables v1, v2, v3 are host variables to hold the string and numbers to be inserted.
(You can optionally use $ FLUSH c; in the loop if you wish.) Because this buffers the values, it is pretty efficient. Of course, you could also simply use $ EXECUTE p USING :v1, :v2, :v3; in the loop; that foregoes the per-row preparation of the statement, too.
If you don't mind writing verbose SQL, you can use the UNION technique suggested by Matt Hamilton, but you will need a FROM clause in each SELECT with Informix. You might specify:
FROM "informix".systables WHERE tabid = 1, or
FROM sysmaster:"informix".sysdual, or
use some other technique to ensure that the SELECT has a FROM clause but only generates one row of data.
In my databases, I have either a table dual with a single row in it, or a synonym dual that is a synonym for sysmaster:"informix".sysdual. You can get away without the "informix". part of those statements if the database is 'normal'; the owner name is crucial if your database is an Informix MODE ANSI database.
In some versions of Infomix you can build a virtual table using the TABLE keyword followed by a value of one of the COLLECTION data types, such as a LIST collection. In your case, use a LIST of values of Unnamed Row type using the ROW(...) constructor syntax.
Creating a TABLE from COLLECTION value
http://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.sqls.doc/ids_sqs_1375.htm
ROW(...) construction syntax, for literals of Unnamed Row data type
http://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.sqlr.doc/ids_sqr_136.htm
Example:
select *
from TABLE(LIST{
ROW('7662', 232, 297.26),
ROW('7662', 232, 297.26)
}) T(external_id, int_id, cost_amount)
into temp mytmptable with no log
In the above, the data types are implied by the value, but when needed you can explicitly cast each value to the desired data type in the row constructor, like so:
ROW('7662'::char(10), 232::integer, 297.26::decimal(10,2))
You can also insert multiple rows by storing the values in an external file and executing the following statement in dbaccess:
LOAD FROM "externalfile" INSERT INTO mytmptable;
However, the values would have to be DELIMITED by a pipe "|" symbol, or whatever you set the DBDELIMITER environment variable to be.
If you're using the pipe delimiter, the data in your external file would look like:
7662|232|297.26|
7663|233|297.27|
...
NOTE that the data in the external file must be properly formatted or able to be converted to successfully be inserted into each mytmptable.column datatype.
Here is a simple solution fro bulk insert with SELECT part solving the rest
INSERT INTO cccmte_pp
( cmte, pref, nro, eje, id_tri, id_cuo, fecha, vto1, vto2, id_tit, id_suj, id_bie, id_gru )
SELECT * FROM TABLE (MULTISET {
row('RC', 4, 10, 2020, 1, 5, MDY(05,20,2020), MDY(05,20,2020),MDY(05,27,2020),101, 1, 96, 1 ),
row('RC', 4, 11, 2020, 1, 5, MDY(05,20,2020), MDY(05,20,2020),MDY(05,27,2020),101, 1, 96, 1 ) })
AS t( cmte, pref, nro, eje, id_tri, id_cuo, fecha, vto1, vto2, id_tit, id_suj, id_bie, id_gru )
;

mysql error 1064 when inserting

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(country,ping,order) VALUES (China,1,1)' at line 1
this is my code
INSERT INTO
(country, ping, order)
VALUES
('China', '1', '1');
You're missing the Table Name. Try:
INSERT INTO MYTABLENAME (country,ping,order) VALUES ('China','1','1');
are ping and order text fields or numeric? if numeric remove the ticks from the 1's
INSERT INTO Tablename (country,ping,order) VALUES ('China',1,1)
could also be reserved word try:
INSERT INTO Tablename (country,`ping`,`order`) VALUES ('China',1,1)
Your insert statement is missing the table name:
INSERT INTO tbl_name (col_name,...) VALUES (expr,...)
you are missing table name. also make sure that those quotes are necessary