ABAP LIKE with select option - abap

I have the following abap program that looks as following:
TABLES lfa1.
DATA gt_lfa1 TYPE SORTED TABLE OF lfa1 WITH UNIQUE DEFAULT KEY.
SELECT-OPTIONS sl_lifnr FOR lfa1-lifnr.
SELECT-OPTIONS sl_name FOR lfa1-name1.
START-OF-SELECTION.
SELECT * FROM lfa1
INTO CORRESPONDING FIELDS OF TABLE gt_lfa1
WHERE lifnr IN sl_lifnr
AND name1 LIKE sl_name.
Searching for vendors that name starts with:
I've got no results, but it exists vendors with this pattern.

It's not necessary to use LIKE. You can use IN instead.
I ran your code with IN in the SQL and I have results in the table.

if you use a select option you have to use the statement IN not like:
s_matnr = *3578 *
select options s_matnr type mara-matnr.
select bismt
from mara
into lv_bismt
where matnr in s_matnr
If you use s/4hana you can use even fuzzy

You need to replace * with %, if you are using LIKE operand. https://help.sap.com/doc/abapdocu_750_index_htm/7.50/de-DE/abenwhere_logexp_like.htm
REPLACE ALL OCCURENCES OF '*' in sl_lifnr WITH '%'
REPLACE ALL OCCURENCES OF '*' in sl_name WITH '%'
SELECT * FROM lfa1
INTO CORRESPONDING FIELDS OF TABLE gt_lfa1
WHERE lifnr LIKE sl_lifnr
AND name1 LIKE sl_name.

Related

Select from where in ( ) table?

I want a select with dynamic where conditions in ABAP Syntax.
An SQL Statement would look like this:
SELECT * FROM MCH1 WHERE MATNR IN (...) AND CHARG IN (...)
My approach was to add 2 structures ZMATN_STR and ZCHARG_STR to the dictionary with associated components as line (MATNR, CHARG).
Then create 2 table types with associated line types.
Now im stucked in ABAP because I don't know how to write the where clause.
That's what I have so far:
SELECT *
FROM
mch1
FOR ALL ENTRIES IN #matnrs
WHERE
matnr = #matnrs-matnr
INTO TABLE #DATA(lt_result).
It works for either matnr or charg but not with both of them.
Additional Info
This select happens in a function module where 2 import parameter exists (the 2 table types) - so I cannot just write where in ('xxx', 'yyy')
data lr_matnr type range of matnr.
data lr_charg type range of MCH1-charg.
"Fill lr_matnr and lr_charg...
SELECT * FROM MCH1 WHERE MATNR IN #lr_matnr AND CHARG IN #lr_charg
INTO TABLE #data(lt_result).

Compare two columns with 'LIKE' Operator in SQL / Laravel

I'm trying to compare two columns from a table. In which i have to check the email is containing his mobile number or not.
TableName:- Table1
TableColumns:- id,email,MOB
Ex.
SQL:
'SELECT * FROM Tabel1 WHERE email LIKE %MOB%'
Laravel :
Tabel1::whereColumn('email', 'LIKE','%MOB%')->get();
I have Tried this above query but it is showing syntax error.
Lets start by answering you question SQL-wise, anything in quotes is a literal to SQL, so you need to use column reference and add the wildcard symbols. You can do that like this:
SELECT * FROM Table1 WHERE email LIKE CONCAT('%', MOB, '%');
Now lets look at Laravel now, the 3rd argument to where expects a literal value not another column. You can overcome this via either whereRaw or DB::raw:
Table1::whereRaw("email LIKE CONCAT('%', MOB, '%')");
or
Table1::where('email', 'LIKE', DB::raw("CONCAT('%', MOB, '%')"));
You should try this
SELECT * FROM Tabel1 WHERE email LIKE '%' +MOB+'%'
SELECT * FROM `table` WHERE `email` LIKE '%MOB%';

select TableData where ColumnData start with list of strings

Following is the query to select column data from table, where column data starts with a OR b OR c. But the answer i am looking for is to Select data which starts with List of Strings.
SELECT * FROM Table WHERE Name LIKE '[abc]%'
But i want something like
SELECT * FROM Table WHERE Name LIKE '[ab,ac,ad,ae]%'
Can anybody suggest what is the best way of selecting column data which starts with list of String, I don't want to use OR operator, List of strings specifically.
The most general solution you would have to use is this:
SELECT *
FROM Table
WHERE Name LIKE 'ab%' OR Name LIKE 'ac%' OR Name LIKE 'ad%' OR Name LIKE 'ae%';
However, certain databases offer some regex support which you might be able to use. For example, in SQL Server you could write:
SELECT *
FROM Table
WHERE NAME LIKE 'a[bcde]%';
MySQL has a REGEXP operator which supports regex LIKE operations, and you could write:
SELECT *
FROM Table
WHERE NAME REGEXP '^a[bcde]';
Oracle and Postgres also have regex like support.
To add to Tim's answer, another approach could be to join your table with a sub-query of those values:
SELECT *
FROM mytable t
JOIN (SELECT 'ab' AS value
UNION ALL
SELECT 'ac'
UNION ALL
SELECT 'ad'
UNION ALL
SELECT 'ae') v ON t.vame LIKE v.value || '%'

SQL query with regex

I have a table vehicles has a column 'name'
Values are stored like car/tesla, car/honda, truck/daimler (each value is stored as type/brand)
I want to query the table using only brand. If I look up tesla, it should return the row corresponding to car/tesla. How do I do it? I'm using postgres.
There is no need in regex in your case. Just use old good like:
select name
from vehicles
where name like '%/tesla'
2 solutions are available a select query with LIKE operand or a select query with contains operand.
select * from vehicles where name LIKE '%tesla%'
Select * from vehicles where Contains(name, "tesla");

using % in IN operator of sql

I have a query like this
select * from table_employee where name in ('Jack','Jon','Jade');
and it gives me three results.
but I want to use % with the names I mentioned in query like:
select * from table_employee where name in ('%Jack%','%Jon%','%Jade%');
The query execution is completed but now I get no results. Whats wrong here and how can i use % in IN operator?
You should use LIKE combined with OR, IN does not support wildcards
select * from table_employee where name LIKE '%Jack%'
OR name LIKE '%Jon%';
The in keyword does not work like that. You can use a like operand and bind the three values with or
example
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';
Convert it to an OR:
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';
If your fields data are exact what you want get in the result is no needed to use like . You should use like only when you don't know the data in a field but you know that it could contains a string Jack for example .
I'm sayng this because if you have an index on the field where you use LIKE it will be not used , so you will get performance problem .