SQL Concat and merge values - sql

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.

Related

Concatenating Full Names in Microsoft SQL Server

Suppose I have a table with the following information
Title FirstName MiddleName LastName Suffix
======|============|=============|============|======
Mr. John Fitzgerald Kennedy NULL
NULL John NULL Doe III
I want to create a query to output the full name of the above information. For example, I want the output to look like
Mr. John Fitzgerald Kennedy
John Doe, III
I was thinking of using the concat() function, but I am unsure how to go about this to ensure I don't have extra spaces or hanging commas in the names.
This is a little tricky. SQL Server 2017+ supports concat_ws(). However, you still need a comma for the suffix. Forturnately, concat() ignores NULLs:
select concat(concat_ws(' ', title, firstname, middlename, lastname),
', ' + suffix
)
Earlier versions require a bit more work to avoid multiple spaces when there are NULL values:
select ltrim(concat(' ' + title,
' ' + firstname,
' ' + middlename,
' ' + lastname,
', ' + suffix
)
)
This uses the fact that + returns NULL when an argument is NULL. But, concat() ignores it.
Note: Neither probably does what you want if all the names are NULL but the suffix is present. However, I doubt that is an issue in your data.
Here is a db<>fiddle.

SQL concatenate columns for search

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,'%')

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.

Trouble using COALESCE function in sql

I am attempting to do the following:
SELECT forename,
forename2,
surname,
(SELECT (COALESCE(LEFT(forename, 2)),"") +
(COALESCE(LEFT(forename2, 1)),"") +
(COALESCE(LEFT(surname, 1)), "") as Mnemonic)
from Persons
trying to get, first 2 letters of forename, first letter of forename2(if NOT null), and first letter of surname if not null, with "" empty strings where a null is present.
any help would be much appreciated,
Regards, Bserk
Your question doesn't say what's wrong with your current code, but I'm going to guess you are getting an error because you're using double-quotes instead of single quotes, and your brackets don't match.
You can also simplify this query somewhat by removing the inner select.
Try this:
SELECT forename, forename2, surname,
COALESCE(LEFT(forename, 2),'') +
COALESCE(LEFT(forename2, 1),'') +
COALESCE(LEFT(surname, 1),'') as Mnemonic
from Persons
Use single quotes instead of double for string literals and count your braces:
SELECT
forename,
forename2,
surname,
COALESCE(LEFT(forename, 2),'') + COALESCE(LEFT(forename2, 1),'') + COALESCE(LEFT(surname, 1), '') as Mnemonic
from Persons
Looks like "trouble" you've mentioned is because using of wrong parenthesis and double quotes.
Following query returns 1123
SELECT COALESCE(LEFT(null, 2), '')
+ COALESCE(LEFT('1111', 2), '')
+ COALESCE(LEFT('2222', 1),'')
+ COALESCE(LEFT('3333', 1), '')
The statement will not even compile as it is.
You don't need the second SELECT statement as you are not doing a subquery.
You need to use single braces. Double braces are used for object names (ie tables, procedures etc).
Too many parentheses
Try:
SELECT forename, forename2, surname,
COALESCE(LEFT(forename, 2),'') +
COALESCE(LEFT(forename2, 1),'') +
COALESCE(LEFT(surname, 1), '') as Mnemonic
from Persons

SQL concatenating strings?

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.