SQL: How to combine WHERE and CONCAT?
By Josip Miskovic•
SQL Server, PostgreSQL, MySQL
To find a record that combines two or more fields in the WHERE clause, use the CONCAT method:
sql
SELECT * FROM EMPLOYEE where CONCAT(firstName, ' ', lastName) = 'John Doe';
For example, if we have a table of employee first and last names and we want to find an employee "John Doe", we can use "WHERE CONCAT" to find matches.
empId | firstName | lastName |
---|---|---|
0001 | Clark | Morris |
0002 | Dave | Invicita |
0003 | John | Doe |
Full Code Example
sql
-- create
CREATE TABLE EMPLOYEE (
empId int,
firstName varchar(100),
lastName varchar(100)
);
-- insert
INSERT INTO EMPLOYEE(empId,firstName, lastName) VALUES (0001, 'Clark', 'Morris');
INSERT INTO EMPLOYEE(empId,firstName,lastName) VALUES (0002, 'Dave', 'Invicita');
INSERT INTO EMPLOYEE(empId,firstName,lastName) VALUES (0003, 'John', 'Doe');
-- fetch
SELECT * FROM EMPLOYEE where CONCAT(firstName, ' ', lastName) = 'John Doe';
GO
About Josip
Josip Miskovic is a software developer at Americaneagle.com. Josip has 10+ years in experience in developing web applications, mobile apps, and games.
Read more posts →Published on:
On this page
Download Free Software Developer Career Guide
I've used these principles to increase my earnings by 63% in two years. So can you.
Dive into my 7 actionable steps to elevate your career.