How to use LIKE statement in sql plus with multiple wild carded values? - oracle

my question is that currently if i want to query for multiple wildcarded values. I need to do something like this.
select customername from customers where customername like '%smith' or customername like '%potter' or customer name like '%harris' or customername like '%williams';
So I wanna ask from the experts, is there any easier way to do this?
Regards,
Sanjan

Create a table of your 100 names
select customername from customers c inner join customersames cn on(c.customernamename like '%'+cn.searchForname)
Can be a table variable if that helps.

you can use regular expressions
EDIT: You can find plenty of resources online. take http://66.221.222.85/reference/regexp.html for example.
Regular expressions are really powerful but can be very SLOW if applied carelessly. For your case they may not squeeze your syntax much because you need to type those names anyway and that's the bulky part.

Related

filter data which doesn't start with list of letters in oracle

I my query dosn't pull any data for this query in Oracle SQL developer can someone help?
SELECT * FROM Customers
WHERE City NOT LIKE '[bsp]%';
You can either use regular expression (advised to avoid for decent size of data as it tends to be considerably slower than the alternate solution using simple like which can even be Sargable sometimes) or use multiple conditions.
Using multiple conditions:
SELECT * FROM Customers
WHERE City NOT LIKE 'b%'
and city not like 's%'
and city not like 'p%';
or using regexp_like:
select *
from customers
where not regexp_like(city, '^[bsp]');

Freemarker Help: Pass Data to List Directive for Sorting

First time poster and a Freemarker novice. I'm hoping someone can assist on this. I am currently referencing data from a 1-to-many table using the <#data> directive. I want to sort the results in a particular field order, but I think that can only be done by the <#list> directive. Here's what I have so far:
There's a CART_ABANDONMENT table with the following fields:
CUSTOMER_ID_
PRODUCT_ID
PRODUCT_NAME
PRODUCT_PRICE
ABANDONED_DATE
<#data CART_ABANDONMENT as abandonment><br>
<#filter CUST_ID="${CONTACTS_LIST.CUSTOMER_ID_}"><br>
<#fields PRODUCT_ID PRODUCT_NAME PRODUCT_PRICE ABANDONED_DATE><br>
${abandonment.PRODUCT_NAME} ${abandonment.PRODUCT_PRICE}<br>
</#data>
What I want to do is to list all related results (by CUSTOMER_ID_) and sort them by PRODUCT_PRICE, descending.
It may be something simple, but I haven't been able to find the answer.
Any guidance would be appreciated.
Freemarker is powerful tool tool but it's just template engine.
It has the sort directive for list, but it can be applied only to built-in types: String, Number and Date.
You may convert list of you complex type to one or more lists of built-in types and use ?sort in template.
Another way is to pass already sorted(as you want) list to template before processing.
What you want sounds like something that should be solved on the database (SQL) level, especially where you say "list all related results (by CUSTOMER_ID_)". FreeMarker is only the V (for View) in MVC.

Need help translating returned data in Oracle query

