How to parse colon within string in Oracle - sql

I wish to insert a record to an Oracle database where one field contains the path to a file, i.e 'C:\\ProjectCodes\\ProjectZ\\ZCode.txt'. But when I attempt an INSERT statement I am asked about binding the value \\ProjectCodes\\ProjectZ\\ZCode.txt.
This is because of the colon I think. Is it possible to parse the colon to insert this value? The target field in the table is declared as a varchar2.
Here is the INSERT statement. I am using SQL Developer. Had to hide the IP address and password etc or I would be in trouble
INSERT INTO SFTP_Creds (SFTP_HOST, SFTP_PORT, SFTP_USERNAME, SFTP_PASSWORD,
SFTP_FINGERPRINT, SFTP_HOSTKEY, SFTP_FILE)
VALUES (‘xxxx.xxxx.xxxx.xxxx’, 22, ‘CORP\\username’, ‘password’, NULL,
NULL, 'C:\\ProjectCodes\\ProjectZ\\ZCode.txt');

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

SQL String or binary data would be truncated. while inserting into table type parameter

I have a scenario where there is a table and i nned to pass table values param inside one of the stored procedure to peform certain actions . Basic table structure is as follows.
CREATE TABLE [dbo].[CitytTax] (
[CountryCode] [int] NOT NULL,
[TaxType] [varchar](255) NULL
)
As you see, Taxtype column is varchar type and takes upto 255 chars. However I will create on table type as below in part of application code and pass this table type as param to one of sp.
DECLARE #TaxDetails as [CitytTax]
Now i will insert some dummy values into it and pass this table type to one of the SP.
INSERT INTO #TaxDetails ([CountryCode],TaxType )
VALUES (6047,'Codfggtuioanoio charge to fund the liquidation of insurancevalues')
but getting error as below :
String or binary data would be truncated
The question here is table value type is having a column which is similar to actual database table. SO when i insert above script, it fails. However if i insert any value to [taxtype] which is less than 50 characters, it will insert successfully. but fails for more than 50 chars. IM wondering why it fails,it is supposed to take upto 255 characters right??
If your data length is indeed shorter than the field length. Then you're having having another table filled by a trigger on the main table, where the column size also had to be changed.
Also, replace all single quotes of your insert into query to double quotes and pass it into the stored procedure.
Well I figured out something which is a quick fix: just added below line before insert query:
SET ANSI_WARNINGS OFF .

How to insert into type text

I wish to insert into an SQL table in a field whose data type is text. However I am informed of an error saying ' check datatype' my Name field is of type nvarchar and my job field is of type text.
INSERT INTO Table1 (Name, Job) VALUES ('John', 'Clerk')
In MS SQL Server, you wont be able to insert string values(with more than 1 characters) in table if the column of type nvarchar. You can only insert only one character using nvarchar.
If you wish to insert some text, please specify the some size with nvarchar.
For example in your case:
Create table Table1(Name nvarchar(5), Job Text)
Insert into Table1(Name, Job) values ('John','Clerk')
This will work.
Hope it will help you out.

Insert empty string into INT column for SQL Server

A SAMPLE table has only one column ID of type int, default null.
In Oracle when I do:
insert into SAMPLE (ID) values ('');
the new record is added with blank value. But in SQL Server 2008, when I run the same insert statement, the new record has the value of 0.
Is there a way to force SQL Server 2008 to default blank string to NULL instead of 0 (for numerical type of columns)?
Assuming that your INSERT statement is part of a stored procedure re-used in many places of your application (or, perhaps, is a batch always constructed by the same part of the client code) and that the inserted value is a number passed as a string argument, you could modify the INSERT like this:
INSERT INTO SAMPLE (ID) VALUES (NULLIF(#argument, ''));
Use NULL instead.
insert into SAMPLE (ID) values (NULL);
How about another idea - define an INSTEAD OF INSERT Trigger.
Despite the fact that you're trying to insert a string, with this the operation is "intercepted", empty string is replaced by NULL, and the insert succeeds.
If you define this trigger on your table, then you can continue to insert empty string as before, with no other changes.
Edit: As Martin Smith points out, this effectively is a comparison to 0 (the equivalent of empty string as an int) meaning you won't be able to store 0 in this table. I leave this answer here in case that's acceptable to your situation - either that or re-do all your queries!
CREATE TRIGGER EmptyStringTrigger
ON [SAMPLE]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO [SAMPLE](ID)
SELECT CASE
WHEN ID = '' THEN NULL
ELSE ID
END
FROM inserted
END
SQL Fiddle example
You can't insert a 'string' into a int column. Oracle must be just handling that for you.
Just try inserting NULL if that's what you need.
insert into SAMPLE (ID) values (NULL);
One more option
insert into SAMPLE (ID) values (DEFAULT)

How can I INSERT data in TEXT datatype field? (Informix)

CREATE TABLE updater
(
nzp_up SERIAL PRIMARY KEY,
version VARCHAR(50),
status INT,
report TEXT
);
INSERT INTO updater (version, status,report) values ('TestVersion' , 0,"123123123");
-617 SQL error: A blob data type must be supplied within this context.
Using a | (pipe) delimited file, you can use the LOAD command to insert values into blob & text data types. I had the same problem in the past - go to link in my comment
See my question: Consistent method of inserting TEXT column to Informix database using JDBC and ODBC
It seems that some tools like ODBC drivers can insert text as TEXT while others like JDBC drivers must use PreparedStatent or other techniques.
INSERT INTO updater (version, status,report)
values ('TestVersion' , 0,"123123123");
and
INSERT INTO updater (version, status,report)
values ('TestVersion' , 0,'123123123');
have the same effect in mySql.So lets try without double quotes in SQL.