MYSQL merge columns - sql

I'm using MySQL and do a select:
SELECT LTRIM(Firstname + ' ' Lastname) AS Fullname FROM Persons
My result is 0 for every result.
Even if i remove the LTRIM, Using CONCAT is giving the same problem.

You are arithmetically adding the string values together; unless you have "1ohn 5mith" in the db, this will always be 0.
Does SELECT LTRIM(CONCAT(Firstname,' ',Lastname)) AS Fullname FROM Persons give you the same problem? (note that there are 3 parameters to CONCAT() here: Firstname, a one-character string containing a space, and Lastname; this function takes as many arguments as you throw at it and outputs them as a string)

Related

SQLite Studio Question for Substr making some of my values disappear?

I'm working with a free dataset from Kaggle to learn SQL further and I've hit a spot where I am stuck. I'm working with an NFL Draft dataset that has player names listed like this:
FirstName LastName\UserName
However, some of the rows are simply this:
FirstName LastName
I wrote this code and have had some success:
SELECT position, substr(Player,0,instr(Player,'\')) AS Player_Name
This specfic code works great on any rows that are formatted like FirstName LastName\UserName but for any rows that are formatted like this FirstName LastName it returns a blank for the Player_Name.
Any tips on how I can fix this to show the FirstName LastName ONLY on my query for both ways?
INSTR returns 0 if the char is not in the string
Supplying a length of 0 causes an output from SUBSTR of zero length string
This is why your name disappears
What we can do is convert the 0 to something else:
SUBSTR(player, 0, COALESCE(NULLIF(INSTR(player, '/'), 0), 9999)
Now if INSTR returns 0, NULLIF converts it to NULL, and COALESCE converts the NULL to 9999
SQLite doesn't care that 9999 is beyond the end of the string (make it bigger if it isn't) and returns the whole rest of string, so effectively the SUBSTR is a non-op
There are other ways to skin the cat:
CASE WHEN player LIKE '%/%' THEN SUBSTR(...) ELSE player END
but they might search the string twice, for lower overall performance.. It might not matter, if it's a handful of values queried infrequently, you might prefer a more readable form over a (theoretically) more performant one

Combine two columns and perform a where operation on the result

I have two columns, one for 'firstName' and one for 'lastName'. I'm trying to perform a where operation on the concatenation of those two columns to match for a full name.
Here is what I've tried:
select * from table where concat_ws(' ', 'firstName', 'lastName') like '%some_value%'
This just returns a blank array.
Example Data:
firstName
lastName
Clayton
Smith
Barbara
Clayman
Sam
Peterson
when some_value = Clay, it should return 'Clayton Smith' and 'Barbara Clayman'
I got a lot of this syntax from some other stackoverflow answers which were marked as solutions to a similar problem, however when implementing this myself I couldn't get it to work.
FWIW I'm going to be building these queries with knex.js so if there's a knex.js specific solution feel free to answer with that.
The arguments to your CONCAT_WS call should be in double quotes. If they're in single quotes, Postgres will interpret them as string literals. This should work:
SELECT * FROM table WHERE CONCAT_WS(' ', "firstName", "lastName") ILIKE '%some_value%'
I also recommend using ILIKE rather than LIKE - this will make the pattern matching for your search term be case insensitve.
use STR_POS
The PostgreSQL strpos() function is used to find the position, from where the substring is being matched within the string.
https://w3resource.com/PostgreSQL/strpos-function.php#:~:text=The%20PostgreSQL%20strpos()%20function,being%20matched%20within%20the%20string.&text=Example%20of%20PostgreSQL%20STRPOS(),within%20the%20argument%20is%205.
select * from table where strpos(concat_ws(' ', first_name, last_name),'Clay') > 0
Replace literal with variable

LEN(firstName) + LEN(lastName) does not equal LEN(firstName + lastName)

I'm trying to search a table based on the concatentation of the firstName and lastName columns. Both of these are defined as NVARCHAR(50) NOT NULL
The query sometimes fails to find a match because the concatenated column is padded with extra spaces. Here's the query:
SELECT firstName + lastName AS fullName, LEN(firstName) + LEN(lastName) AS realLength, LEN(firstName + lastName) AS concatLength FROM UsersTable
And here's an image with the results:
What is the deal with this? How can I avoid the extra spaces? If I do SELECT RTRIM(firstName) + RTRIM(lastName) ... I get the correct full name with no extra spaces, but using RTRIM is too expensive because my data set is very big. This would lead me to think that the issue is the data itself, except that LEN(firstName) is the same as LEN(RTRIM(firstName))
You have spaces at the end of your FirstName. It is easy enough to check that the following returns 4:
select len(N'abcd ')
This is a property of the varchar() data types and len(). Of course, when you concatenate them, then SQL Server decides to recognize the spaces at the end.
This behavior is documented in the "Remarks" section of the documentation:
Remarks
LEN excludes trailing blanks. If that is a problem, consider using the
DATALENGTH (Transact-SQL) function which does not trim the string. If
processing a unicode string, DATALENGTH will return twice the number
of characters.
As the comments suggest, you can ltrim()/rtrim() the values before concatenating them. Or, use like.

doesn't return a row when using combined string columns in where clause in mssql

Hi I have a dynamic query that filters the main query
All is working fine except that when i try to use a combined string column it doesn't return any row. Is there anything I need to do make this work? I try to use alias in having clause but it doesn't work too
Try using CONCAT() like:
... CONCAT(Employees.emp_last_name, ', ', Employees.emp_first_name) ...
Your query result show the name is Parungao, Mark Anthony (without space in the beginning), but your query condition is like '% '
Remove space after % in your query.
and Employees.emp_last_name + ', ' + Employees.emp_first_name like '%Parungao, Mark Anthony%'
query for firstname and lastname seperatly
and Employees.emp_last_name like '%Parungao%'
and Employees.emp_first_name like '%Mark Anthony%'
looking closely at your picture, i'd say, you have a few spaces after Parungao.

SQL Aliases ',' and + function

What is the function of ',' and + on the given following SQL script?
SELECT CustomerName, Address+', '+City+', '+PostalCode+', '+Country AS Address
FROM Customers;
' is the delimiter for a string literal. ', ' is a string containing a comma and a blank.
So obviously this query selects CustomerName plus a concatenation of address and a comma and a blank and city and a comma and a blank and country, calling this new string Address.
The DBMS in question accepts a + for string concatenation. Either additionally to or instead of the standard operator || .
This query will give you two columns: CustomerName and Address.
The Address will be composed of: Address, City, PostalCode and Country seperated by comma (,)
The + sign is used for concatenating strings in this specific SQL query.
Some DBMS like (MySql and Sybase) use the + for concatenation of strings.