how to insert special characters in oracle query - oracle

Bringing data from sql to oracle, I need to generate a query from Oracle where I am sending a query to sql to replace “ in a text field.
So, in sql I want to generate a query in oracle "SELECT REPLACE(TEXT, '“', '"'). It works fine if i run this directly on SQL server. But, since I need to store this query in Oracle first and then read it, oracle converts it to a junk "SELECT REPLACE(TEXT, '<somejunk>, '"'). Does anyone know how to make oracle literally take “. I tried ESCAPE and \ but nothing works

You need to concatenate two single quotes.
SELECT REPLACE( '"" - Double quotes to single quotes ', '""', ''''||'''') from dual;

Related

Oracle SQL developer - What is the easiest way to surround a list of texts with quotes and commas?

Here's an example. I get the list like this below
99X2H19JBF11534
16B4H5FHCA12592
ABE4H5FHAA08646
SE0753207527
PAK810869145
PAK810714143
PAK810547887
PAK810340854
PAK820090918
I then use concatenate in excel (and surround the list with quotes and commas) to run queries like below in Oracle SQL Developer.
select * from admin.repair
where repair_nbr in (
'99X2H19JBF11534',
'16B4H5FHCA12592',
'ABE4H5FHAA08646',
'SE0753207527',
'PAK810869145',
'PAK810714143',
'PAK810547887',
'PAK810340854',
'PAK820090918');
I was wondering if other people have an easier way to surround the list with quotes and commas in Oracle SQL developer.
If you got your list from a query, just drag and drop.
We'll not only quote the strings, we'll separate them with commas for you.
If you just have the text, SQL Developer also support block editing. Enable that on the Edit menu, then simply use down arrow to go through each line/curpos and select then paste the quotes.
Or if you're a fan of regex, the Search and Replace (ctrl+R) panel has a RegEx mode you can toggle on.
Disclaimer: I'm a product manager at Oracle for SQL Developer.
Assuming you got your list using query
select val from your_table order by whatever
change your query to
select 'select * from admin.repair where repair_nbr in ('
|| listagg('''' || val || '''', ',
' order by whatever)
|| ');'
from your_table
The result of auxiliar query is the query you require.
Note: You might get ORA-01489 if the result of concatenation is too long but it is different - yet still solvable - story (look for xmlagg function then).

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.

Export to CSV in Oracle SQL Developer not quoting values

I'm trying to export the results of a query in Oracle SQL Developer. One of my columns is a user-defined type which looks like this when it gets exported:
SDE.ST_GEOMETRY(1,1,2702721.09480406,249404.580511138,2702721.09480406,249404.580511138,NULL,NULL,NULL,NULL,0,0,2272,'oracle.sql.BLOB#615bd3ac')
The problem is that SQL Developer doesn't quote values of this column, even though I've specified left and right enclosures of ", and the commas inside the parentheses break my CSV file.
Is there any way to force SQL Developer to enclose all values on export?
As seen from your problem add extra pipes in beginning and end of the column as like:
select ||'|'||column1||'|'|| from table1;

autogenerated sql code: single backslash as an escape character is failing

I'm querying an oracle 9i database with:
SELECT * FROM table WHERE column LIKE '%' || ‘someText’ || '%' ESCAPE '\';
and it fails with the error "escape character must be character string of length 1" ( ORA-01425 error), while succeeding in an oracle express 10g database.
Making it a double backslash (ESCAPE '\\') solves the problem for the oracle 9i database, but generates instead the same ORA-01425 error for the 10g database.
I cannot edit the SQL since it's auto-generated via Telerik OpenAccess ORM.
The Linq code that leads to the SQL above is:
activity.Name.Contains.("someText")
I would like both databases to handle the ESCAPE '\'... Or instead, have another way of searching table items by their name or description.
Thanks in advance!
Not familiar with Linq but I'm a bit confused about where you're executing the query - are you just pasting the generated code into SQL*Plus running against two databases, where that behaviour can at least be explained?
If you are doing it in SQL*Plus, do a show escape in each environment; I suspect 9i will report escape "\" (hex 5c) while the 10g will report escape off. This might indicate that escape handling has previously been set up in the 9i instance but not in the (presumably more recent) 10g one.
If any of this has turned out to be relevant so far, try doing set escape \ in the 10g session and try the \\ version again. And in 9i try doing escape off and try the single-\ version there. Both should now work.
Assuming you're still with me, the next question is why 9i has that setting; there's probably a login.sql or glogin.sql file that's setting it automatically. You might be able to get that removed, as long as it won't affect anything else, to allow the generated code to run unaltered.
I don't think any of that will be relevant if you're going to be executing the code some other way; not sure if you're just testing and debugging the generated code in SQL*Plus and will eventually execute it elsewhere (lack of knowledge of Linq again), in which case this may be a transitory problem anyway.
I'm also not sure what you're actually escaping anyway...
Try:
SELECT * FROM TABLENAME
WHERE COLUMNNAME LIKE '\%' ESCAPE '\';
Generally ESCAPE symbol in LIKE used for allow search symbols '%' and '_'
you could avoid the backslash issue altogether. Try using the curly braces around the escaped characters instead.
http://download.oracle.com/docs/cd/B10500_01/text.920/a96518/cqspcl.htm
Does it fail for every input or just specific strings? The problem may not be with the query, but with the input. If there is an odd number of backslashes, Oracle may try to escape something that shouldn't need an escape.
For example, this works because it's escaping the '%':
select * from dual where 'test' like '%'||'\'||'%' escape '\';
But this fails because it's trying to escape 'a', which doesn't need escaping:
select * from dual where 'test' like '%'||'\a'||'%' escape '\';
Can you modify the string before it's passed to the function and fix odd backslashes?
In case anyone stops by with the same problem... My issue was that I was dealing with “NVARCHAR2” fields. I received help with this issue in the oracle forums :)
This query: select * from dual where 'dummy' like '%' escape '\';
works on both because the field ‘dummy’ is varchar2. If it were nvarchar2, the part of the query that could (only possibly!) cause problems would be the “escape '\'” part (my oracle 9i wants escape ‘\’, my oracle 10g wants ‘\\’).
To overcome the problem, instead of using the ORM’s autogenerated code, I have written a stored procedure (only when I’m searching for strings), where I handle nvarchar2 fields like this: where TableName.ColumnName like N'%' || ‘someText’ || N'%' escape N'\'
And it’s working fine :)
That doesn’t explain, however, how having the same NVARCHAR2 columns, and the same SQL queries, they were handled differently by the two oracle servers (the 10g express on my local PC and the 9i) – that remains a question. So for anyone running into similar problems, it may be good to know if it’s a nvarchar2 issue (I had no idea it could be a problem), and try working around it.

