LIKE but NOT LIKE - sql

I have a table filled with company code prefixes (cp-partnum,ag-partnum,ff-partnum). I would like to select all codes which begin with 'cp-partnum' where partnum does not begin with 'I' or 'i'. So 'cp-aaa' is acceptable but 'cp-iaa' is not. How can this be achieved?
edit: to be clear, I would like ONLY the codes that begin with 'cp-[letter where letter is not "I" or "i"]'

You can use SIMILAR TO and a (SQL) regular expression:
yourcolumn SIMILAR TO 'cp-[^iI]%'
If company are always 2 (alpha) characters, you can do something like:
yourcolumn SIMILAR TO '[:ALPHA:]{2}-[^iI]%'
If company code can be any two characters, you can use
yourcolumn SIMILAR TO '__-[^iI]%'
For more complex patterns, study the documentation. SIMILAR TO is available starting with Firebird 2.5.

Use below query:
select *
from companies
where company_code like '__-%'
and company_code not like '__-I%'
and company_code not like '__-i%'

You can use SUBSTRING and NOT IN with only 1 LIKE :
WHERE YourColumn LIKE 'cp-%'
AND SUBSTRING(YourColumn from 4 for 1) NOT IN('i','l')

You can do this:
where col like 'cp-%' and
col not like 'cp-l%' and
col not like 'cp-i%'
For multiple prefixes:
where left(col, 2) in ('cp', 'ag', 'ff') and
substring(col, 4, 1) not in ('l', 'i') and
col like '__-%'

I guess I did not need to support lower case letters so the following worked.
SELECT * FROM product
WHERE product.num LIKE 'CP-%'
AND product.num NOT IN (SELECT product.num FROM product
WHERE product.num LIKE 'CP-I%')

Related

like search on number column in SQL

How do I do a like search on a number column in SQL?
I want numbers which are like '0.0000%'.
I tried with
select * from emp where emp_id & '' like '123%'
select * from emp where CONVERT(varchar(20), emp_id) like '123%'
but in vain.
Please help me
Regardless of which DBMS you are using AND assuming you have a valid reason to do this, you have several ways to solve problems like these. I can think of three right now:
Convert the number to a string and use a LIKE operator on this:
select *
from emp
where to_char(emp_id) like '123%';
Use mathematical operators directly (like Andrey suggests), for example:
select *
from table
where num between 0 and 0.0001;
Construct a mathematical expression (actually, this is just another case of method 2), for example:
select *
from table
where abs(num - round(num, 5)) < 0.00001;
Use comparison operators (> and <):
select * from table where num > 0 and num < 0.00001
select 0.0001*1000 from dual if it is <1 means the 0.0001 has 3 or more zeros.
so i did like
select * from emp where emp_id*10000<1

Search (in Oracle), using wild card without worrying about the case or order in which the words appear

How to search using wild card (in Oracle), without worrying about case or order in which the words appear.
e.g. if I search for like '%a%b%', it should return values containing *a*b*, *A*B* ,*b*a* and *B*A* This is just a sample, the search may have 5 or more words, is it possible get the result in just one expression rather than using AND.
select *
from yourtable
where yourfield like '%a%'
and yourfield like '%b%'
Or you can investigate Oracle Text
select * from table
where upper(column) like '%A%B%';
or
select * from table
where lower(column) like '%a%b%';
or
select * from table
where upper(column) like '%A%'
and upper(column) like '%B%';
or
select * from table
where lower(column) like '%a%'
and lower(column) like '%b%';
However, if you would like to find out *a*b* or *b*a* and so on. The other solution would be to sort the CHARS in the VARCHAR first i.e. if you have a string like aebcd then sort it to abcde and then do pattern match using like. As per this you can use below query to sort the chars in a varchar and then do the pattern match on it.
SELECT 1 val
FROM (SELECT MIN(permutations) col
FROM (SELECT REPLACE (SYS_CONNECT_BY_PATH (n, ','), ',') permutations
FROM (SELECT LEVEL l, SUBSTR ('cba', LEVEL, 1) n
FROM DUAL --replace dual by your table
CONNECT BY LEVEL <= LENGTH ('cba')) yourtable
CONNECT BY NOCYCLE l != PRIOR l)
WHERE LENGTH (permutations) = LENGTH ('cba')) temp_tab
WHERE upper(col) like '%A%B%';
Returns
val
------------------
1

How to quickly compare many strings?

