Replace comma with double quotes enclosed comma for all character fields? - oracle

In my schema, for a definite set of tables, if any character field contains a comma it must be enclosed in double quotes.How can I achieve this for all the character fields of that set of tables in one go.I am using Oracle 11g?

Not sure if I'm following the question correctly, but maybe this will give you what you need.
Create our example table:
create table foobar (foo varchar2(30));
Populate it with test values:
insert into foobar values ('There is no comma.');
insert into foobar values ('There is, a comma.');
This query will wrap comma-containing values in double-quotes:
select decode(instr(foo,','),0,foo,'"'||foo||'"') from foobar;
And here's the output:
"There is, a comma."
There is no comma.
Here's how it works. The INSTR tests for the presence of a comma in the column. If there is no comma (INSTR returns 0) the DECODE returns the column as is. In all other circumstances (i.e. a comma exists) we wrap the output in double-quotes.
Repeat for each table/column.
It would be simple to use the syntax in an update statement, if that's what you're trying to achieve. So,
update foobar
set foo=decode(instr(foo,','),0,foo,'"'||foo||'"');

Related

How to avoid " " in select statement in column name for column that has numberAlphabet pattern in Oracle?

I have doubts regarding double quote column name in Oracle. I tried creating column name in number_alphabets pattern but this won't work. Then I used double quote and I was able to create table with this column name. When I do select, column name comes within double quote.
I have attached script in here.
CREATE TABLE test
(
"100_title" VARCHAR2(200) NULL
)
SELECT * FROM test
When I do select, in result set, column name will be "100_title" but I do not want "" in it. Is there a way to fix this?
From the Database Object Names and Qualifiers documentation:
Nonquoted identifiers cannot be Oracle Database reserved words. Quoted identifiers can be reserved words, although this is not recommended.
and
Nonquoted identifiers must begin with an alphabetic character from your database character set. Quoted identifiers can begin with any character.
Nonquoted identifiers can only contain alphanumeric characters from your
database character set and the underscore (_). Database links can contain
periods (.) and "at" signs (#).
Quoted identifiers can contain any characters and punctuations marks as well
as spaces. However, neither quoted nor nonquoted identifiers can contain
double quotation marks or the null character (\0).
So your question:
When I do select, in result set, column name will be "100_title" but I do not want "" in it. Is there a way to fix this?
The column identifier 100_title starts with a non-alphabetic character so by point 6 of that documentation you must use double quotes with the identifier.
How the column name displays depends on the user interface you are using. On db<>fiddle, the column name is displayed without quotes and this will be the same with many other interfaces.
If the user interface you are using only outputs the identifier with surrounding quotes then you could change the identifier from "100_title" to title_100 as this starts with an alphabetic character and contains only alpha-numeric and underscore characters and, thus, does not need to be quoted.
The short version is "no; pick a name that starts with a letter"
If you use a name that starts with a number you'll have to use " every time you mention the column name, and you'll have to get the case right. Your column is called "100_title", not "100_Title" or "100_TITLE"
Call it title_100, then you can refer to it as any case, even TiTLe_100 if you like, and generally your life will be easier

When I use a db2 insert statement it only runs if I use single quotes, but I don't want single quotes in the value that is inserted into the table

I am trying to insert values into a table on a db2 db, and its inputting single quotes.. argggh
So I am able to insert values using
insert into table abc.house (house_name, is_active) values ('Treasure', 1);
however when selecting the value in the table is 'Treasure' which I don't want those lovely quotes.
If I try to use:
insert into table abc.house (house_name, is_active values (Treasure, 1);
I get an error
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=TREASURE, DRIVER=4
Any solutions? Thanks, JT
so i learned that the sql UI that was set up, was done so that for Varchar values single quotes are part of the return from a query. The UI shows 'Treasure', whereas if I query on the command line the return is simply Treasure
Good to go. using insert statement with single quotes around the value is good syntax.
That's correct. We MUST put single quotes across char/varchar/blob data.

ERROR at line 1: ORA-00984: column not allowed here

create table student(id int,name varchar2(20),marks int);
insert into student(id,name,marks) values(545,"wiahr",100);
or
insert into student values(545,"wiahr",100);
For the above two insertions, I am getting the following error
insert into student(id,name,marks) values(545,"wiahr",100)
* ERROR at line 1: ORA-00984: column not allowed here
can you please help with this.
Per the error message, you're actually using an Oracle database, and with Oracle databases strings should be encapsulated by single quotes:
insert into student(id,name,marks) values(545,'wiahr',100);
(live demo)
Double quotes instead name fields (and fields are invalid in the VALUES clause of an INSERT statement).
It's specifically called out in the INSERT documentation:
Character and date literals in the VALUES list must be enclosed by single quotes ('). Numeric literals are not enclosed by quotes.
This is in contrast to MySQL, with which you can use either single- or double-quotes for strings, and backticks introduce fields.
String literals are denoted by single quotes ('), not double quotes ("):
insert into student (id, name, marks) values (545, 'wiahr', 100);
-- Here -------------------------------------------^-----^

Delphi Adoquery SQL add or text

I'm trying to update my database in Delphi, but I'm not getting it right.
What I want is simple. This is my code:
form1.ADOQuery1.SQL.Clear;
form1.ADOQuery1.SQL.Add('Update Table1 set mark=' +Form1.Edit4.Text);
form1.ADOQuery1.ExecSQL;
So basically, what I want is the Text written in the Edit to go into my database with the UPDATE function, where my database table is table1 and the field is named mark.
There is not enough information in your question to provide a definitive answer. However, I can make an estimated guess.
What you have shown would only work successfully if mark is defined as an ordinal or boolean field, and the user is entering appropriate numeric/boolean values into the TEdit.
But, if the mark field is defined as a textual field instead, you need to wrap the Text value in quote characters, otherwise you will produce invalid SQL syntax.
Imagine you entered a Text value of 'hello world'. Your original SQL statement would end up being the following, which is invalid syntax:
Update Table1 set mark=hello world
You need to wrap text values in quote characters instead:
Update Table1 set mark='hello world'
Or:
Update Table1 set mark="hello world"
For example:
form1.ADOQuery1.SQL.Add('Update Table1 set mark=' + QuotedStr(Form1.Edit4.Text));
Or:
form1.ADOQuery1.SQL.Add('Update Table1 set mark=' + AnsiQuotedStr(Form1.Edit4.Text, #34));
It is important to use a function like (Ansi)QuotedStr() to avoid SQL injection attacks. This is done by ensuring any embedded quote characters in the input text are escaped property. Otherwise, if you just did something like this instead:
form1.ADOQuery1.SQL.Add('Update Table1 set mark="' + Form1.Edit4.Text + '"');
The user could enter a text value like '"; <arbitrary SQL here>' and really reek havoc with your database.
The safer approach is to use a parameterized query instead, and let ADO handle any necessary SQL formatting for you (make sure TADOQuery.ParamCheck is true):
form1.ADOQuery1.SQL.Clear;
form1.ADOQuery1.SQL.Add('Update Table1 set mark=:Mark');
form1.ADOQuery1.Parameters.ParamByName('Mark').Value := Form1.Edit4.Text;
form1.ADOQuery1.ExecSQL;

sqlldr WHEN clause

I am trying to code a sqlldr.ctl file WHEN Clause to limit the records imported to those matching a portion of the current Schema's name.
The code I have (which does NOT work) is:
LOAD DATA
TRUNCATE INTO TABLE TMP_PRIM_ACCTS
when REGION_NUM = substr(user,-3,3)
Fields terminated by "|" Optionally enclosed by '"'
Trailing NULLCOLS
( PORTFOLIO_ACCT,
PRIMARY_ACCT_ID NULLIF (PRIMARY_ASSET_ID="NULL"),
REGION_NUM NULLIF (PARTITION_NUM="NULL")
)
sqlldr returns:
SQL*Loader-350: Syntax error at line 3.
Expecting quoted string or hex identifier, found "substr".
when PARTITION_NUM = substr(user,-3,3)
I cannot put single quotes around "user", because that turns it into the literal string "user". Can anyone explain how I can reference the "active" User in this WHEN Clause?
Thank you!
Can you try something like this? (now I can't make test with SQLLDR, but this is syntax I used for changing values):
when REGION_NUM = "substr(:user,-3,3)"
It doesn't look like you can. The documentation only shows fixed values:
Trying to use an expression in when that clause (or in nullif; thought I'd try to see if you could cause a rejection based on null PK value) you just see the literal value in the log:
Table TMP_PRIM_ACCTS, loaded when REGION_NUM = 0X73756273747228757365722c2d332c3329(character 'substr(user,-3,3)')
which is sort of what you referred when you said you couldn't quote user, but you'd have to quite the whole thing anyway. Using :user doesn't work either, the colon is seen as just another character, it doesn't try to find a column called user instead.
The simplest approach may be to pre-process the data file and remove any rows which don't match the pattern (e.g. via a regex). That would actually be slightly easier if you used an external table instead of SQL*Loader.
Alternatively, generate your control file and embed the correct literal value based on the user you'll connect as.

Resources