Reputation: 73
I used this query to make a table NewAddData
select (
FirstName +' '+
coalesce(MiddleInitial + '''', '') +
' ' +
Lastname
) as Name
into NewAddData
from c_client c;
I'm getting the following output if I do
Select * from NewAddData
I want to get name which has apostrophe present in it. The Select Statement is working as
Select Name from AddData where Name like '%Paige%' and Name like '%''%'
And I'm getting the output as Paige A'Harrell
ALTER PROCEDURE GetName @ClientName Varchar(max)
AS
BEGIN
SET NOCOUNT ON;
Select Name from AddData where Name like '%@ClientName%' and Name like '%''%'
END
GO
I'm trying to do this in Stored Procedure, but it is not working. I want to pass any part of the name as parameter and if it has apostrophe, I want to get that name as output. If that name doesn't have apostrophe, it should show that name is not present. Please help.
Upvotes: 1
Views: 51
Reputation: 5893
ALTER PROCEDURE GetName @ClientName Varchar(max)
AS
BEGIN
SELECT NAME FROM ADDDATA WHERE NAME IN
(
SELECT TOKEN
FROM UDF_SPLITSTRING(@CLIENTNAME, ',')
)
END
GO
Upvotes: 0
Reputation: 1172
write '%'+@ClientName+'%' inplace '%@ClientName%'
ALTER PROCEDURE GetName @ClientName Varchar(max)
AS
BEGIN
SET NOCOUNT ON;
Select Name from AddData where Name like '%'+@ClientName+'%' and Name like '%''%'
END
GO
Upvotes: 1
Reputation: 5453
You can do it like below :
ALTER PROCEDURE GetName @ClientName varchar(100)
AS
BEGIN
SET NOCOUNT ON;
if exists(select [name] from AddData where [name] LIKE concat('%', @ClientName, '%') and [name] like '%''%')
begin
select @ClientName
end
else
begin
select @ClientName + ' is not found'
end
END
Upvotes: 0