Like command SQL - sql

I'm trying to construct an SQL query that looks for multiple different things I think the like command is the best way to achieve this
the line in the query is:
where *field* like 'AB%' it lists all the instaces in the table where it begins AB....
when i try and add multiple instances though i get an error message, I want to do something like the following:
where *field* like ('AB%','CD%','EF%')
So I get the fields for specific entries that start with the list of text I have provided
Could somebody help me with this please?

You want OR
WHERE field LIKE 'AB%'
OR field LIKE 'CD%'
OR field LIKE 'EF%'
If you are using other WHERE clause criteria, you'll need to bracket off the OR clauses using parenthesis.
Hope it helps...
EDIT:
After your comment, you could try using regular expressions, especially REGEXP_LIKE in your WHERE clause.
Something along the lines of (untested):
SELECT *
FROM table
WHERE regexp_like(field, '^(AB|CD|EF).*$')

WHERE *field* LIKE 'AB%' OR *field* LIKE 'CD%' OR *field* LIKE 'EF%'

If you are using Oracle, you can write:
where regexp_like(t1, '^AB|^CD|^EF')
etc.

You could use the instr function. If it finds the substring you want at the first position of the string, it would return 1:
where instr(field,'AB') = 1
OR instr(field,'CD') = 1
OR instr(field,'ED') = 1

If you want to do it with single string, try to use REGEXP:
WHERE *field* REGEXP 'AB(.*)|CD(.*)|EF(.*)'

Related

BigQuery - Using regexp with LIKE operator (?)

I'd like to get productids from url and I've almost finetuned a query to do it but still there is an issue I cannot solve.
The url usually looks like this:
/xp-pen/toll-spe43-deco-pro-small-medium-spe43-tobuy-p665088831/
or
/harry-potter-es-a-tuz-serlege-2019-m19247107/
As you can see there are two types of ids:
in general, ids start with '-p'
ids of some special products start with '-m'
I created this case when statement:
CASE
WHEN MAX(hits.page.pagePath) LIKE '%-p%'
THEN MAX(REGEXP_REPLACE(REGEXP_EXTRACT(
hits.page.pagePath, '-p[0-9]+/'), '\\-|p|/', ''))
WHEN MAX(hits.page.pagePath) LIKE '%-m%'
THEN MAX(REGEXP_REPLACE(REGEXP_EXTRACT(
hits.page.pagePath, '-m[0-9]+/'), '\\-|m|/', ''))
ELSE NULL
END AS productId
It's a little complicated at the first look but I really needed a regexp_replace and a regexp_extract because '-p' or '-m' characters doesn't appear only before the id but it can be multiplied times in a url.
The problem with my code is that there are some special cases when the url looks like this:
/elveszett-profeciak-2019-m17855487/
As you can see the id starts with '-m' but the url also contains '-p'. In this case the result is empty value in the query.
I think it could be solved by modifying the like operator in the when part of the case when statement: LIKE '%-p%' or LIKE '%-m%'
It would be great to have a regexp expression after or instead of the LIKE operator. Something similar to the parameter of '-p[0-9]+/' what I used in regexp_extract function.
So what I would need is to define in the when part of the statement that if the '-p' or '-m' text is followed by numbers in the urls
I'm not sure it's possible to do or not in BQ.
So what I would need is to define in the when part of the statement that if the '-p' or '-m' text is followed by numbers in the urls
I think you want '-p' and '-m' followed by digits. If so, I think this does what you want:
select regexp_extract(url, '-[pm][0-9]+')
from (select '/xp-pen/toll-spe43-deco-pro-small-medium-spe43-tobuy-p665088831/' as url union all
select '/elveszett-profeciak-2019-m17855487/' union all
select '/harry-potter-es-a-tuz-serlege-2019-m19247107/'
) x

Similar to with regex in Postgresql

In Postgresql database I have a column called names where I have some names which need to be parsed using regex to clean up punctuation parts. I am able to get a clean name using regexp_replace as follows:
select regexp_replace(name,'\.COM|''[A-Z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)','','g')
from tableA
However, I would like to compare with some strings that are also cleaned of punctuation. How can I use similar to with the formed regular expression?
select name
from tableA
where (lower(name) ~ '\.COM|''[A-Za-z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)') as nameParsed similar to '(fg )%' and
(lower(name) ~ '\.COM|''[A-Za-z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)') as nameParsed similar to '%( cargo| carrier| cartage )%'
With the previous query I am getting this error:
LINE 3: ...-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)') as namePar...
I have tried in where clause like this and it seems to be working:
select name
from tableA
where (select lower(regexp_replace(name,'\.COM|''[A-Z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)','','g'))) similar to '(fg )%'
Is this the best approach? The execution time went to 46 seconds :(
Thanks in advance
You're trying to get a column name in a WHERE clause (is a comparison, not a column). So, you can use as follows:
SELECT name
FROM "tableA"
WHERE (regexp_replace(name,'\.COM|''[A-Z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)','','g') similar to '(fg )%'
OR regexp_replace(name,'\.COM|''[A-Z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)','','g') similar to '%( cargo| carrier| cartage )%');
Alternatively, you can use ilike instead of similar to if you want to find a specific word.

Using parameter from a form as part of a "like" sql statement

I have a query like this:
select name,label from tablename where label like '*[form]![label search]![searchtxt]*';
I know the query works in the sense that if I replace the "[form]![label search}![searchtxt]" with an actual word, the query works. When I run the query as is, the data from the form doesn't go into the query so I get no rows.
How can I get the like part of the query to work with an input from a form. I realize that this will probably require some VBA in addition to SQL.
Thanks
You need to concatenate the asterisks in. Something like:
select name,label from tablename where label like '*' & [form]![label search]![searchtxt] & '*';

Crystal reports, trying to use: startswith and looking for 2 values

I am new with Crystal Reporting. I need to search the records and find all records that start with the letter "P" and "W"
Would I say something like this? {MNBDD.BD_ORD} startswith ["W", "P"]
Or something like, {MNBDD.BD_ORD} startswith ["W" OR "P"]
Or something like, {MNBDD.BD_ORD} startswith ["W" AND "P"]
What I am wanting to do it just return the records with P and W in the front. but, I don't want to leave any out.
you can try like this in crystal reports:
{MNBDD.BD_ORD} like "P*" and
{MNBDD.BD_ORD} like "W*"
I assume your table name is MNBDD and BD_ORD is the column which should start with either 'P' or 'W'. Then this query should work for you.
select * from MNBDD
where MNBDD.BD_ORD like 'W%'
or MNBDD.BD_ORD like 'P%';
like compares the column values with the specified string pattern where '%' signifies wildcard string. This will fetch all records that start with either 'P' or 'W' without leaving out any records.
I just tested under Crystal Reports 2013 and it looks like your first option works:
{MNBDD.BD_ORD} startswith ["P", "W"]
The keyword is startswith in Crystal Reports which functions as "W*" or "P*", * being a wildcard.

access: LIKE conditional formatting question

i am doing conditional formatting on a report in access
i need to check if a certain string exists in a field as the condition in conditional formatting. something like this:
[field_name] like '%something%'
will this kind of condition work?
You have to use asterisk with MS Access.
[field_name] like '*something*'
For a single character, you can do this
[field_name] like 'fieldnam?'
For three characters, you can do this
[field_name] like 'fieldna???'
Access uses the asterisk instead of the percent. Try
[field_name] like "*something*"
Yes, but it is a condition...
so you need something like
SELECT * FROM Foobar WHERE
[field_name] LIKE '*something*'