Skip to content

Instantly share code, notes, and snippets.

@HamidMolareza
Last active November 19, 2023 09:16
Show Gist options
  • Save HamidMolareza/be281d2be2d3d447d604c6a32597711c to your computer and use it in GitHub Desktop.
Save HamidMolareza/be281d2be2d3d447d604c6a32597711c to your computer and use it in GitHub Desktop.
CONCAT vs CONCAT_WS
# https://quera.org/college/8939/chapter/32512/lesson/108911/
-- Create a sample table
CREATE TABLE sample_table
(
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
-- Insert sample data
INSERT INTO sample_table (first_name, last_name)
VALUES ('John', 'Doe'),
('Jack', NULL),
('Bob', 'Johnson');
select *
from sample_table;
-- Test CONCAT
SELECT id,
CONCAT(first_name, ' ', last_name) AS full_name_concat
FROM sample_table;
-- Test CONCAT_WS
SELECT id,
CONCAT_WS(' ', first_name, last_name) AS full_name_concat_ws
FROM sample_table;
@HamidMolareza
Copy link
Author

CONCAT output:

ID Full Name
1 John Doe
2
3 Bob Johnson

CONCAT_WS output:

ID Full Name
1 John Doe
2 Jack
3 Bob Johnson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment