I need to find all the FIRST_NAMES in the table that looks like - There is a substring of 3 letter in the name of which first letter is in range 'A' to 'X' and 3rd one is 'A'. Moreover, the substring should be like '[A-X]_A' and the Name should be like '%[A-X]_A%'. But however ORACLE is showing NO DATA FOUND . Whenever I try with '[]' ORACLE can't find any data. Can anyone give me the solution?
For more information: I am using HR SCHEMA in LIVESQL.oracle.com for the purpose.
You want a regular expression:
where regexp_like(col, '[A-X].A')
I'm trying to filter out words that have two letter "a"'s in them.
I've tried using LIKE, but can't figure out how to filter words with two "a"'s (they don't have to be consecutive).
SELECT Sanat.sana FROM Sanat WHERE Sanat.sana LIKE '%a%';
For exactly 2 a's you could do:
SELECT Sanat.sana FROM Sanat WHERE LENGTH(REPLACE(Sanat.sana, 'a', '')) = LENGTH(Sanat.sana) - 2;
You would use like:
WHERE Sanat.sana LIKE '%a%a%';
Note: This will generally also match upper-case 'a's.
"LIKE '%a%a%'" would also show words with more than 2 a's.
You could instead use something like this:
SELECT sana FROM Sanat WHERE sana LIKE '%a%a%' AND sana NOT LIKE '%a%a%a%';
I need to write a select statement that returns all last names from a column that contains the letter A. I can't use LIKE. I am trying to do so with SUBSTR.
I don't think substr is the way to go. instr, on the other hand, may do the trick:
SELECT last_name
FROM mytable
WHERE INSTR(last_name, 'A') > 0
EDIT:
As David Bachmann Jeppesen mentioned, Oracle is case sensitive, so if you want to find last names containing any case of "A", you could do something like this:
SELECT last_name
FROM mytable
WHERE INSTR(UPPER(last_name), 'A') > 0
I have a situation where a string should match one pattern or the other. I tried several options, but none works. If I use both patterns independently they work, but when I concatenate using Pipe ["|"] operator, outcome is not correct. Any help is much appreciated. Thank you in advance.
Select 'P' from dual Where REGEXP_LIKE('W777AA,WZGET0,WZGEG0','(^W[0-9A-Z]{5}(,W[0-9A-Z]{5}){0,3}$)')
Select 'P' from dual Where REGEXP_LIKE('WZGET%','%$')
Concatenate SQL:
Select 'P' from dual Where REGEXP_LIKE ('W777AA,WZGET0,WZGEG0','(^W[0-9A-Z]{5}(,W[0-9A-Z]{5}){0,3}$ | (%$))')
Smells like an order of operations issue. Try throwing in some parens, something like this:
Concatenate SQL: Select 'P' from dual Where REGEXP_LIKE
('W777AA,WZGET0,WZGEG0','((^W[0-9A-Z]{5}(,W[0-9A-Z]{5}){0,3}$)|(%$))')
I want to get only those rows that contain ONLY certain characters in a column.
Let's say the column name is DATA.
I want to get all rows where in DATA are ONLY (must have all three conditions!):
Numeric characters (1 2 3 4 5 6 7 8 9 0)
Dash (-)
Comma (,)
For instance:
Value "10,20,20-30,30" IS OK
Value "10,20A,20-30,30Z" IS NOT OK
Value "30" IS NOT OK
Value "AAAA" IS NOT OK
Value "30-" IS NOT OK
Value "30," IS NOT OK
Value "-," IS NOT OK
Try patindex:
select * from(
select '10,20,20-30,30' txt union
select '10,20,20-30,40' txt union
select '10,20A,20-30,30Z' txt
)x
where patindex('%[^0-9,-]%', txt)=0
For you table, try like:
select
DATA
from
YourTable
where
patindex('%[^0-9,-]%', DATA)=0
As per your new edited question, the query should be like:
select
DATA
from
YourTable
where
PATINDEX('%[^0-9,-]%', DATA)=0 and
PATINDEX('%[0-9]%', LEFT(DATA, 1))=1 and
PATINDEX('%[0-9]%', RIGHT(DATA, 1))=1 and
PATINDEX('%[,-][-,]%', DATA)=0
Edit: Your question was edited, so this answer is no longer correct. I won't bother updating it since someone else already has updated theirs. This answer does not fulfil the condition that all three character types must be found.
You can use a LIKE expression for this, although it's slightly convoluted:
where data not like '%[^0123456789,!-]%' escape '!'
Explanation:
[^...] matches any character that is not in the ... part. % matches any number (including zero) of any character. So [^0123456789-,] is the set of characters that you want to disallow.
However: - is a special character inside of [], so we must escape it, which we do by using an escape character, and I've chosen !.
So, you match rows that do not contain (not like) any character that is not in your disallowed set.
Use option with PATINDEX and LIKE logic operator
SELECT *
FROM dbo.test70
WHERE PATINDEX('%[A-Z]%', DATA) = 0
AND PATINDEX('%[0-9]%', DATA) > 0
AND DATA LIKE '%-%'
AND DATA LIKE '%,%'
Demo on SQLFiddle
As already mentioned u can use a LIKE expression but it will only work with some minor modifications, otherwise too many rows will be filtered out.
SELECT * FROM X WHERE T NOT LIKE '%[^0-9!-,]%' ESCAPE '!'
see working example here:
http://sqlfiddle.com/#!3/474f5/6
edit:
to meet all 3 conditions:
SELECT *
FROM X
WHERE T LIKE '%[0-9]%'
AND T LIKE '%-%'
AND T LIKE '%,%'
see: http://sqlfiddle.com/#!3/86328/1
Maybe not the most beautiful but a working solution.