选择LIKE或CHARINDEX()函数以使用SQL查找一些特殊的unicode字符

时间:2014-10-30 06:18:32

标签: sql sql-server unicode

我的第二个和第三个查询没有输出任何记录有什么问题,而第一个查询输出一条记录。

查询1:

DECLARE @FullName nvarchar(50)
DECLARE @FULLNamelike nvarchar(50)
SET @FullName = 'NGUYỄN THỊ DIỆU'
SET @FULLNamelike = N'%' + @FullName + '%'

SELECT HR_CInformation.[CandidateId]
    ,HR_CInformation.[FullName]
    ,HR_CInformation.[Email]
FROM [dbo].[IT_HR_Candidate_Information] HR_CInformation
WHERE (HR_CInformation.[FullName] like N'%NGUYỄN THỊ DIỆU%' OR @FULLNamelike = '')
  • 结果:21NGUYỄNTHỊDIỆUaddieunt@gmail.com

查询2:

DECLARE @FullName nvarchar(50)
DECLARE @FULLNamelike nvarchar(50)
SET @FullName = 'NGUYỄN THỊ DIỆU'
SET @FULLNamelike = N'%' + @FullName + '%'

SELECT HR_CInformation.[CandidateId]
    ,HR_CInformation.[FullName]
    ,HR_CInformation.[Email]
FROM [dbo].[IT_HR_Candidate_Information] HR_CInformation
WHERE (HR_CInformation.[FullName] like @FULLNamelike OR @FULLNamelike = '')

查询3:

DECLARE @FullName nvarchar(50)
DECLARE @FULLNamelike nvarchar(50)
SET @FullName = 'NGUYỄN THỊ DIỆU'
SET @FULLNamelike = N'%' + @FullName + '%'

SELECT HR_CInformation.[CandidateId]
    ,HR_CInformation.[FullName]
    ,HR_CInformation.[Email]
FROM [dbo].[IT_HR_Candidate_Information] HR_CInformation
WHERE (CHARINDEX(@FULLNamelike, HR_CInformation.[FullName]) > 0 OR @FULLNamelike = '')

1 个答案:

答案 0 :(得分:0)

这是因为您的unicode字符被转换为问号。

因此,当您声明并为变量N

指定unicode值时,确实需要使用FullName

尝试此查询以澄清:

DECLARE @FullName nvarchar(50)
DECLARE @FullNameO nvarchar(50)
DECLARE @FullNamelike nvarchar(50)
DECLARE @FullNameOkay nvarchar(50)
SET @FullName = 'NGUYỄN THỊ DIỆU'
SET @FullNameO = N'NGUYỄN THỊ DIỆU'
SET @FullNamelike = N'%' + @FullName +'%'
SET @FullNameOkay = N'%' + @FullNameO +'%'
SELECT @FullNamelike, N'%NGUYỄN THỊ DIỆU%', @FullNameOkay

SQLFiddle:http://sqlfiddle.com/#!3/d41d8/40582