Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

I have a stored procedure where I have a variable that will be English or Arabic string, for Arabic string I need to add prefix N to the variable.

declare @ModuleName nvarchar(max)

set @ModuleName = 'أساسيات الكمبيوتر'

SELECT * FROM Module WHERE  NameEN LIKE N'%' + @ModuleName + '%' OR NameAR LIKE '%' + @ModuleName  + '%'


What I have tried:

Create function fnGetModuleIDByModuleName
(@ModuleName nvarchar(max))
returns int
begin

declare @ModuleID int

SELECT @ModuleID = ID FROM Module where NameEN like '%' + @ModuleName + '%' or NameAR like '%' + @ModuleName + '%'

return @ModuleID

end


Calling function
declare @m nvarchar(max) 
set @m = 'أساسيات الكمبيوتر'
select dbo.fnGetModuleIDByModuleName(@m)


if I add prefix N to the string then it will work
declare @m nvarchar(max) 
set @m = N'أساسيات الكمبيوتر'
select dbo.fnGetModuleIDByModuleName(@m)


I can't hardcode any variable, the variable value is coming from the parameter of the stored procedure. So, my question is, how can I add prefix N to the @variable?


Can anyone please help me.

Thanks in advance
Posted
Updated 23-Oct-19 3:01am
v2
Comments
Richard Deeming 23-Oct-19 11:06am    
Just add the prefix to the variable. What's the problem?
declare @ModuleName nvarchar(max)

set @ModuleName = N'أساسيات الكمبيوتر'

SELECT * FROM Module WHERE  NameEN LIKE N'%' + @ModuleName + N'%' OR NameAR LIKE N'%' + @ModuleName  + N'%'

1 solution

Try to move the % to where the parameter is added.
See this question: How to search value from SQL data base using %[^]

scCommand.Parameters.Add("@ModuleName", SqlDbType.NVarChar, 50).Value = "ساسيات الكمبيوتر";
 
Share this answer
 
v4
Comments
abdul subhan mohammed 23-Oct-19 8:14am    
the problem is not the '%' but the variable contains Arabic text for which I have to add prefix N to the variable.

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