NOT LIKE IN statement in SQL [duplicate] - sql

This question already has answers here:
Is there a combination of "LIKE" and "IN" in SQL?
(28 answers)
Closed 5 years ago.
I need to print names that do not start and end with vowel. I tried it like this:
SELECT DISTINCT name FROM people WHERE
lower(name) NOT LIKE IN ('a','e','i','o','u')%('a','e','i','o','u');
I got error.

You may want to try avoid using REGEXP from performance reasons in case of large data sets.
In such case is TRANSLATE your friend.
1) translate all vowels to one representative
2) perform normal LIKE predicate with the selected vowel
select txt from tab1
where translate(lower(txt),'aeiou','aaaaa') not like 'a%a';
REGEXPs are mighty, but should not be used on non-trivial data sets in case that they could be avoided. (My 8M rows test data gives 7 seconds elapsed using TRANSLATE vs. 2+ minutes with REGEXP).

You can use regexp_like with match parameter i for case insensitive matching:
select distinct name from people
where not regexp_like(name, '^[aeiou]($|.*[aeiou]$)', 'i');
Pattern details:
^[aeiou] - starts with a vowel
($|.*[aeiou]$) - either there is only one character (matched in the first step) or ends with a vowel.

Related

Split String in Oracle? [duplicate]

This question already has answers here:
Split String by delimiter position using oracle SQL
(2 answers)
Closed last month.
I have a select query in Postgres with SPLIT_PART() function which works fine in postgres
SELECT SPLIT_PART(totalscore, '_', 1) highlow,
SPLIT_PART(totalscore, '_', 2) score,
score_desc
FROM student_score
WHERE (totalscore LIKE 'HN%' OR totalscore LIKE 'LN%')
http://sqlfiddle.com/#!15/877966/4
The SPLIT_PART() is not working in Oracle
Could please let me know how to achieve the same in Oracle as SPLIT_PART() is not working .
http://sqlfiddle.com/#!4/042f62/5
You can use REGEXP_SUBSTR() with [^_]+ patterns such as
SELECT REGEXP_SUBSTR(totalscore, '[^_]+') AS highlow,
REGEXP_SUBSTR(totalscore, '[^_]+$') AS score,
score_desc
FROM student_score
WHERE REGEXP_LIKE(totalscore,'^HN_|^LN_')
Demo
considering the whole dataset contains one and only one underscore per each value of the column.

How to search for presence of em dash in T-SQL? [duplicate]

This question already has answers here:
What is the meaning of the prefix N in T-SQL statements and when should I use it?
(4 answers)
Closed 2 years ago.
I have a database column that is nvarchar(50). There are some records that might have an em dash in them.
I want to find those records.
However, when I copy and paste an em-dash from another program, SQL Server Management Studio treats it like a regular hyphen.
This just returns all the parts with hyphens even though it's an em-dash in between the percent signs:
select * from part
where partnum like '%−%'
How can I make SSMS search for the em-dash and not hyphens?
emdash: −
hyphen: -
In case anyone is wondering this was solved by learning how to use NVARCHAR searches. You can search for something you copy paste from another program by prefixing the search string with an 'N' like this:
SELECT * FROM part
WHERE PartNum LIKE N'%−%'
You could try something like this.
select * from part
where partnum like '%' + NCHAR(8212) + '%'

Extract word by Regex postgreSQL [duplicate]

This question already has an answer here:
Postgresql regexp_matches syntax not working as expected
(1 answer)
Closed 2 years ago.
a colums include code like 'a357' , 'b123' with many word and i want to extract them
it work with
select *, regexp_matches(col1,'a\d{3}') from table
but i also want the 'b123' code then i write this code not work:
select *, regexp_matches(col1,'(a|b)\d{3}') from table
where as (a|b) is regex. Please show me solution or any other way not regexp_matches because i need to trim '{}' sign after that.
The issue is the subexpression in parentheses. As the documentation explains:
If a match is found, and the pattern contains parenthesized
subexpressions, then the result is a text array whose n'th element is
the substring matching the n'th parenthesized subexpression of the
pattern.
In your case, this is easily fixed by using a character class:
regexp_matches(col1, '[ab]\d{3}')

How to Search for % using SQL and Wildcards? [duplicate]

This question already has answers here:
How do I escape a percentage sign in T-SQL?
(5 answers)
Closed 3 years ago.
I have to replace % in a number of fields. I need to get a list of the records to be changed first. I know how to do the actual REPLACE easily enough, but my query to find the records isn't working correctly.
SELECT * FROM inventory WHERE desc LIKE '%%%'
I also tried the following the the same results:
SELECT * FROM inventory WHERE desc LIKE '%'+CHAR(37)+'%'
What's the best way to search for %?
I am using SQL Server 2016.
You need to escape the wildcard:
where [desc] like '%$%%' escape '$'
or, use a character class:
where [desc] like '%[%]%'

SQL : REGEX MATCH - Character followed by numbers inside quotes

I have a column in sql which holds value inside double quotes like "P1234567" , "P1234" etc..
I need to identify only columns which start with letter P and is followed by seven digits (numbers) only. I tried where column like'"P[0-9][0-9][0-9][0-9][0-9][0-9][0-9]"' but it doesn't seem to work.
Can someone please correct me or point me to a thread which can help me out?
Thanks
Standard SQL has no regex support, but most SQL engines have regex extensions added to them on top of the standard SQL. So, for example, if you're using MySQL then you'd do this:
... WHERE column REGEXP '^"P[0-9]{7}"'
And if you're using Postgres then that would be:
... WHERE column ~ '^"P[0-9]{7}"'
(updated to match the double-quote part of the question, I'd misunderstood that to begin with)
How about using length and isnumeric:
Select
*
from
mytable
where
mycolumn like '"P%'
and len(mycolumn) = 10 --2 chars for quotes + 1 for 'P' + 7 for the digits
and isnumeric(substring(mycolumn, 3, 7))=1
This answer is for SQL Server, other DBMS's may have a different syntax for length