I am trying to create a view in SQL. We will call this view "A7T8". The view will pull first and last names from the database, and concatenate the first and last names into a field called FullName. However, I only want to select the concatenated names that have a total of at least four As in their first and last names. (Rebecca Aaronson would not be displayed since she only has 3 As, but Harry Flanagan would be displayed.
Currently my code is:
CREATE VIEW A7T8 AS
SELECT FNAME || ' ' || LNAME AS FULLNAME
FROM A7
WHERE Upper(LNAME) LIKE '%A%A%A%A%' OR Upper(FNAME) LIKE '%A%A%A%A%'
ORDER BY LNAME, FNAME;
But this only pulls names that have four As in the first name or four As in the last name. I want it to pull names that have a total of at least four As in the concatenated full name. How do I do this?
You can simply write:
CREATE VIEW A7T8 AS
SELECT FNAME || ' ' || LNAME AS FULLNAME
FROM A7
WHERE UPPER(FNAME || ' ' || LNAME) LIKE '%A%A%A%A%'
ORDER BY LNAME, FNAME;
You can try the below approach
CREATE VIEW A7T8 AS
select FNAME + ' ' + LNAME AS FULLNAME
from [A7]
where len(FNAME + LNAME) - len(Replace(Upper(FNAME) + Upper(LNAME),'A','')) >= 4
ORDER BY LNAME, FNAME;
I think Concat do the job, you must use it in a query not with the designer
CREATE VIEW [dbo].[DEMO_VIEW]
AS
SELECT Concat('IND_', LIBELLE) as SECONDLIBELLE,
CODE as SECONDCODE
from dbo.DEMOTABLE
GO
Use a REGEX. This will match first last with 1-4 A's
CREATE VIEW A7T8 AS
SELECT FNAME || ' ' || LNAME AS FULLNAME
FROM A7
WHERE CONCAT(Upper(LNAME), " ", Upper(FNAME)) REGEX '[A]{1,4}'
ORDER BY LNAME, FNAME;
Related
I am not usually an SQL person but need to find matching name/surnames from a list of people on MSSql. I am trying to find similar names from TABLENAME to match the list of people that I have. The line looks like this:
Select *
from TABLENAME(nolock)
Where SomeRule='04' and RTRIM(Name) +' ' + RTRIM(SecondName) +' '+RTRIM(Surname) in (THE LIST OF PEOPLE HERE)
But this method only gives me the match of people with second names. If someone does not have a second name but their name+surname match, it does not show up. I want to see people who have 100% match of name-second name-surname OR name-surname (if they don't have second name).
Thank you guys in advance
Provided list of people is a table
Select distinct t.*
from TABLENAME t
join [THE LIST OF PEOPLE HERE] lp on SomeRule='04'
and RTRIM(t.Name) = lp.Name
and RTRIM(t.Surname) = lp.Surname
and (t.SecondName is null and pl.SecondName is null or RTRIM(t.SecondName) = lp.SecondName)
Let's look at some rows:
Name
SecondName
Surname
Remark
Your concatenation
'John'
'Thomas'
'Smith'
Person with middle name
'John Thomas Smith'
'John'
''
'Smith'
Person that has no middle name
'John Smith'
'John'
null
'Smith'
Person with an unknown middle name
null
The person without a middle name cannot be found because of two blanks separating first and last name in your created string. The person of whom we don't know the middle name cannot be found because the concatenated string is null.
You can use CASE WHEN to check this and react accordingly:
select *
from tablename
where somerule = '04'
and
rtrim(name) + ' ' +
case when rtrim(secondname) is null then ''
when rtrim(secondname) = '' then ''
else rtrim(secondname) + ' '
end +
rtrim(surname) in (the list of people here)
I was looking for advice on the best way to implement search functionality over several columns.
I have a SQL server with columns for:
title
firstname
middlenames
lastname
maidenname
If a user searches for "John Smith" I need to look through all the columns for those values. Obviously, none of those columns will contain both parts of "John" and "Smith". I previously added the columns together eg
select * from PEOPLE where (firstname + ' ' + middlenames + ' ' + lastname) = %#SEARCHVALUE%
However this prevents certain searches returning values because the wildcard does not match the search criteria.
If one of your fields can have null value you should do CONCAT() them as well.
SELECT * FROM PEOPLE WHERE CONCAT(firstname, ' ', middlenames, ' ', lastname) LIKE CONCAT('%',#SEARCHVALUE,'%')
SELECT name FROM clients;
Table clients
id | name |
1 John
2 John Bravo
3 John Alves
4 Jo
In postgres, how can I select only names with more than one word? For example. This should be the output:
John Bravo
John ALves
I think just test if the name contains a space: where name like '% %'
But this will give you some problem if you name can contain space char befor or after the name, like: ' JOHN' or 'Luck '
If word means to you a space delimited token, then the following would do the trick:
SELECT name FROM clients WHERE name LIKE '% %';
But this will also give you those that have empty names made out of spaces only. Also performance-wise, this will be a costly query.
First you may have to find the number of words in the column, then only select where the number of words is greater than 1.
For example:
SELECT LENGTH(name) - LENGTH(REPLACE(name , ' ', ''))+1 FROM clients;
This will return the number of words in each row, which we have in the field name.
Then you can proceed putting this in a nested select SQL statement something like :
SELECT LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1 as mycheck, name FROM clients where (SELECT LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1)>1
This will return only where words are greater than 1 (>1). Else you can user >2 to return columns with more than 2 words.
LIKE is an option, or you can use split_part:
SELECT name FROM clients WHERE split_part(trim(name), ' ', 2) <> ''
I'm new in VB.NET programming but I have to do a research bar in my program. Actually, the search bar is looking for first name or for last name (If first name or last name have space it's working) but if I want to search for both, how can I build my sql query? I don't know if the user will put first name first or last name first. What I've tried is to split the names if there is a space but if the person have a name like 'Robert Downey Junior', how can I know which part of the space will be the first name or the last name?
So,
how can I build my sql query for this task?
how can I know which part of the space will be the first name or the last name?
Actual regex
' value of query = "% + search bar + %" for sql LIKE parameter
Regex.IsMatch(query, "^'%(\w+ \w+)+%'$")
How I detect the first and last names
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"
Actual sql selector
SELECT id, firstName, lastName
FROM beneficiaire
WHERE LOWER(lastName) LIKE '%robert downey junior%'
OR LOWER(firstName) LIKE '%robert downey junior%'
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
)
I have a query that takes input from the end user and compares it to 2 columns individually, and the two columns concatenated.
SELECT f_name, l_name, (f_name + ' ' + l_name) AS full_name
FROM users_table
WHERE f_name = user-input
OR l_name = user-input
OR 'full_name' = user-input
Excuse the massive syntax fail, but I assure you it's correct in the program.
It is written in PHP, querying a SQL SERVER 2005 database, and is case insensitive.
Typing one name (first or last) will return correct results, but typing first, then space, then last, returns an empty set.
Any ideas?
that is because 'full_name' is a literal and not a column name, it does not exists at the WHERE clause level
you need to add
OR f_name + ' ' + l_name = user-input
instead of
OR 'full_name' = user-input
Under the "teach a man to fish" theory ...
You cannot use a column alias in a where or group by clause. You can use it in an order by clause. To use a column alias the way you want to, you would have to do something like ...
SELECT [Full Name] FROM
(SELECT f_name, l_name, f_name + ' ' + l_name AS [Full Name]
FROM users_table)
WHERE [Full_Name] = #paramVal
'full_name' is a literal string not the column...
(f_name + ' ' + l_name) = user-input
This will work:
SELECT *
FROM users_table
WHERE CONCAT(first_name,' ',last_name) = user-input;
try:
SELECT f_name,
l_name,
(f_name + ' ' + l_name) AS full_name
FROM users_table
WHERE f_name = user-input
OR l_name = user-input
OR f_name + ' ' + l_name = user-input
For starters, remove the single quotes from around the full_name in the last where criteria. Now the user-input is compared to the string literal 'full_name'.
The concatenation part should be a-ok!
The problem is in this line:
OR 'full_name' = user-input
Provided that's actually what you have written down, you are comparing the literal string "full_name" to the provided input. Putting single quotes around something treats it as a literal. Try the following:
SELECT f_name, l_name, (f_name + ' ' + l_name) AS full_name
FROM users_table
WHERE f_name = user-input
OR l_name = user-input
OR full_name = user-input
Make sure that the user enters "full_name" (without the quotes) as the user-input and the database will return some rows.