I would like to combine FirstName
and LastName
into one column called 'FULL NAME'
and search with LIKE in SQL.
Example 1:
FirstName : Milan
LastName: PatelFullName: Milan Patel
Search with: Like '%SomeText%'
Example 2:
Title: " Meetings: A practical alternative to work."
Search: I would like to search the entire word within the give string. ex: work should return the above title.
Here is what I have so far.
SELECT
CE.Title, CC.FirstName + ' ' + CC.LastName AS FullName
FROM
EntryTable CE
JOIN
UserTable CC ON CE.EntryId = CC.USerId
WHERE
CC.FirstName LIKE 'n%'
I am getting it there slowly. search is working fine with this query.
SELECT
CE.Title
FROM
EntryTable CE
JOIN
UserTable CC
ON
CE.EntryId = CC.USerId
WHERE
CC.FirstName + ' ' + CC.LastName LIKE 's%'
BUT it only search for name starting with 's'
, i would like to search name starting, ending, contains conditions as well. how can i go about that. please help
You can use LIKE
with concatenated column values as below
WHERE
CC.FirstName + ' ' + CC.LastName LIKE 's%'
BUT it only search for name starting with 's', i would like to search name starting, ending, contains conditions as well. how can i go about that. please help
Then you have to use %
before and after search string like below
WHERE
CC.FirstName + ' ' + CC.LastName LIKE '%s%'
you can use concat
SELECT CE.Title, concat(CC.FirstName , ' ' , CC.LastName) AS FullName
FROM EntryTable CE
JOIN UserTable CC
ON CE.EntryId = CC.USerId
WHERE
concat(CC.FirstName , ' ' , CC.LastName) LIKE 'n%'
OR CC.FirstName LIKE 'n%' OR CC.FirstName LIKE '%n'
OR CC.LastName LIKE 'n%' OR CC.LastName LIKE '%n'
^^--// (1). ^^--// (2)
(1) to find lastname in first of the string you search .
(2) to find lastname in the end of the string you search.
(3)to find last name in middle of string do this '%n%'
you can't use the ALIAS
on the WHERE
clause because in the SQL Order of Operation, the WHERE
clause is executed before the SELECT
clause.
better use the complete concatenation is the WHERE
clause
WHERE CC.FirstName + ' ' + CC.LastName LIKE 'n%'
if you don't want to use that, wrap your query in a subquery so you can use the ALIAS
,
SELECT *
FROM
(
SELECT CE.Title, CC.FirstName + ' ' + CC.LastName AS FullName
FROM EntryTable CE JOIN UserTable CC
ON CE.EntryId = CC.USerId
) sub
WHERE sub.FullName LIKE 'n%'
其中CONCAT(CC.FirstName,'',CC.LastName)喜欢“%n%”
SELECT some_column_name FROM some_table where firstname + ' ' + lastname LIKE 'some_value'
不需要FullName
列。
将它们组合在WHERE子句中,带有两个%符号。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.