To extract only the appropriate rows, i have to do something like Mycolum in ('x%','f%') : wich means i want all the rows where Mycolumn is starting with x or f,
i can use REGEXP_LIKE(Mycolumn, '^x', 'i') : to extract all rows where Mycolumn start with x how can i add OR in regex to tell to my function i need also rows starting with f
thanks
You can do this:
REGEXP_LIKE(Mycolumn, '^[xf]')
You can use OR to provide both the conditions.
SELECT * FROM TABLE_NAME
WHERE REGEXP_LIKE(COLUMN, '^x')
OR REGEXP_LIKE(COLUMN, '^f');
I love regexes, but you don't need them for this. I would use just normal LIKE, which may be faster:
WHERE MyColumn LIKE '[^xfXF]%'
LIKE also has a more readable syntax, in my opinion at least.
Related
I have a database with workers and their names. How can I get a list of the workers whose name contains only 5 characters
What about this?
SELECT * FROM table_name WHERE island_name LIKE '_____'
Alternatively,
SELECT * FROM table_name WHERE REPLICATE('A',LEN(island_name)) = 'AAAAA'
You could use SUBSTRING(), if you are looking for an alternative method. You can check if a SUBSTRING of X characters == The Original String.
However, you would also need to account for 4-character strings, for example. You could add "padding characters", or you could make sure that X-1 Character Substring != X-character Substring. In Sql 2016 for example, these are the same with at least one case of query options:
SELECT SUBSTRING('ISLA',1,4)
SELECT SUBSTRING('ISLA',1,5)
I agree that LENGTH is the best option. Maybe this is a school question.
Give this a try:
SELECT SUBSTR(field1,1,5)
FROM table1
WHERE substr(field1,5,1) IS NOT NULL
AND SUBSTR(segment1,6,999) IS NULL;;
I have a query where I am using regex_like, and I need more than one parameter, something like this:
WHERE regexp_like (FILENAME,'_G_',) or (FILENAME,'_Z_',) or (FILENAME,'_M_',)
Thanks in advance
You can factorize the regexp as follows:
WHERE regexp_like (FILENAME,'_[GMZ]_',)
[GMZ] represents a custom character class made of characters 'G', 'M' and 'Z'.
You can use the following regexp:
regexp_like (FILENAME,'.{1}[GZM]{1}.{1}')
Here . (dot) represents any character
{1} represents only 1 character is allowed for the preceding pattern.
Cheers!!
If you want to add two or more different parameters, that do not have a lots in common, then you can use | to separate them like this:
select *
from table_name
WHERE regexp_like (FILENAME,'_G_|-kk_|-AH-');
Here is a small DEMO
Do not know what exactly you want when you ask to "order by it" but try this:
select id, filename
from table_name
WHERE regexp_like (FILENAME,'_G_|-kk_|-AH-')
order by filename
I have column store_name (varchar). In that column I have entries like prime sport, best buy... with a space. But when user typed concatenated string like primesport without space I need to show result prime sport. how can I achieve this? Please help me
SELECT *
FROM TABLE
WHERE replace(store_name, ' ', '') LIKE '%'+#SEARCH+'%' OR STORE_NAME LIKE '%'+#SEARCH +'%'
Well, I don't have much idea, and even I am searching for it. But may be what I know works for you, You can achieve this by performing different type of string operations:
Mike can be Myke or Myce or Mikke or so on.
Cat an be Kat or katt or catt or so on.
For this you should write a function to generate number of possible strings and then form a SQL Query using all these, and query the database.
A similar kind of search in known as Soundex Search from Oracle and Soundex Search from Microsoft. Have a look of it. this may work.
And overall make use of functions like upper and lower.
Have you tried using replace()
You can replace the white space in the query then use like
SELECT * FROM table WHERE replace(store_name, ' ', '') LIKE '%primesport%'
It will work for entries like 'prime soft' querying with 'primesoft'
Or you can use regex.
I am using Oracle 11 G and have the following set of data:
12.0
4.2
Version.1
7.9
abc.72
I want to return all string characters before the period. What sort of query would I run in order to achieve this? Any help would be greatly appreciated, thanks!
You can try a combination of instr and substr.
Something like this:
select substr(field, 1, instr(field, '.') - 1)
from your_table;
Assuming field always contains a . character on it.
You can also deal with strings without a . by using case, if or any other similar valid conditional function on Oracle's SQL language implementation.
Of course, you can always put this on a function to make it look nicer on your query.
I'm trying to construct an SQL query that looks for multiple different things I think the like command is the best way to achieve this
the line in the query is:
where *field* like 'AB%' it lists all the instaces in the table where it begins AB....
when i try and add multiple instances though i get an error message, I want to do something like the following:
where *field* like ('AB%','CD%','EF%')
So I get the fields for specific entries that start with the list of text I have provided
Could somebody help me with this please?
You want OR
WHERE field LIKE 'AB%'
OR field LIKE 'CD%'
OR field LIKE 'EF%'
If you are using other WHERE clause criteria, you'll need to bracket off the OR clauses using parenthesis.
Hope it helps...
EDIT:
After your comment, you could try using regular expressions, especially REGEXP_LIKE in your WHERE clause.
Something along the lines of (untested):
SELECT *
FROM table
WHERE regexp_like(field, '^(AB|CD|EF).*$')
WHERE *field* LIKE 'AB%' OR *field* LIKE 'CD%' OR *field* LIKE 'EF%'
If you are using Oracle, you can write:
where regexp_like(t1, '^AB|^CD|^EF')
etc.
You could use the instr function. If it finds the substring you want at the first position of the string, it would return 1:
where instr(field,'AB') = 1
OR instr(field,'CD') = 1
OR instr(field,'ED') = 1
If you want to do it with single string, try to use REGEXP:
WHERE *field* REGEXP 'AB(.*)|CD(.*)|EF(.*)'