Click here to Skip to main content
15,900,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am searching nvarchar text in my stored procedure. nvarchar text to be searched is passed as parameter.

dummy table

DECLARE @tb TABLE (Id INT, Name NVARCHAR(100));

insert into @tb VALUES (1,'Aman');
insert into @tb VALUES (2,N'سانديب');
insert into @tb VALUES (3,'Anuj');
insert into @tb VALUES (4,N'فيكاس');
insert into @tb VALUES (5,N'اليت');
insert into @tb VALUES (6,'Sunil');
insert into @tb VALUES (7,'Ajay');
insert into @tb VALUES (8,N'فيشال');
insert into @tb VALUES (9,'Tarun');

--select * from @tb


DECLARE @str NVARCHAR(50)=N'سانديب'
SELECT * FROM @tb WHERE Name LIKE '%'+ @str +'%'

--It is working fine

I know we need to prefix N' to search nvarchar text. But how can i add this in parameter to stored procedure

What I have tried:

DECLARE @str1 NVARCHAR(50)='سانديب'
SELECT * FROM @tb WHERE Name LIKE '%'+ @str1 +'%'

it is not working

DECLARE @str2 NVARCHAR(50)='سانديب';
SELECT * FROM @tb WHERE Name LIKE '%' + N'' + @str2 + '%'

it is still not working
Posted
Updated 19-Jan-18 5:54am

Try this

SQL
CREATE PROCEDURE SEARCHTEXT

      -- Add the parameters for the stored procedure here

      @TEXT NVARCHAR(50)

AS

BEGIN

DECLARE @str1 NVARCHAR(50)
SET @str1=N'' + @TEXT 
SELECT * FROM @tb WHERE Name LIKE '%'+ @str1 +'%'
 
Share this answer
 
v2
Comments
xpertzgurtej 18-Jan-18 4:08am    
& cant be used for concatenation. Showing error :The data types nvarchar and nvarchar are incompatible in the '&' operator.
RDBurmon 18-Jan-18 6:32am    
ohh yeah , that should be +
RDBurmon 18-Jan-18 6:33am    
updated my solutions. if worked please accept and vote
xpertzgurtej 18-Jan-18 7:24am    
No..it is not working as per ur solution:
DECLARE @tb TABLE (Id INT, Name NVARCHAR(100));

insert into @tb VALUES (1,'Aman');
insert into @tb VALUES (2,N'سانديب');
insert into @tb VALUES (3,'Anuj');
insert into @tb VALUES (4,N'فيكاس');
insert into @tb VALUES (5,N'اليت');
insert into @tb VALUES (6,'Sunil');
insert into @tb VALUES (7,'Ajay');
insert into @tb VALUES (8,N'فيشال');
insert into @tb VALUES (9,'Tarun');


DECLARE @str NVARCHAR(50)='سانديب'

DECLARE @str1 NVARCHAR(50)
SET @str1=N'' + @str
SELECT * FROM @tb WHERE Name LIKE '%'+ @str1 +'%'

no results
Try:
SQL
DECLARE @str1 NVARCHAR(50) = N'سانديب' -- Add the missing "N" prefix here
SELECT * FROM @tb WHERE Name LIKE N'%'+ @str1 + N'%' -- And to the two "%" strings here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900