How to find all entries that match part of a string criteria but not another in Oracle SQL - sql

I have a column like:
Values
111.111.111-Dummy
111.111.111-Dummy2
111.111.111-X
222.222.222-Dummy
222.222.222-Dummy2
333.333.333-Dummy
333.333.333-Dummy2
333.333.333-X
I need to find the numbers that do not have an entry with "-X" in the end.
So in this scenario the query should show: 222.222.222.
My idea so far was to first trim the results to only have the numbers part (or everything before the '-')
But I don't know what to do next. How can I find entries that don't match in the same column and same table?

select substr(values_, 1, instr(values_, '-') - 1) as numbers
from {your-table}
group by substr(values_, 1, instr(values_, '-') - 1)
having count(case when values_ like '%-X' then 1 end) = 0;
values is a reserved keyword in Oracle, and therefore it can't be an identifier (such as a column name); I changed it by adding a trailing underscore.
Note that this assumes all "values" are followed by a dash and a (possibly empty) string. If you may also have values like 111.11.1111 (with no dash at the end) then the query must be modified slightly, but I assumed there aren't any - otherwise you should have included one or two in your sample.

Use not like in a having clause:
select substring_index(values, '-', 1)
from t
group by substring_index(values, '-', 1)
having sum(values like '%-x') = 0;

Related

Removing characters after a specified character format

I have a field that should contain 6 digits, a period, and six digits (######.######). The application that I use allows this to be free-form entry. Because users are users and will do what they want I have several fields that have a dash and some letters afterwards (######.######-XYZ).
Using T-SQL how do I identify and subsequently remove the -XYZ so that I can return the integrity of the data. The column is an NVARCHAR(36), PK, and does not allow null values. The column in question does have a unique columnID field.
If the part you want is the first 13 characters, then use left():
select left(field, 13)
You can check if the first 13 characters are what you expect:
select (case when field like '[0-9][0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9]%'
then left(field, 13)
else -- whatever you want when the field is bad
end)
since it'a free-form and "users are users", use charindex to find out if 1) there is a - and 2) remove it.
Example:
DECLARE #test NVARCHAR(36) = N'######.######-XYZ'
SELECT SUBSTRING(#test,1,COALESCE(NULLIF(CHARINDEX('-',#test,1),0),LEN(#test)+1)-1)

Sqlite - How to remove substring in a field using wildcards

I would like to remove all occurrences of a certain string pattern across all fields in a table.
For example, find all occurrences of the pattern "<XY>*<Xy>" where * represents any possible configuration of characters. I want to remove just those substrings and leave the remainder of the string intact.
This is an example of what I would like to use as my SQL command, but of course this doesn't work:
UPDATE Table SET Field = replace(Field, '<XY>*<Xy>', '');
What is the solution?
Here is an option which attempts to splice around the <XY>...</XY> tags:
UPDATE yourTable
SET Field = SUBSTR(Field, 1, INSTR(Field, '<XY>') - 1) ||
SUBSTR(Field, INSTR(Field, '</XY>') + 5)
WHERE Field LIKE '%<XY>%</XY>%'
It updates fields containing this pattern with the concatenation of everything coming before the first <XY> and everything coming after the second </XY>.
Note that I used <XY>...</XY> rather than what you had originally, because INSTR() is not case sensitive, and both tags would appear as being the same thing.
Demo
The demo is for MySQL but the sytnax is almost identical to SQLite.

remove dashes from a column in a select statment without altering table data

I have a query that grabs a list of 'job numbers' from a table. However, the job number displays numbers with a dash in the middle.(e.g. 645-123)
How do I select this field and only retrieve the number up to the dash (e.g.'645') and not the '-123'?
I do not want the data in the table to be 'replaced' or edited; I just need to select the data but without the dash and remaining digits after the dash.
Thanks for any help
You can use something like this:
select (case when jobnum like '%-%'
then left(jobnum, charindex('-', jobnum) - 1)
else jobnum
end)
This will not return an error if there is no hyphen (the reason for the case).

Select rows that has mixed charcters in a single value e.g. 'Joh?n' in name column

In an oracle table:
1- a value in a VARCHAR column contains characters that are not letters.
Consider a scenarion where a name in 'last_name' column contains regular characters (A - Z, a - z) as well as characters that are not english letters (e.g. '.', '-', ' ','_', '>' or similar).
The challenge is to select the rows that has names in 'last_name' as '.John' or 'John.' or '-John' or 'Joh-n'
2- Is it possible to have non-date values in a Date defined column? If yes, how can such records be selected in an oracle query?
Thanks!
I believe this will do the trick:
SELECT * FROM mytable WHERE REGEXP_LIKE(last_name, '[^A-Za-z]');
As for your 2nd question, I am unsure. I would be glad if someone else could add on to what I have to answer your 2nd question. I have found this website thought that might be of help. http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html
It explains the DATE format.
If I properly understand your goal, you need to select rows with last_name column containing the name 'John', but it may also have additional characters before, after, or even inside the name. In that case, this should be helpful:
select * from tab where regexp_replace(last_name, '[^A-Za-z]+', '') = 'John'

How to replace a comma separated value in table column with user input value oracle

I have a table in oracle with a column with comma separated values. What i need is when a user enters a value and if that value is present in any of the rows, it should be removed.
eg.
COL
123,234
56,123
If user enters 123, the 1st column should have only 234 and second row should have only 56.
How do we do this in oracle??
Please help
Thanks
delete from yourtable t
where
instr(','||t.col||',', '123') > 0
You can replace '123' with a parameter if you like.
But a better way would be not to store comma separated values, and create a detail table instead. If you need to look for a specific value within a comma separated list, you cannot make use of indices, amongst other limitations.
[edit]
Misunderstood the question. You meant this:
update YourTable t
set
t.col = substr(substr(replace(','||t.col||',', ',123,', ','), 2), -2)
where
instr(','||t.col||',', '123') > 0
Add ',' before and after to match items at the beginning or end of the value.
Replace using the value ',123,' (within comma's) to prevent accidentally matching 1234 too.
Use substr twice to remove the first and last character (the added commas)
Use instr in the where to prevent updating records that don't need to be updated (better performance).
try this :
UPDATE t
SET col = REPLACE(REPLACE(col, '&variable', ''), ',', '') FROM t ;