How to use a backward search using Oracle sql - sql

I would like to make a search for several parameters. In input string i can have fields: type and number.
Field number is a number like(3,2.15, 11.64).
Type field is a string like "com", "org", "cu", "mi"
Possible variations of input string:
3 mi
2 com
3,13--org
i would like to find some strings matching this string.
So, i have to make some
select number, type from orders
where (%number%) like '49-М' or (%type%) like '49-М'
But it doesn't work. How can i compare exactly fields?
And also i can't split string into 2 numbers, cause i don't know a separator

Your question is a little vague, but regexp_substr() should be able to help:
where (%number%) like regexp_substr('49-M', '^[0-9.]+', 1) and
(%type%) like regexp_substr('49-M', '[a-zA-Z]+$', 1)

Related

Check if a string has a combination of a substring and numbers in sql

how do I write a SQL where statement that checks if a string contains some substring and a number. For example:
string: macsea01
where string like 'macsea' plus a number
Regex is the most obvious solution to this question. Without more detail about the specific format of the string, I can suggest the following, which will match a sequence of a letter in the alphabet followed immediately by a digit:
where column_name like '%[a-zA-Z][0-9]%'
If you're literally looking for macsea at the beginning of the string followed by a digit, it would be:
where column_name like 'macsea[0-9]%'
Regex seem to bee a little slippery here, depending on your needs you can for instance divide the string into several parts, first the text part, and take the rest of the string, try to convert it into a number.
Somthing like this (but I think this perticular code is broken
where substring(column_name, 1, 6) = 'macsea' and cast(substring(column_name, 7, 1000) as int) > 0

Extracting number from a description filed for Hive

I am trying to extract numbers from a column which contains number and characters. They are however, structured hence I would like to know if we can just extract the number. I wonder if explode will work.
The current description column:
I need a help in setting up a campaign soon. Revenue: 1000
What I tried to do is to create a new column for that number called revenue.
My current command is:
SELECT description, X.value
FROM task
lateral view
explode(description) X as value
You could try using the Split function like this
SELECT
description,
split (description, ':\\s')[1] as Revenue
FROM task
Where :\\s is the regex pattern to match a colon followed by a space.
-------- EDIT: --------
If there are multiple : in the data then you could try (not sure if it will work though) the following (assuming that the last split will always contain the digits)
SELECT
description,
split (description, ':\\s')[size(split (description, ':\\s')) - 1] as Revenue
FROM task
Also your try of using Revenue\\s:\\s as the pattern may not be working due to the extra space matching try `Revenue:\s'
---------------------------
Or alternatively if the description doesn't always have the colon you could use the method regexp_extract(string subject, string pattern, int index)
Something like:
SELECT
description,
regexp_extract(description, '.*?(\d+)$', 1) as Revenue
FROM task
Where the regex pattern .*?(\\d+)$ will match multiple digits at the end of the description (but only if they are at the end)
With the latter option you should be able to find a suitable pattern if the description is not always consistent.
You can also use the following to remove any non-numeric characters:
select regexp_replace(description, '[^0-9]', '') as Revenue from task
This only works, though, if there is only a single number in the [description] field. If it's reliably formatted, using a more specific RegEx would likely be preferable.

Matching exactly 2 characters in string - SQL

How can i query a column with Names of people to get only the names those contain exactly 2 “a” ?
I am familiar with % symbol that's used with LIKE but that finds all names even with 1 a , when i write %a , but i need to find only those have exactly 2 characters.
Please explain - Thanks in advance
Table Name: "People"
Column Names: "Names, Age, Gender"
Assuming you're asking for two a characters search for a string with two a's but not with three.
select *
from people
where names like '%a%a%'
and name not like '%a%a%a%'
Use '_a'. '_' is a single character wildcard where '%' matches 0 or more characters.
If you need more advanced matches, use regular expressions, using REGEXP_LIKE. See Using Regular Expressions With Oracle Database.
And of course you can use other tricks as well. For instance, you can compare the length of the string with the length of the same string but with 'a's removed from it. If the difference is 2 then the string contained two 'a's. But as you can see things get ugly real soon, since length returns 'null' when a string is empty, so you have to make an exception for that, if you want to check for names that are exactly 'aa'.
select * from People
where
length(Names) - 2 = nvl(length(replace(Names, 'a', '')), 0)
Another solution is to replace everything that is not an a with nothing and check if the resulting String is exactly two characters long:
select names
from people
where length(regexp_replace(names, '[^a]', '')) = 2;
This can also be extended to deal with uppercase As:
select names
from people
where length(regexp_replace(names, '[^aA]', '')) = 2;
SQLFiddle example: http://sqlfiddle.com/#!4/09bc6
select * from People where names like '__'; also ll work

SQL String contains ONLY

I have a table with a field that denotes whether the data in that row is valid or not. This field contains a string of undetermined length. I need a query that will only pull out rows where all the characters in this field are N. Some possible examples of this field.
NNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNN
NNNNNEEEENNNNNNNNNNNN
NNNNNOOOOOEEEENNNNNNNNNNNN
Any suggestions on a postcard please.
Many thanks
This should do the trick:
SELECT Field
FROM YourTable
WHERE Field NOT LIKE '%[^N]%' AND Field <> ''
What it's doing is a wildcard search, broken down:
The LIKE will find records where the field contains characters other than N in the field. So, we apply a NOT to that as we're only interested in records that do not contain characters other than N. Plus a condition to filter out blank values.
SELECT *
FROM mytable
WHERE field NOT LIKE '%[^N]%'
I don't know which SQL dialect you are using. For example Oracle has several functions you may use. With oracle you could use condition like :
WHERE LTRIM(field, 'N') = ''
The idea is to trim out all N's and see if the result is empty string. If you don't have LTRIM, check if you have some kind of TRANSLATE or REPLACE function to do the same thing.
Another way to do it could be to pick length of your field and then construct comparator value by padding empty string with N. Perhaps something like:
WHERE field = RPAD('', field, 'N)
Oracle pads that empty string with N's and picks number of pad characters from length of the second argument. Perhaps this works too:
WHERE field = RPAD('', LENGTH(field), 'N)
I haven't tested those, but hopefully that give you some ideas how to solve your problem. I guess that many of these solutions have bad performance if you have lot of rows and you don't have other WHERE conditions to select proper index.

Is it possible to get the matching string from an SQL query?

If I have a query to return all matching entries in a DB that have "news" in the searchable column (i.e. SELECT * FROM table WHERE column LIKE %news%), and one particular row has an entry starting with "In recent World news, Somalia was invaded by ...", can I return a specific "chunk" of an SQL entry? Kind of like a teaser, if you will.
select substring(column,
CHARINDEX ('news',lower(column))-10,
20)
FROM table
WHERE column LIKE %news%
basically substring the column starting 10 characters before where the word 'news' is and continuing for 20.
Edit: You'll need to make sure that 'news' isn't in the first 10 characters and adjust the start position accordingly.
You can use substring function in a SELECT part. Something like:
SELECT SUBSTRING(column, 1,20) FROM table WHERE column LIKE %news%
This will return the first 20 characters from column column
I had the same problem, I ended up loading the whole field into C#, then re-searched the text for the search string, then selected x characters either side.
This will work fine for LIKE, but not full text queries which use FORMS OF INFLECTION because that may match "women" when you search for "woman".
If you are using MSSQL you can perform all kinds VB-like of substring functions as part of your query.