Please keep in mind I am building a query in two phases here. The first phase is to get this to work with the existing query which is inefficient.
I am not good with PL/SQL at all, but I am learning slowly here.
I have this as a query:
SELECT LOGONID,FIRSTNAME,LASTNAME,ORGNAME
FROM WCSADMIN.USERREG UR,WCSADMIN.ADDRESS A
WHERE UR.USERS_ID = A.MEMBER_ID
AND A.ADDRESSTYPE IN('S','SB')
AND A.STATUS='P'
AND UR.STATUS='1'
AND (UPPER(LOGONID) LIKE UPPER('%cn=users%')
OR UPPER(LOGONID) LIKE UPPER('%o=Buyer A Organization%'))
AND UPPER(LOGONID) LIKE UPPER('uid=resourcereaper%')
AND rownum < 10; -- limits the rows back
Essentially the LOGONID field holds the LDAP string for logging on. The first characters in the field is uid=username,ou=......
I need to be able to carve that field down to just be "username". I think you can use the translate command, but I am unsure about how to trim off the uid= and everything (including) the first ",". Any insight would be greatly appreciated.
Josh
The translate command isn't the one you want - that does a character-for-character substitution.
You can use a combination of SUBSTR and INSTR to get the username, but the REGEXP_REPLACE is a little cleaner (my opinion of course). This will give you the uid value:
REGEXP_REPLACE(LogonID, '^uid=(.*?),.*$', '\1')
I'd explain the regular expression (and the \1) more, but I think the Oracle docs already do a much better job than I can.
Also, beware of the WHERE ROWNUM < 10. It's sometimes quirky (or at least appears so), and it won't work at all if you ORDER BY in your query. There's more info and a great explanation here. If you run into to trouble with ROWNUM you can fix it by putting ROWNUM into an outer query:
SELECT * FROM (
SELECT <your query>
) WHERE ROWNUM < 10
Something like this:
substr(LOGONID,
instr(LOGONID,'uid=')+4,
instr(LOGONID,',')-instr(LOGONID,'uid=')-4
)
This relies on the fact that there's always 'uid=' and a comma somewhere after it. If it's not the case, you'll need to handle exceptional cases as well. You could also use REGEXP_SUBSTR() if you want to be fancy.

Does hsqldb provide a function similar to listagg?

I am looking for a function (or a group of functions) in HSQLDB that does something similar to Oracle's LISTAGG.
I have this as part of a larger select and would like to keep the syntax as similar as possible in HSQLDB:
SELECT LISTAGG(owner_nm, ', ') WITHIN GROUP (ORDER BY owner_nm)
FROM OWNERSHIP WHERE FK_BIZ_ID = BIZ.BIZ_DATA_ID) AS CURRENT_OWNER
The point of this is that we're trying to use HSQLDB for remote work and Oracle for working on site, prod, etc so I want to change the DDLs as little as possible to achieve that.
Looking at ARRAY_AGG, it doesn't seem like it does anything similar (as far as being able to pull from a separate table like we're doing above with OWNERSHIP). Any suggestions for how I may accomplish this?
group_concat is probably what you are looking for:
http://www.hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_aggregate_funcs
Quote from the manual:
GROUP_CONCAT is a specialised function derived from ARRAY_AGG. This function computes the array in the same way as ARRAY_AGG, removes all the NULL elements, then returns a string that is a concatenation of the elements of the array

LINQ group by question

I started playing around with Linq today and ran into a problem I couldn't find an answer to. I was querying a simple SQL Server database that had some employee records. One of the fields is the full name (cn). I thought it would be interesting to group by the first name by splitting the full name at the first space. I tried
group by person.cn.Split(separators)[0]
but ran into a lengthy runtime exception (looked a lot like a C++ template instantiation error).
Then I tried grouping by a few letters of the first name:
group by person.cn.Substring(0,5)
and that worked fine but is not what I want.
I'm wondering about two things:
Why does the first example not work when it looks so close to the second?
Knowing that behind the scenes it's SQL stuff going on, what's a good way to do this kind of thing efficiently
Thanks,
Andrew
Split has no translation into SQL.
So, how to do this string manipulation without split? Cheat like hell (untested):
string oneSpace = " ";
string fiftySpace = " ";
var query =
from person in db.Persons
let lastname = person.cn.Replace(oneSpace, fiftySpace).SubString(0, 50).Trim()
group person by lastname into g
select new { Key = g.Key, Count = g.Count };
The reason your first attempt didn't work is because LINQ to SQL uses Expression Trees to translate your query into SQL. As a result any code that isn't directly translatable into SQL is an exception - this includes the call to Split.
Thanks guys, I'll try the "Replace" trick to see if that runs. I'm very intrigued by LINQ but now it looks like there's some hidden mysteriousness where you have to know what your LINQ queries translate into before being able to use it effectively.
The core problem is of course that I don't know SQL very well so that's where I'll start.
Edit:
I finally tried the "Replace" today and it works. I even got to sort the grouped results by count so now I have a pareto of name in my company. It's horrendously slow, though. Much faster to select everything and do the bucketing in C# directly.
Thanks again,
Andrew

Resources