SQL Server – Troubleshooting LIKE Pattern Match with CONCAT()

concatlikesql serverwhere

I am facing problem in a simple query script for SQL server. Please help

The script is

CREATE PROCEDURE [dbo].[Search_By_Author_And_Name] 
(
    @Author_name AS [varchar](50)
)
AS
BEGIN
    Select B.*, R.R_number From Book AS B , Rack As R
    where B.Author LIKE CONCAT('%', @Author_name, '%')
    AND B.B_Id=R.B_Id
END 
GO

The problem is this returns no columns. Even if I pass a valid argument(@Author_name) to the procedure it returns nothing. There must be a problem in the where clause in the usage of LIKE.

sample data in the table are

enter image description here

the first table is Book table and the other table is Rack table

I am calling the procedure by

EXEC Search_By_Author_And_Name 'Physics'

Please help

Thanks in advance

Best Answer

You are only matching on Author, not B_Name. Change code to:

CREATE PROCEDURE [dbo].[Search_By_Author_And_Name] 
(
    @Author_name AS [varchar](50)
)
AS
BEGIN
    Select B.*, R.R_number From Book AS B , Rack As R
    where (B.Author LIKE CONCAT('%', @Author_name, '%') OR
    B.B_Name LIKE CONCAT('%', @Author_name, '%'))
    AND B.B_Id=R.B_Id
END 
GO