问题
Why is this query giving me ???????? as output.
Declare @Search NVARCHAR(MAX) = 'اختبار كتاب اللغة العربية'
SET @Search = N''+ @Search
select @Search
Where as this one works fine :-
Declare @Search NVARCHAR(MAX) = N'اختبار كتاب اللغة العربية'
select @Search
I want to make my query dynamic. Is there any other way I can do it?
Dynamic in the sence I can pass anything in the @Search parameter. I have a .NET application where I will pass the string to be searched in the @Search parameter which can be either English or Arabic.
回答1:
Because you need to prefix a string literal with N to identify it as a Unicode string, otherwise it is treated at though it only contains single-byte characters.
Can you elaborate on "I want to make my query dynamic"?
Declare @Search NVARCHAR(MAX) = 'اختبار كتاب اللغة العربية'
When you execute this line it is treating the string literal as a single-byte literal and assigning it to the variable @Search. The variable is capable of storing a unicode string (but you didn't assign one to it). By the time you reference it later, the text has already been modified and you have already lost some of the encoding and it can't be recovered. You have to prefix the literal with N at the point you assign it to the variable.
Is there are reason why you can't put that prefix in your script?
来源:https://stackoverflow.com/questions/30097213/select-query-giving-output-as-with-different-language-text