SQL search on part of column - sql

Let's say I have multiple 6 character Alphanumeric strings. abc123, abc231, abc456, cba123, bac231, and bac123.
Basically I want a select statement that can search and list all the abc instances.
I just want a select statement that can list all instances with keyword "abc".

Use LIKE and wildcards %
SELECT *
FROM yourtable
WHERE yourfield LIKE '%abc%'
Input
yourfield
abc123
abc231
abc456
cba123
bac231
bac123
Output:
yourfield
abc123
abc231
abc456
SQL Fiddle:

Could you try like this way
SELECT *
FROM yourtable
WHERE field LIKE '%a%' Or field LIKE '%b%' Or field LIKE '%c%'

Select * from tablename
where yourfield like 'yourconstantcharacters%'

You can use LIKE operator in sql
Starting With Case
Select * From Table Where Column LIKE 'abc%';
Ending With Case
Select * From Tablo Where Column Like '%abc';
Contains With Case
Select * From Table Where Column LIKE '%abc%';
So If you want to use escape character in LIKE condition , you must make small changes on your condition
For Example :
SELECT * FROM Table WHERE column LIKE '!%' escape '!';
More information for you is here

Related

Multiple strings in LIKE condition - Presto SQL

I want to query a column in my table using a LIKE condition and this works fine-
select * from my_table where my_column LIKE '%hello%';
But, how do I query this column with multiple strings in my LIKE condition? Looking for something like-
select * from my_table where my_column LIKE ['%hello%'|'example%'|'%random%'|'%demo'];
Use regexp_like():
select *
from my_table
where regexp_like(my_column, 'hello|example|random|demo');

MS Query with comma separated parameters

How to make MS Query work with comma separated parameters in Excel cell?
My query is:
SELECT *
FROM ABC
WHERE Id in (?)
When I put id number for example "1" the query works, but I want to put into a parameters cell a few id's 1, 2, 3, 4 etc, but then I'm trying to to this the query doesn't work... How can I put parameter with comma separated values?
there is 2 diff way to do it:
select * from abc where id in ('1','2','3') etc but not in excel - maybe use notepad++
second way :)
select * from abc where (id like '1' or id like '2' or id like '3') etc
:)
You can use IN in you sql query.
SELECT column_name(s)
FROM table_name
WHERE column_name IN (1,2,3,4);
also try to use BETWEEN with comma as parameters.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Selecting only integer data within a column with characters

I am trying to write a query that will select items with only integer data such as: 019280498 as opposed to 0129830INK. These are sku's in a product database and my current solution is something to this effect:
Column name is SKU
SELECT *
FROM mydb
WHERE SKU not like '%a%' or SKU not like '%b%' or SKU not like '%c%' ..... or SKU not like '%z%'
This would only return values with no characters in them. However it does not like what I have written and i'm sure there is a more elegant way to execute this.
Using your original method with like, you can do:
SELECT *
FROM mydb
WHERE SKU not like '%[^0-9]%';
This has the advantage that expressions like '3e9' are not accepted as a numeric value.
Or, if is specifically alphabetic characters that you want to keep out:
SELECT *
FROM mydb
WHERE SKU not like '%[a-z]%'; -- or for case sensitive collations '%[a-zA-Z]%'
There's an IsNumeric() function that could be used.
Select *
from mydb
Where IsNumeric(sku) = 0x1
SELECT DISTINCT Pincode
FROM ps_all_india_pin_code
WHERE Pincode between '100000' and '999999'

sql searching multiple words in a string

What is the most efficient and elegant SQL query looking for a string containing the words "David", "Moses" and "Robi". Assume the table is named T and the column C.
Select * from table where
columnname like'%David%' and
columnname like '%Moses%' and columnname like'%Robi%'
In SQL Server 2005+ with Full-Text indexing switched on, I'd do the following:
SELECT *
FROM T
WHERE CONTAINS(C, '"David" OR "Robi" OR "Moses"');
If you wanted your search to bring back results where the result is prefixed with David, Robi or Moses you could do:
SELECT *
FROM T
WHERE CONTAINS(C, '"David*" OR "Robi*" OR "Moses*"');
Here is what I uses to search for multiple words in multiple columns - SQL server
Hope my answer help someone :) Thanks
declare #searchTrm varchar(MAX)='one two three ddd 20 30 comment';
--select value from STRING_SPLIT(#searchTrm, ' ') where trim(value)<>''
select * from Bols
WHERE EXISTS (SELECT value
FROM STRING_SPLIT(#searchTrm, ' ')
WHERE
trim(value)<>''
and(
BolNumber like '%'+ value+'%'
or UserComment like '%'+ value+'%'
or RequesterId like '%'+ value+'%' )
)
If you care about the sequence of the terms, you may consider using a syntax like
select * from T where C like'%David%Moses%Robi%'
Oracle SQL :
select *
from MY_TABLE
where REGEXP_LIKE (company , 'Microsodt industry | goglge auto car | oracles database')
company - is the database column name.
results - this SQL will show you if company column rows contain one of those companies (OR phrase)
please note that : no wild characters are needed, it's built in.
more info at : http://www.techonthenet.com/oracle/regexp_like.php
if you put all the searched words in a temporaray table say #tmp and column col1, then you could try this:
Select * from T where C like (Select '%'+col1+'%' from #temp);
Maybe EXISTS can help.
and exists (select 1 from #DocumentNames where pcd.Name like DocName+'%' or CD.DocumentName like DocName+'%')
Oracle SQL:
There is the "IN" Operator in Oracle SQL which can be used for that:
select
namet.customerfirstname, addrt.city, addrt.postalcode
from schemax.nametable namet
join schemax.addresstable addrt on addrt.adtid = namet.natadtid
where namet.customerfirstname in ('David', 'Moses', 'Robi');

Select row which has apostrophe value postgresql

I want to select rows which has apostrophe value postgresql:
select * from table where column like '%'||chr(39)||'%'
But it doesn't work.
Table buku:
id new_issn
------------- --------
1.003.111.641 ''
1.003.111.642 ''
1.003.111.643 125698
select * from buku where new_issn like '%'||chr(39)||'%'
An apostrophe can be escaped with another apostrophe. Try this:
select * from table where column like '%''%'
Reference: Postgresql docs.