parameter in sql query :SSRS

I am using oracleclient provider. I was wondering how do I use a parameter in the query.
select * from table A where A.a in ( parameter).
The parameter should be a multivalue parameter.
how do I create a data set?
Simple. Add the parameter to the report and make sure to check it off as multi-valued. Then in the data tab and go in and edit the query click the "..." button to edit the dataset. Under the parameters tab create a mapping parameter so it looks something like this (obviously you will have different names for your parameters):
#ids | =Parameters!ContractorIDS.Value
Then in the query tab use the coorelated sub-query like your example above. I have done this many times with SQL server and there is no reason it should not work with Oracle since SSRS is going to build an ANSI compliant SQL statement which it will pass to Oracle.
where A.myfield in (#ids)
You can't have a variable in list in oracle directly. You can however, break apart a comma seperated list into rows that can be used in your subquery. The string txt can be replaced by any number of values seperated by a comma.
select * from a where a.a in (
SELECT regexp_substr(txt,'[^,]+',1,level)
FROM (SELECT 'hello,world,hi,there' txt -- replace with parameter
FROM DUAL)
CONNECT BY LEVEL <= LENGTH (REGEXP_REPLACE (txt, '[^,]'))+1
)
The query works by first counting the number of commas that are in the text string. It does this by using a reqular expression to remove all non commas and then counts the length of the remainder.
It then uses an Oracle "trick" to return that number + 1 number of rows from the dual table. It then uses the regexp_substr function to pull out each occurence.
Firstly in SSRS with an Oracle OLEDB connection you need to use the colon, not the # symbol e.g. :parameter not #parameter but then you aren't able to do this as a multi-valued parameter, it only accepts single values. Worse, if you are using an ODBC connection you have to use the question mark by itself e.g. ? not #parameter and then the ordering of parameters becomes important, and they also cannot be multi-valued. The only ways you are left with is using an expression to construct a query (join() function for the param) or calling a stored proc.
The stored proc option is best because the SSRS can handle the parameters for stored procs to both SQL Server and Oracle very cleanly, but if that is not an option you can use this expression:
="select column1, column2, a from table A where A.a in (" + Join(Parameters!parameter.Value,", ") + ")"
Or if the parameter values are strings which need apostrophes around them:
="select column1, column2, a from table A where A.a in ('" + Join(Parameters!parameter.Value,"', '") + "')"
When you right-click on the dataset, you can select "dataset properties" and then use the fx button to edit the query as an expression, rather than using the query designer which won't let you edit it as an expression.
This expression method is limited to a maximum limit of about 1000 values but if you have that many this is the wrong way to do it anyway, you'd rather join to a table.
I don't think you can use a parameter in such a situation.
(Unless oracle and the language you're using supports array-type parameters ? )
The parameters in oracle are defined as ":parametername", so in your query you should use something like:
select * from table A where value in (:parametername)
Add the parameter to the paramaters folders in the report and mark the checkbox "Allow multiple values".
As Victor Grimaldo mentioned… below worked for me very fine. As soon as I use the :parameter in my SQL query in SSRS dataset1.. it asked me to enter the values for these parameters, for which I choose already created SSRS parameters.
SELECT * FROM table a WHERE VALUE IN (**:parametername**)
Thanks Victor.

Resources