SELECT Roughly equals - oracle

I am transfering from access to oracle and couldn't find a roughly equals function in oracle. Does such a thing exist?
I am referring to something like:
SELECT * FROM Table WHERE name ='*nswer is thi*';

Use LIKE:
SELECT * FROM Table WHERE name LIKE '%nswer is thi%';
Explanation:
This query will select records which contains "nswer is thi" anywhere in the field name.
For example:
name
--------------------
answer is this
blahnswer is thiblah
The LIKE conditions specify a test involving pattern matching. Whereas the equality operator (=) exactly matches one character value to another
Read more here.

Related

How to pass more than one string in bind variable in a Oracle Report formula column? [duplicate]

This question already has answers here:
how to convert csv to table in oracle
(5 answers)
Closed 4 years ago.
We’ve got a page that displays a list of records, and the user is allowed to check the desired lines, and then get a report printed of those selected records. I’m trying to define the Oracle Report to have a single parameter which would be a comma-delimited list of the IDs to be printed. However it is not working. The SQL is something like this:
Select * from CEP_TABLE where Table_ID in (:P_IDLIST)
If I define the parameter to be numeric, I get an “invalid parameter input” error when trying to give it 654,655 – it doesn’t like having the comma.
If I define the parameter to be character, it accepts the parameter but then the database gives an “invalid number” error. It appears it is substituting the bind variable with the entire parameter in quotes – e.g.
Select * from CEP_TABLE where Table_ID in (‘654,655’)
But I want it without the quotes:
Select * from CEP_TABLE where Table_ID in (654,655)
I can get the report to work if I define multiple parameters – e.g. Table_ID in (:P1,:P2,:P3,:P4) but they could have a hundred items they want to include so it seems crazy to define 100 parameters – and really… I don’t want any limit.
Has anyone tackled this problem before? – it seems like it would be pretty common. I could have the page write out the selected ids to some temporary table and then define the query to join to that table, but that also seems excessive.
There's a hero that comes to rescue, and his name is lexical parameter.
You said that your query looks like this:
select *
from CEP_TABLE
where Table_ID in (:P_IDLIST)
Report already contains parameter named p_idlist. Now create another one, manually; let's call it lex_idlist. Its datatype should be character, its length somewhat larger than p_idlist parameter's (for example, if p_idlist is character(50), make lex_idlist character(70)).
Then rewrite query as follows:
select *
from cep_table
&lex_idlist
Go to After Parameter Form trigger which should look like this:
function AfterPForm return boolean is
begin
:lex_idlist := 'where table_id in (' || :p_idlist || ')';
return (TRUE);
end;
That's all - compile the report and run it. Enter some values into p_idlist parameter (such as 654,655). The trigger will dynamically create the WHERE clause, store it into the lexical parameter which will then "act" as if it was a real WHERE clause.
Read more about lexical parameters in online Reports Help system.
This is a very common and non-trivial question.
You need to split a single input string into its component tokens. Since I don't have your table, I illustrate how it's done on the emp table in the standard scott schema; here is how you can get the rows for the list of department numbers given in a single comma-separated string, such as '10,30':
select * from scott.emp
where deptno in
(select to_number(regexp_substr(:p_deptno, '\d+', 1, level)) from dual
connect by level <= regexp_count(:p_deptno, '\d+')
)
;
In fairness, there are also methods that don't split the string, and instead play silly games with string comparisons; but those will be quite ineffective - for example they would not allow you to use an index you may have on table_id (or on deptno in my example).

Oracle - Can't use * sign with other column in select clause