In SQL Server, I have a string column that contains numbers. Each entry I need is only one number so no parsing is needed. I need some way to find all rows that contain numbers from 400 to 450. Instead of doing:
...where my stringcolumn like '%400%' or stringcolumn like '%401%' or stringcolumn like '%402%' or ...
is there a better that can save on some typing?
There are also other values in these rows such as: '5335154', test4559#me.com', '555-555-5555'. Filtering those out will need to be taken into account.
...where stringcolumn like '4[0-4][0-9]' OR stringcolumn = '450'
You don't need the wildcard if you want to restrict to 3 digits.
Use regex to accomplish this.
...where stringcolumn like '4[0-4][0-9]' OR stringcolumn like '450'
one way
WHERE Column like '%4[0-4][09]%'
OR Column LIKE '%500%'
keep in mind that this will pick anything with the number in it, so 5000 will be returned as well
I would do the following:
select t.*
from (select t.*,
(case when charindex('4', col) > 0
then substrint(col, charindex('4', col), charindex('4', col) + 2)
end) as col4xx
from t
) t
where (case when isnumeric(col4xx) = 1
then (case when cast(col4xx as int) between 400 and 450 then 'true'
end)
end) = 'true'
I'm not a fan of having case statements in WHERE clauses. However, to ensure conversion to a number, this is needed (or the conversion could become a column in another subquery). Note that the following is not equivalent:
where col4xx between '400' and '450'
Since the string '44A' would match.

SQL startswith (using `LIKE`) on an expression

What's an appropriate way to do startswith(expression) in SQL?
I can do it with LIKE ((expression) || '%'), but it doesn't look very nice to me.
Full query is in form:
SELECT …, (SELECT COUNT(*)
FROM post AS child
WHERE child.path LIKE (post.path || '%')
AND child.depth >= post.depth)
FROM post WHERE …
I suppose it is preferable to use LIKE because of DB indexing for this case.
Just use LIKE 'input%'. I.E:
WHERE child.path LIKE post.path + '%'
(I assume this is for SQL Server, though this syntax probably works elsewhere)
In standard SQL, you can also say:
where position(post.path in child.path) = 1
I don't know if your RDBMS supports that. PostgreSQL does.
You can use
where DATE LIKE '[(SELECT STR(YEAR(GETDATE())-1))]%'
WHERE child.path LIKE '[(SELECT STR(YEAR(GETDATE())-1))]%' (post.path || '%')
WHERE CustomerName LIKE 'a%'
--Finds any values that start with "a"
WHERE CustomerName LIKE '%a'
--Finds any values that end with "a"
WHERE CustomerName LIKE '%or%'
--Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%'
--Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a__%'
--Finds any values that start with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o'
--Finds any values that start with "a" and ends with "o"
-- Case insensitive
2
SELECT * FROM my_table WHERE upper(my_column) LIKE 'SEARCHED %';
-- starts with
3
SELECT * FROM my_table WHERE upper(my_column) LIKE '% SEARCHED';
-- ends with
4
SELECT * FROM my_table WHERE upper(my_column) LIKE '%SEARCHED%'; -- contains

Searching Technique in SQL (Like,Contain)

I want to compare and select a field from DB using Like keyword or any other technique.
My query is the following:
SELECT * FROM Test WHERE name LIKE '%xxxxxx_Ramakrishnan_zzzzz%';
but my fields only contain 'Ramakrishnan'
My Input string contain some extra character xxxxxx_Ramakrishnan_zzzzz
I want the SQL query for this. Can any one please help me?
You mean you want it the other way round? Like this?
Select * from Test where 'xxxxxx_Ramakrishnan_zzzzz' LIKE '%' + name + '%';
You can use the MySQL functions, LOCATE() precisely like,
SELECT * FROM WHERE LOCATE("Ramakrishnan",input) > 0
Are the xxxxxx and zzzzz bits always 6 and 5 characters? If so, then this is doable with a bit of string cutting.
with Test (id,name) as (
select 1, 'Ramakrishnan'
union
select 2, 'Coxy'
union
select 3, 'xxxxxx_Ramakrishnan_zzzzz'
)
Select * from Test where name like '%'+SUBSTRING('xxxxxx_Ramakrishnan_zzzzz', 8, CHARINDEX('_',SUBSTRING('xxxxxx_Ramakrishnan_zzzzz',8,100))-1)+'%'
Results in:
id name
1 Ramakrishnan
3 xxxxxx_Ramakrishnan_zzzzz
If they are variable lengths, then it will be a horrible construction of SUBSTRING,CHARINDEX, REVERSE and LEN functions.