Display from two column ComboBox - ms-access

I have a lastName, firstName sorted ComboBox. After having made a selection from this ComboBox only lastName is displayed. But I need both lastName AND firstName being displayed.
What settings do I have to set in order to display both values?

On combobox properties:
set "Column Count" to 3
set "Column Widths" to 0;0;1
set your "Row Source" to get 3 columns, for example query:
SELECT LastName,FirstName,FirstName + ' ' + LastName AS FirstAndLastName FROM Table1 ORDER BY LastName,FirstName

Related

I'm trying to select two columns as one column in MySQL but output is not coming

I'm trying to use the concatenation operator( || ) to select two columns as one in MySQL. But, the output is not coming.
The table 'emp' has four columns named -> eid (int), fname (varchar(20)), lname (varchar(20)) and salary (float).
3 records are entered into the table emp.
Now, when I'm trying to execute the following query:
SELECT fname || lname as Name from emp;
The result is coming out to be:
Name
0
0
0
instead of the names like "John Doe", etc.
Use the CONCAT() function
SELECT concat(fname, ' ', lname) as Name from emp;
In MySQL the || is a logical OR, hence the result you are getting

To get a column name using the column value

I have a table AccountDetails with some miscellaneous columns no_1,no_2,no_3. I will be giving some label values in any of these columns. For e.g. 'Number of accounts'. How can I find out the column name which has this label 'Number of accounts'.
You can do this:
SELECT CASE
WHEN no_1='Number of accounts' THEN 'no_1'
WHEN no_2='Number of accounts' THEN 'no_2'
WHEN no_3='Number of accounts' THEN 'no_3'
END AS ColumnName
FROM MyTable

Update that splits one column into two?

I'm in a bit of a time crunch. I have a table with 2 columns. firstName, and lastName.
The data imported into SQL had both first and last name inside of the firstName column.
Is there a quick way to update the table to put everything before the first space into the lastName, and everything after the space into the firstName column? I know I could export, and do this via excel, but it in close to production time so I would rather not risk issues.
I have looked at a couple different split posts on here, but those do not involve updating at the same time.
Try to use:
UPDATE
table
SET
lastname = SUBSTRING(firstName, 1, CHARINDEX(' ', firstName) - 1),
firstName= SUBSTRING(firstName, CHARINDEX(' ', firstName) + 1, LEN(firstName))
MySQL
Demonstration:
SET #str := 'Robert Optional Dickey';
SELECT
SUBSTRING_INDEX(#str,SUBSTRING_INDEX(#str,' ',-1),1) AS lastName,
SUBSTRING_INDEX(#str,' ',-1) AS firstName;
Output:
lastName firstName
Robert Optional Dickey
Update Query:
UPDATE your_table
SET lastName = SUBSTRING_INDEX(#str,SUBSTRING_INDEX(#str,' ',-1),1),
firstName = SUBSTRING_INDEX(#str,' ',-1);
Note: It will work for any number spaces inside the full name.It just considers the string after the last space as first name.

List the employees whose last names begin with an "L"

In SQL Server Management Studio
List the employees whose last names begin with an "L". Both first name and last name appear in the same column called employeename and the last name is the second word.
This is what I came up with but only works for first name since is the first word:
SELECT employeename
FROM employee_T
WHERE employeename LIKE 'L%'
So what can I add to make it work?
You can add a ' ' before the 'L%' meaning that it is after a previous words:
select employeename
from employee_T
where employeename LIKE ' L%'
This option won't work if you have records with more than 2 words
Or use substring by last index of ' ' and do Like on that
Or use this to get the last part of your name and compare to what you want:
select employeename
from employee_T
where SUBSTRING_INDEX(employeename, ' ', -1) LIKE 'L%'
Assuming every entry you want to match will look like this:
FirstName LastName
then all you need to do is add a space to your LIKE condition:
SELECT employeename
FROM employee_T
WHERE employeename LIKE ' L%'
If some of your data might only have a single name, or three names, then all bets are off for the above.

MySQL how to return concatenated name and lastname of a user

I need my MySQL query return "value" and "label" field name.
I would like to have "value" as id of user and "label" as "name lastname" or "*name_first_letter lastname*" directly from the query.
Lets say I have a table like this:
id name lastname
1 John Johhanson
2 Peter Petterson
3 Jeff Jefferson
I get the ID as "value" by a simple query: SELECT id as value. I would also like to get "J. Johhanson" or "P. Petterson" as "label".
UPDATE
Sorry for bothering. I tried CONCAT, CONCAT_WS. Initially unsuccessfully, but now managed to get the results easily.
In my case this query worked as I needed:
SELECT CONCAT_WS(' ', name, lastname ) FROM users
WHERE name LIKE "%Peter%"
Thank you.
SELECT id AS value,
Concat(LEFT(name, 1), '. ', lastname) AS label
FROM table;