REGEXP_LIKE QUERY IN ORACLE DB - regexp-like

I need to currently match 00000012345 and 12345 in my DB search query.
I am currently using following query:
SELECT *
FROM BPP.CHECK_REGISTER
WHERE CHECK_NO like CONCAT('%',:checkNum)
for searching but here % can mean any character other than 0 hence I have replaced this query with following:
SELECT *
FROM BPP.CHECK_REGISTER
WHERE REGEXP_LIKE (CHECK_NO,'^(0*12345)$')
but in this query I don't want to mention 12345 but mention it as a user entered parameter like the first query :checkNum
How do I rephrase the REGEXP_LIKE condition with only 2 arguments with user input :checkNum as oracle db allows only a maximum of 2 arguments. (another problem)

You can concatenate the parameter:
SELECT *
FROM BPP.CHECK_REGISTER
WHERE REGEXP_LIKE (CHECK_NO,'^(0*'||:checkNum||')$');
Alternatively add the regex part to the user entered value (in your application code) before passing it to the query.

Related

format issue in scala, while having wildcards characters

I have a sql query suppose
sqlQuery="select * from %s_table where event like '%holi%'"
listCity=["Bangalore","Mumbai"]
for (city<- listCity){
print(s.format(city))
}
Expected output:
select * from Bangalore_table where event like '%holi%'
select * from Mumbai_table where event like '%holi%'
Actual output:
unknown format conversion exception: Conversion='%h'
Can anyone let me how to solve this, instead of holi it could be anything iam looking for a generic solution in scala.
If you want the character % in a formatting string you need to escape it by repeating it:
sqlQuery = "select * from %s_table where event like '%%holi%%'"
More generally I would not recommend using raw SQL. Instead, use a library to access the database. I use Slick but there are a number to choose from.
Also, having different tables named for different cities is really poor database design and will cause endless problems. Create a single table with an indexed city column and use WHERE to select one or more cities for inclusion in the query.

Fetch record based on string that is present in a field

I'm using the following query to fetch records from database. The row that exists in database and the column question contains: "How are we going to do solve this. Is it complex?"
I'm using the following query to fetch the data.
SELECT * FROM TRENDING WHERE question ILIKE '%how are we%' ;
The issue is that, the above query works only if those three words are present in database. If I use additional word as following, the query does not work. For instance, the following query does not work
SELECT * FROM TRENDING WHERE question ILIKE '%how are we doing %' ;
How can I sort this out? I want it to fetch any record that might contain these words that are being queried?

TSQL / SSRS : Using LIKE with multi-valued parameters

I am trying to determine a solution to filter records using LIKE with a multi-valued parameter. In a simplistic example a user wants to return a set of 5-digit Object codes by entering the following in a parameter window in a SSRS report:
#parm_Object
1,24,333,34567
This ideally would return Object codes satisfying the following criteria:
1 : All Object codes starting with '1'
24: All Object codes starting with '24'
333: Similar
34567: Object code '34567'
I guess a starting point for me would be to determine whether this could be handled in the actual query, or should I do it on the SSRS side.
general good practice is to get rid of the data you don't need ASAP. so that would be in the query.
a SSRS multivalued parameter will show up as a comma separated list in the query.
the first step is to get from this comma separated list to a table (or table function), then you can join this table and apply like operators
for example
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS a
INNER JOIN dbo.split1('test,fafa,tata') b
ON 1=1
WHERE a.COLUMN_NAME like b.Value + '%'
will return rows having column names starting with test, fafa or tata. the dbo.split1 function you have to write your own or get one from the internet. the link suggested by Tab Alleman for example.

Sql Looking for Similar Names in a Table

I have a WinForms C# app which uses a SqlCommand to extract info from a back end SqlServer DB. When a user types in a name, I want to inspect my DB table to look for similar names (say having the same two initial characters). I've been trying the following sql command text:
SELECT Name
FROM tblCases
WHERE Name LIKE SUBSTRING(#Name,1,2);
Where #Name is a parameter in the SqlCommand generated by the application, the value of which is set by the user.
But this returns nil query results every time even when I know it should be returning some results.
What I am trying to achieve is that is the user types say name "Smith", I want the DB to return values like 'Smythe' or 'Smithson', but I cant see to get the SQL command text right.
Help most appreciated.
You can do it this way with the LIKE clause. It adds the wildcard % at the end of you name you are passing:
SELECT Name
FROM tblCases
WHERE Name LIKE (SUBSTRING(#name,1,2) + '%')

SELECT query in PostgreSQL

I am trying to retrieve values from a PostgreSQL database in a variable using a WHERE clause, but I am getting an error.
The query is:
select age into x from employee where name=name.GetValue()
name is the textcontrol in which I am entering a value from wxpython GUI.
I am getting an error as name schema doesn't exist.
What is the correct method for retrieving values?
"name.GetValue()" is a literal string, you are sending that to your db which knows nothing about wxpython and nothing about the variables in your program. You need to send the value of that data to your db, probably using bound parameters. Something like:
cur.execute("select age from employee where name=%s", [name.GetValue()])
x = cur.fetchone()[0] # returns a row containing [age] from the db
is probably what you're after. This will create a query with a placeholder in the database, then bind the value of name.GetValue() to that placeholder and execute the query. The next line fetches the first row of the result of the query and assigns x to the first item in that row.
I'm not positive what you are trying to do, but I think your issue might be syntax (misuse of INTO instead of AS):
SELECT age AS x FROM employee WHERE name = ....