I am trying to get the user information from a table (named userinfo) from the Oracle database on the basis of name.
In database name can be like {"Ashwani Dahiya","Ashwani kumar","ashwani dahiya","ashwani kumar","Ashwani dahiya","ashwani Dahiya","ashwani"}
So I want if I search for name "ashwani" then it should return the above whole list of users
select *
from userinfo
where regexp_like('name','Ashwani([[:space:]]* | [[:space:]]+[a-zA-Z0-9]*)','i')
I had tried this but "no result found".
This expression
regexp_like('name','Ashwani([[:space:]]* | [[:space:]]+[a-zA-Z0-9]*)','i')
searches inside the string 'name' not inside a column called name. When you want to refer to a column you don't need quotes.
So you need your expression to:
regexp_like(name,'Ashwani([[:space:]]* | [[:space:]]+[a-zA-Z0-9]*)','i')
(Note the missing single quote ' around name).
But I don't see the need for a regex here. a simple
where lower(name) like '%ashwani%'
will also do the trick (and will not be slower than the regex because neither of them will use an index)
I assume you meant to use the column called NAME and not the literal 'name' so try without the quotes around name and also lose the spaces around the "|":
select * from userinfo where regexp_like(name,'Ashwani([[:space:]]*|[[:space:]]+[a-zA-Z0-9]*)','i')
Note that for testing purposes you can try it with literals like so:
select *
from dual
where regexp_like('ashwani ','Ashwani([[:space:]]*|[[:space:]]+[a-zA-Z0-9]*)','i')
As mentioned by a_horse_with_no_name, you may get away with using the "LIKE" but I'm guessing you're looking for just "ashwani" and not "ashwaniX" (where X is some other letter) in which you could have just
lower(name) = 'ashwani'
or lower(name) LIKE 'ashwani %'
The trouble with regex functions is that they can be rather slow if you're working with a lot of data.
if you wish to cling to regexes, try
select *
from userinfo
where regexp_count(name, '^ashwani', 1, 'i') = 1
;
but there really is no need for if the matches will always start with the literal to compare against.
Related
I hope there is no post limit since I have posted more than once today. :-P
Now I have a table in OracleSQL. I noticed there are some useless signs and want to delete them. The way I do it is to replace all of them. Below is my table and my query.
Here is my query:
SELECT
CASE WHEN WORD IN ('!', '"', '#','""') Then ''
ELSE WORD END
FROM TERM_FREQUENCY;
It is not giving me an error, but these special characters are not going away either... Any thoughts?
A little typo of yours: you use - instead of _
SELECT
CASE WHEN WORD IN ('!', '"', '#','""') Then ''
ELSE WORD END
-- FROM TERM-FREQUENCY; --This is where the problem is.
FROM TERM_FREQUENCY; -- Because your table is named TERM _ FREQUENCY
You originally tagged your question with 'replace' but then didn't use that function in your code. You're comparing each whole word to those fixed strings, not seeing if it contains any of them.
You can either use nested replace calls to remove one character at a time:
select replace(replace(word, '!', null), '"', null) from ...
... which would be tedious and rely on you identifying every character you didn't want; or you could use a regular expression only keep alphabetic characters, which I suspect is what you're really after:
select regexp_replace(word, '[^[:alpha:]]', null) from ...
Quick demo.
You might also want to use lower or upper to get everything into the same case, as you probably don't really want to count different capitalisation differently either.
I want SQLAlchemy to generate the following SQL code:
SELECT t171 AS "3Harm" FROM production
I've been playing around with something similar to this SQLAlchemy ORM snippet:
session.query(Production.t171.label('3harm'))
The problem here is that this doesn't properly quote "3harm" in the generated SQL. Instead of "3harm" this generates the unquoted 3harm, which is invalid because it starts with a numerical character and therefore raises the following Oracle exception:
ORA-00923: FROM keyword not found where expected
I can get this to work by capitalizing any character in the alias name:
session.query(Production.t171.label('3Harm'))
But I would still prefer to use all lowercase column names since the rest of my program is standardized for all lowercase. Any idea how to force quote the lowercase version?
Found the solution while looking for something else.
Any column can be forced to use quotes with column.quote = True.
So for the original example:
column = Production.t171.label('3harm')
column.quote = True
session.query(column)
Success!
The SQL you want to generate isn't valid; rather than this:
SELECT t171 AS '3Harm' FROM production
... you need the identifier to be enclosed in double quotes, not single quotes:
SELECT t171 AS "3Harm" FROM production
So it looks like you should be able to do this:
session.query(Production.t171.label('"3harm"'))
or maybe:
session.query(Production.t171.label("3harm"))
But I don't use SQLAlchemy and I don't have any way to check if either is valid; you might need to escape the double quotes in the first one, for instance, though from this perhaps the second is more likely to work... and I don't understand why 3Harm would work unquoted.
I'm using oracle text to do a readahead (according to the spec writer) in the search bar.
Basically, a user can start typing text and we fill the suggestions bar with likely matches.
I tried using oracle text for this, and ran into some issues, and the latest one being:
Table contains this entry for answertext: ... we offer many pricing options ...
SELECT
questiontext as qtext,
answertext as text,
questionid FROM question
WHERE contains(answertext, '{pric}', 1) > 0
;
This query returns nothing. But using {pricing} will return the correct result.
And suggestion why this is happening would be great!
Edit: just wanted to add that using stemming does not work for me because the user wants to differentiate between "report" and "reporting" and they want the matching substring to be highlighted which can be done if I can find the substring among the returned results.
Edit 2: I have my guess, that oracle tokenizes each word using word boundary of some sort in the index, and thus without any wildcards it looks for a token that equals = 'pric' and therefore does not find it (because there is a token 'pricing'). So, if that guess is correct I would love if someone can chime in for how I can make the query above work with the example entry while still maintaining whitespace so if type 'pricing options' it should return but if i type 'many options' it should not...
CONTAINS operator supports wildcards and fuzzy text search. Try:
SELECT * FROM question WHERE contains(answertext, '{pric%}', 1) > 0;
or
SELECT * FROM question WHERE contains(answertext, 'fuzzy({pric})', 1) > 0;
But with fuzzy "prize" will also match your search criteria.
To highlight found substrings you can use CTX_DOC.MARKUP.
When I am trying to execute INSERT statement in oracle, I got SQL Error: ORA-00917: missing comma error because there is a value as Alex's Tea Factory in my INSERT statement.
How could I escape ' ?
To escape it, double the quotes:
INSERT INTO TABLE_A VALUES ( 'Alex''s Tea Factory' );
In SQL, you escape a quote by another quote:
SELECT 'Alex''s Tea Factory' FROM DUAL
Instead of worrying about every single apostrophe in your statement.
You can easily use the q' Notation.
Example
SELECT q'(Alex's Tea Factory)' FROM DUAL;
Key Components in this notation are
q' which denotes the starting of the notation
( an optional symbol denoting the starting of the statement to be fully escaped.
Alex's Tea Factory (Which is the statement itself)
)' A closing parenthesis with a apostrophe denoting the end of the notation.
And such that, you can stuff how many apostrophes in the notation without worrying about each single one of them, they're all going to be handled safely.
IMPORTANT NOTE
Since you used ( you must close it with )', and remember it's optional to use any other symbol, for instance, the following code will run exactly as the previous one
SELECT q'[Alex's Tea Factory]' FROM DUAL;
you can use ESCAPE like given example below
The '_' wild card character is used to match exactly one character, while '%' is used to match zero or more occurrences of any characters. These characters can be escaped in SQL.
SELECT name FROM emp WHERE id LIKE '%/_%' ESCAPE '/';
The same works inside PL/SQL:
if( id like '%/_%' ESCAPE '/' )
This applies only to like patterns, for example in an insert there is no need to escape _ or %, they are used as plain characters anyhow. In arbitrary strings only ' needs to be escaped by ''.
SELECT q'[Alex's Tea Factory]' FROM DUAL
Your question implies that you're building the INSERT statement up by concatenating strings together. I suggest that this is a poor choice as it leaves you open to SQL injection attacks if the strings are derived from user input. A better choice is to use parameter markers and to bind the values to the markers. If you search for Oracle parameter markers you'll probably find some information for your specific implementation technology (e.g. C# and ADO, Java and JDBC, Ruby and RubyDBI, etc).
Share and enjoy.
Here is a way to easily escape & char in oracle DB
set escape '\\'
and within query write like
'ERRORS &\\\ PERFORMANCE';
How would I go about counting the characters after a certain character. I'm new to Oracle, and I've learned quite a bit however I'm stumped at this point. I found a couple functions that will get you a substring and I found a function that will give you the length of a string. I am examining an email address, myemail#thedomain.com. I want to check the length after the '.' in the email.
SELECT email
FROM user_table
WHERE length(substr(email, /*what values*/, /*to put here*/))
I don't know if it's actually possible to find the location of the final '.' in the email string?
I'm not sure I would use substr. You can try something like this :
select length('abcd#efgh.123.4567') - instr('abcd#efgh.123.4567', '.', -1) from dual
Using instr(..,..,-1) searches backwards from the last character to find the position.
Since you're doing checks, I suggest you validate the format with regular expressions using REGEXP_INSTR. For instance, an email validation I found on this site is REGEXP_INSTR(email, '\w+#\w+(\.\w+)+') > 0
I didn't check it myself, but it looks quite ok.
Cheers.