Sorry if it's trivial, but selecting column with * sign isn't working always, and I don't find reference to this behavior.
I can select table A and column col with the following statements:
select * from A; and select col from A; and select aa.col,aa.* from A aa;
But I can't view it together:
select *,col from A;
Will result in error ORA-00923: FROM keyword not found
select col,* from A aa;
Will result in error ORA-00936: missing expression
Why i must use the alias for * sign ?
select col,aa.* from A aa;
Why are the errors so misleading?
The restriction is not so clearly stated in documentation, but you can find it by following this diagram.
Here you see that if you use the *, you can't use anything else in select list
The syntax diagram for select shows:
The outermost path of that shows the plain, unprefixed * all-column wildcard on its own, and there is no loop back around for additional column expressions - all paths with a comma (to separate terms) are distinct from that plain * path.
On the inner path that does allow a comma, and thus multiple expressions, you can only use .* prefixed by a table/view/alias, and can then follow (or precede) that with other expressions.
(I really thought that was stated more clearly somewhere, but I can't find it anywhere in recent documentation...)
Why i must use the alias for * sign ?
select col,aa.* from A aa;
That isn't quite accurate; you don't have to use an alias, you can use the table name directly if it isn't aliased, so this is also valid:
select col,A.* from A;
There is a school of thought that you shouldn't use a wildcard anyway, at least for anything except an ad hoc query - it's better to list all of the required column name explicitly, prefixed with the appropriate table name/alias particularly if there is a join, for clarity and to avoid unexpected issues with tables being modified. That's rather outside the scope of this question though *8-)
Why are the errors so misleading?
The errors are correct. They just don't guess what you are trying to do. After
select *
the next keyword should be from, so anything else gives
FROM keyword not found where expected
After , there should be a valid expression such as a column name, not * which is unexpected, so you get
ORA-00936: missing expression
Perhaps it would be nice if Oracle wrote a special error message about the incorrect use of *, but so far they have not. You could propose it on the Oracle Database Ideas forum.

Filter option using regular expression

I have requirement for the filter the names in the table.
Example Table Name :- student
Columns :- Name, Class.
The date is like
Name ----Class
HIGHSPEED ---C
HIGHSPEED11 ---C1
HIGHSPEED22 ---C2
NORMAL-------------N
NORMAL1-------------N1
NORMAL2-------------N2
Like this i have data SRQ, PWE ...
My requirement is I need to remove some data using a name...
SELECT distinct name FROM student WHERE UPPER (name) LIKE 'SRP%'
Or UPPER (name) LIKE 'HIGHSPEED%'
Or UPPER (name) LIKE 'SRQ%'
Or UPPER (name) LIKE 'PWE%'
Or UPPER (name) LIKE 'SPINTERFACE%'
Or UPPER (name) LIKE 'SRM%'
Instead of using OR condition, please let me know any other option... ?
I'm not an Oracle user, but a quick look through their docs indicates Oracle supports POSIX Extended Regular Expressions (ERE). So a regular expression that matches the names in your query would be:
^(SRP|HIGHSPEED|SRQ|PWE|SPINTERFACE|SRM)
It looks like Oracle supplies a REGEXP_LIKE function, so your query could be re-written using the regular expression as:
SELECT distinct name FROM student WHERE REGEXP_LIKE(UPPER(name), '^(SRP|HIGHSPEED|SRQ|PWE|SPINTERFACE|SRM)');
I'm afraid I don't have access to Oracle to test but that's what the regular expression will look like.

how does oracle contain query work?

I have this query:
select id from mytable where contains(all_text,'('||?||' within name)*2,
('||?||' within description)',1)>0
How does the contains in the where clause work?
Thanks,
If the first parameter occurs in the name section, then its weight is twice the weight of the second parameter occurring in the description section.
This "contains" operator will set the score variable. Without it doubling the weight has no meaning for ">0" condition. However,
SELECT id FROM mytable WHERE CONTAINS(all_text,'('||?||' WITHIN name)*2,
('||?||' WITHIN description)',1)>0 ORDER BY SCORE(1) DESC
would totally make sense and will order id's first by those rows where search term is found in the name section.
Here is a useful reference, just in case: http://docs.oracle.com/cd/B19306_01/text.102/b14218/cqoper.htm

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