SELECT *
FROM employee
WHERE is_deleted != true
AND (:lastname IS NULL
OR lastname ILIKE '%'||lastname||'%'
OR :firstname IS NULL
OR firstname ILIKE '%'||:firstname||'%'
OR :middlename IS NULL
OR middlename ILIKE '%'||:middlename||'%');
I have a full name column and I need to filter by first name, last name or patronymic, depending on what the user enters (or last name and first name together) b tell me how to implement
Your logic is correct except the the various name criteria should be ANDed together:
SELECT *
FROM employee
WHERE is_deleted != true AND
(:lastname IS NULL OR lastname ILIKE '%' || lastname || '%') AND
(:firstname IS NULL OR firstname ILIKE '%' || :firstname || '%') AND
(:middlename IS NULL OR middlename ILIKE '%' || :middlename || '%');
SELECT *
FROM employee
WHERE is_deleted != true
AND (:search_term IS NULL OR
(split_part(full_name, ' ', 1) ILIKE '%' || :search_term || '%' OR
split_part(full_name, ' ', 2) ILIKE '%' || :search_term || '%' OR
split_part(full_name, ' ', 3) ILIKE '%' || :search_term || '%'));
This query will use the split_part() function to extract the first, second, and third parts of the full name, and then use them in the filtering. The ILIKE operator is used for case-insensitive matching. If the search_term parameter is null, the filtering will be skipped, and all the rows will be returned.
Related
We have a stored proc, called externally to feed the sname, sssn.
The where must be applied when sname and sssn are NOT NULL only.
Desired Result
name is provided as "John Doe" but sssn is blank then the query where la.name clause will be filter results.
The query attempted is as below. AND only works when both are provided.
OR doesn't do its job as well.
SELECT
la.id,
la.name,
la.application_number,
la.status,
la.created_at
FROM
loanapp_flattened_view la
WHERE
la.name ILIKE '%' || sname || '%'
AND la.ssn ILIKE '%' || sssn
Is this what you want?
WHERE (la.name ILIKE '%' || sname || '%' OR sname IS NULL) AND
(la.ssn ILIKE '%' || sssn OR sssn IS NULL)
This ignores the parameter if it is NULL.
or you can use or , however this returns result even if combination of name and sssn doesn't exists , it returns result for each match name and sssn separately:
SELECT
la.id,
la.name,
la.application_number,
la.status,
la.created_at
FROM
loanapp_flattened_view la
WHERE
la.name ILIKE '%' || sname || '%'
OR la.ssn ILIKE '%' || sssn
How would I make it so that this query is case insensitive so it finds instances of "Hop" and "hop" even when the search query is only "hop"
const {
rows,
} = await db.query(
"SELECT * FROM course WHERE header LIKE '%' || $1 || '%'",
[req.body.searchbar]
);
Use ILIKE:
SELECT * FROM course WHERE header ILIKE '%' || $1 || '%'
You can also express this with the ~~* operator:
SELECT * FROM course WHERE header ~~* '%' || $1 || '%'
I have a query in c# (with Oracle command and Oracle parameters). I want to use the input that user entered when getting data from database.
This is my query:
SELECT USER_ID, FIRSTNAME, LASTNAME
FROM USERS
WHERE USER_ID LIKE :userid || '%'
AND FIRSTNAME LIKE :firstname || '%'
AND LASTNAME LIKE :lastname || '%'
AND DEPARTMENT LIKE :department || '%'
AND TEAM LIKE :team || '%'
AND SUBTEAM LIKE :subteam || '%'
AND MACHINE LIKE :machine || '%'
User could be search entering just one input or all the inputs or none of the inputs.
Note: I've heard that, 'like%' doesn't work with the rows which has null column. But my database must has null columns. I need advice. What can I do?
For each parameter, if user input is given, the LIKE is evaluated. Otherwise user input IS NULL.
SELECT USER_ID, FIRSTNAME, LASTNAME
FROM USERS
WHERE (USER_ID LIKE :userid || '%' or :userid IS NULL)
AND (FIRSTNAME LIKE :firstname || '%' or :firstname IS NULL)
AND (LASTNAME LIKE :lastname || '%' or :lastname IS NULL)
AND (DEPARTMENT LIKE :department || '%' or :department IS NULL)
AND (TEAM LIKE :team || '%' or :team IS NULL)
AND (SUBTEAM LIKE :subteam || '%' or :subteam IS NULL)
AND (MACHINE LIKE :machine || '%' or :machine IS NULL)
Problem solved here is the solution ;
"SELECT USER_ID, FIRSTNAME, LASTNAME FROM USERS WHERE (USER_ID LIKE :userid || '%' OR USER_ID IS NULL) AND (FIRSTNAME LIKE :firstname || '%' OR FIRSTNAME IS NULL) AND(LASTNAME LIKE :lastname || '%' OR LASTNAME IS NULL) AND (DEPARTMENT LIKE :department || '%' OR DEPARTMENT IS NULL) AND (TEAM LIKE :team || '%' OR TEAM IS NULL) AND (SUBTEAM LIKE :subteam || '%' OR SUBTEAM IS NULL) AND (MACHINE LIKE :machine || '%' OR MACHINE IS NULL)"
If the user input is empty for missing value, try this
SELECT USER_ID, FIRSTNAME, LASTNAME FROM USERS
WHERE (USER_ID LIKE :userid || '%' OR :userid='')
AND (FIRSTNAME LIKE :firstname || '%' OR :firstname ='')
AND (LASTNAME LIKE :lastname || '%' OR :lastname='')
AND (DEPARTMENT LIKE :department || '%' OR :department='')
AND (TEAM LIKE :team || '%' OR :team='')
AND (SUBTEAM LIKE :subteam || '%' OR :subteam='')
AND (MACHINE LIKE :machine || '%' OR :machine='')
Then maybe you could compare NVL(..,'x')?
SELECT USER_ID, FIRSTNAME, LASTNAME FROM USERS
WHERE (nvl(USER_ID ,'¬') LIKE nvl(:userid , '¬')|| '%' )
AND (nvl(FIRSTNAME ,'¬') LIKE nvl(:firstname , '¬')|| '%' )
AND (nvl(LASTNAME ,'¬') LIKE nvl(:lastname , '¬')|| '%' )
AND (nvl(DEPARTMENT,'¬') LIKE nvl(:department , '¬')|| '%' )
AND (nvl(TEAM ,'¬') LIKE nvl(:team , '¬')|| '%' )
AND (nvl(SUBTEAM ,'¬') LIKE nvl(:subteam , '¬')|| '%' )
AND (nvl(MACHINE ,'¬') LIKE nvl(:machine , '¬')|| '%' )
;
I have a set of table as following
customer(cus_id,cus_first_name,cus_last_name);
insert into customer values ('c001', 'tan', 'wah khang');
I want to create a select statement to display the first name join with the last name.
Example :
tan wah khang
is that possible?
You can use the (this is not called "join" but) concatenation *|| (double pipe)* operator:
SELECT (cus_first_name || ' ' || cus_last_name) AS full_name
FROM customer
|| isn't quite equivalent to MySQL's CONCAT_WS. CONCAT_WS eliminates the delimiter if one of the operands is NULL. So if firstname is NULL and lastname is 'Smith', in MySQL:
CONCAT_WS(' ', firstname, lastname) returns "Smith"
whereas, in Oracle:
firstname || ' ' || lastname returns " Smith" (prepended with a space)
I'd love to know if there's a true equivalent, or if you'd have to write a stored procedure to emulate CONCAT_WS. It's terribly useful.
select cus_first_name || ' ' || cus_last_name from customer
Yes:
select cus_first || ' ' || cus_last from your_table;
In Oracle, PostgreSQL, DB2, Informix:
select cus_first_name || ' ' || cus_last_name from customer
In SQL-Server:
select cus_first_name + ' ' + cus_last_name from customer
In MS-Access:
select cus_first_name & ' ' & cus_last_name from customer
In MySQL:
select concat(cus_first_name , ' ', cus_last_name) from customer
In Informix:
select concatenate(cus_first_name,concatenate(' ',cus_last_name)) from customer
I have to concatenate 2 columns (ex. FIRSTANME and LASTNAME).
I do it this way:
FIRSTNAME || ' ' || LASTNAME`.
If one of them is null, but the other one is not null, I get null as concatenation result.
And I want following behavior
FIRSTNAME = null and LASTNAME = "Smith" ==>
FIRSTANME || ' ' || LASTNAME == ' Smith'.
How to solve this in DB2?
Use coalesce
...
CONCAT( COALESCE(firstname,'') , COALESCE(lastname,'') )
Or using the || concat operator
...
COALESCE(firstname,'') || COALESCE(lastname,'')
Note that IBM recomments using the keyword concat and not the || operator.
Concat: http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2.doc.sqlref%2Ffconc.htm
Coalesce: http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2.doc.sqlref%2Ffcoal.htm