SQL concatenating strings? - sql

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.

Related

SQL Concat and merge values

How to make it correct, whats wrong with this?
SELECT Name, Surname, CONCAT(NAME + ' '+ SURNAME) 'name and surname'
FROM Worker
WHERE Born = '1988';
or
SELECT Name, Surname, 'NAME' + ' '+ 'SURNAME' AS 'name and surname'
FROM Worker
WHERE Born = '1988';
The standard SQL operator for concatenation is ||, so you want:
SELECT Name, Surname, (NAME || ' ' || SURNAME) as full_name
FROM Worker
WHERE Born = '1988';
Many databases also support a CONCAT() function, although logically you want at least two arguments. SQL Server uses + for string concatenation, but that is not widespread.
Your syntax is wrong in the two queries:
SELECT Name, Surname, CONCAT(NAME, ' ', SURNAME) "name and surname"
FROM Worker
WHERE Born = '1988';
or
SELECT Name, Surname, NAME + ' '+ SURNAME AS "name and surname"
FROM Worker
WHERE Born = '1988';
Suposing you are using Microsoft SQL Server, keep in mind there are actually few differences between regular + operator and concat() function.
concat() will implicitly converts any parameter to a string while + will throw an error if you mix strings and other types
concat() will always return a not null string even if all parameters are null. + will return null as soon as any column is null.

SQL Concatenation Query - Four As in Concatenated Name

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;

Performance Tuning of SQL query for DB2 Z/oS with like predicate(Pattern Match)

Please help me in tuning the performance of below query or suggest any alternate logic.
Select FNAME, MNAME, SURNAME, DOB, ADDRESS, PHONE
from INDIVIDUAL_DATA
WHERE DOB = V_DOB
AND (SURNAME = V_SURNAME
OR (SURNAME LIKE '%' || ' ' || V_SURNAME)
OR (SURNAME LIKE V_SURNAME || ' ' || '%')
OR (SURNAME LIKE '%' || ' ' ||
V_SURNAME || ' ' || '%'));
V_SURNAME is variable having surname input and V_DOB is having input for DOB(Date Of Birth).
I have created an index using SURNAME and DOB column.
This query is part of an Stored Procedure. and this query will be the first query which executes on invoking the Stored Procedure.
I have around 7 Million records in DB2 database on which this query will be executed.We are facing performance issue, its taking much time. I suspect because of Like Predicate with OR operator is the cause of issue.
This logic is implemented to perform Pattern search. Please have a look into below test cases :
case 1.
DOB= 1992-10-10 and SURNAME = 'ALEX MATHEWS'
V_DOB = '1992-10-10' and SURNAME = 'ALEX'
this should find a positive match.
case 2.
DOB= 1965-05-10 and SURNAME = 'FRANKLIN JERRY'
V_DOB = '1965-05-10' and V_SURNAME = 'FRANK'
This should not be fetched. its a negative case.
Try the LOCATE function:
AND LOCATE(' '||V_SURNAME||' ', ' '||SURNAME||' ') > 0
DOB column should be the leading column of the index.

Obtain first name, last name with unknown number of space

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
)

How to extract the last name from firstname.lastname#email.com using Oracle?

I need to compare the value of a column (LASTNAME) with a system variable (:VARIABLE), but the variable is an email address, so I need to trim off the "#email.com" and "firstname." Some things I've tried:
select *
from TABLENAME
where LASTNAME LIKE :VARIABLE
select *
from TABLENAME
where LASTNAME IN :VARIABLE
I've been able to trim off the #email.com, can't figure out how to trim off FIRSTNAME. at the same time.
Regular expressions can help:
SQL> SELECT LTRIM(regexp_substr('firstname.lastname#abc.com','\.[^#]*'),'.') last_name from dual;
LAST_NAME
---------
lastname
Your query could then look like:
SELECT *
FROM tablename
WHERE UPPER(LTRIM(regexp_substr(:VARIABLE,'\.[^#]*'),'.')) = UPPER(lastname);
You can use a combination of SUBSTR and INSTR.
The instr function returns the location of a substring in a string. With this one you can locate the "."or the "#" char.
Then use the substr to get the substring from the begining to the previous located position
SELECT SUBSTR('Take the first four characters', 1, 4)
Use:
SELECT t.*
FROM TABLE t
WHERE t.lastname LIKE '%' || SUBSTR(:VARIABLE,
INSTR(:VARIABLE, '.') +1,
INSTR(:VARIABLE, '#')) || '%'
Add UPPER or LOWER to both sides if you need to be sure of case insensitive matching.
Reference:
SUBSTR
INSTR