我是VB.NET编程的新手,但我必须在我的程序中做一个研究吧。实际上,搜索栏正在查找名字或姓氏(如果名字或姓氏有空格,那么如果我想搜索它们,我该如何构建我的SQL查询?我不知道用户是先将名字放在名字的第一个或最后一个名字。我试过的是如果有空格就拆分名称,但如果这个人的名字有'罗伯特唐尼少年',我怎么知道这个空间的哪一部分将是名字或姓氏?
所以,
实际正则表达式
' value of query = "% + search bar + %" for sql LIKE parameter
Regex.IsMatch(query, "^'%(\w+ \w+)+%'$")
我如何检测名字和姓氏
Dim name As String = query.ToString.Substring(2, query.Length - 4)
Dim nameParts As String() = name.Split(" ")
'So nameParts(0) = "robert", nameParts(1) = "downey" and namePart(2) = "junior"
实际的sql选择器
SELECT id, firstName, lastName
FROM beneficiaire
WHERE LOWER(lastName) LIKE '%robert downey junior%'
OR LOWER(firstName) LIKE '%robert downey junior%'
答案 0 :(得分:0)
CREATE PROCEDURE SEARCH( @SearchName VARCHAR(50)) AS
DECLARE @name VARCHAR(52)
SET '%' + @SearchName + '%'
SELECT id,
firstName,
lastName
FROM beneficiaire
WHERE (
LOWER(lastName) + ' ' + LOWER(firstName) LIKE @name
) OR
(
LOWER(firstName) + ' ' + LOWER(lastName) LIKE @name
)