Split Full Name into First Name and Last Name fields Access - sql

I have this query to retrieve the First Name out of the Full_Name field.
SELECT Employee_Table.Full_Name, Left([Full_Name],InStr([Full_Name]," ")-1) AS First_Name
FROM Employee_Table;
It works fine,
However, I tried to change the query to get the Last Name into the Last_Name field by changing the query to this one but it did not work. Please Help
SELECT Employee_Table.Full_Name, Right([Full_Name],InStr([Full_Name]," ")+1) AS Last_Name
FROM Employee_Table;
I would like to have only one query that pulls the information and not two separate ones.
Thanks
Regards

In your second query, you are pulling from the end of the string, but the length is from the beginning. Oops. The function that you want is MID() rather than RIGHT():
SELECT Employee_Table.Full_Name, Left([Full_Name],InStr([Full_Name]," ")-1) AS First_Name,
mid([Full_Name],InStr([Full_Name]," ")+1) as Last_Name
FROM Employee_Table;

Related

Select 2 distinct columns in 4GL

Needed for my 4gl program:
Let's say I have a table that holds a phone number and a name. There can be 2 people with the same phone number, or 2 names with 1 phone number.
I need to select just 1 of each phone number in the table.
I did:
SELECT DISTINCT phone_number, last_name FROM table
The results will show 2 records. Even phone number is the same, since the names are different it is no longer unique. How can I get a unique phone number regardless of its last_name? (But I want to get the last name as well. I don't care which one)
DISTINCT, as you've noticed, will return rows that are distinct in their entirety.
It sounds like you're looking for something like group by. Essentially, GROUP BY phone_number will return one row for each phone number. Because you also want to get last_name, you'll need to instruct the database how you want it to be returned. You said you don't care which so you could simply write:
SELECT phone_number, MAX(last_name) as last_name
FROM table
GROUP BY phone_number
Informix also supports a FIRST_VALUE aggregate function although I've only used that in OLAP situations so I don't recall if it will work in this context.
If you don't care which last name, then try this out:
SELECT phone_number,
MAX(last_name) AS last_name
FROM table
GROUP BY phone_number

SELECT without commas between attributes?

I need help understanding the following queries and why they are valid/invalid:
SELECT first_name last_name, salary FROM employee VALID
SELECT first_name, last_name salary FROM employee VALID
SELECT first_name last_name salary FROM employee INVALID
For reference, the first retrieves last_name and salary and the second retrieves first_name and salary.
Also, to note, the "extra" unprinted column on the valid lines must be an actual row. Typing something like "asfsfasfs last_name, salary" will be INVALID.
in the second case, salary is considered as an alias for last_name.
So you will get last_name value, but named salary (the column header in a resultset, for example). So it's valid, but you don't retrieve salary's value.
In the third case, you have too much spaces (an alias can't have spaces if you don't put quotes around it), so it's not valid.

SQL - retrieval query for specific string

I am making a small database at the moment (less than 50 entries) and I am having trouble with a query. My query at the moment is
SELECT Name
FROM Customers
WHERE Name LIKE '%Adam%'
The names are in the format of "Adam West".
The query works fine in retrieving all the people with "Adam" in their name but I would like to only retrieve the first name, not the last name. I don't want to split the columns up but would like to know how to rewrite my query to account for this.
SELECT Name
FROM Customers
WHERE Name LIKE 'Adam%'
if you are storing name with space as separator example "Adam abcd" where 'Adam' is firstname and 'abcd' as lastname then following will work
SELECT Expr1
FROM (SELECT LEFT(Name, CHARINDEX(' ', Name, 1)) AS Expr1
FROM Customers) AS derivedtbl_1
WHERE (Expr1 LIKE 'Adm%')
for more details read this article http://suite101.com/article/sql-functions-leftrightsubstrlengthcharindex-a209089
If the name of your customers starts with first name (like Adam West) you can use
select Name
from Customers
where Name like 'Adam %'
Otherwise, if the format of the name is <last name> <first name> you can use
select Name
from Customers
where Name like '% Adam'
Try this
{SELECT Name
FROM Customers
WHERE SUBSTRING(Name,1,CHARINDEX(' ',Name)-1) LIKE '%Adam%'
}
I assumed the first name and last name is saparated by space and i took the firstname out
Using SUBSTRING(Name,1,CHARINDEX(' ',Name)-1)
and compared how you wanted. if your have something else saparating first name and last name change space in charindex with the saparater.
Please let me know if any other help needed.
Regards
Ashutosh
You should indeed "split" your columns and normalize your tables to avoid having to use complicated string functions to search for a lastname or firstname or what ever you need to look for. What about if someone entered lastname first and then firstname? Or just a nickname?
That said, check the use of LIKE on Microsoft technet site. The following query should be helpful on you case:
SELECT Name
FROM Customers
WHERE Name LIKE 'Adam%'

Geting value count from an Oracle Table

I have a table, that contains employees. Since the company I'm working for is quite big (>3k employees) It is only natural, that some of them have the same names. Now they can be differentiated by their usernames, but since a webpage needs a drop-down with all of these users, I need to add some extra data to their names.
I know I could first grab all of the users and then run them through a foreach and add a count to each of the user objects. That would be quite ineffective though. Therefore I'm in need of a good SQL query, that would do something like this. Could a sub-query be the thing I need?
My Table looks something like this:
name ----- surname ----- username
John Mayer jmaye
Suzan Harvey sharv
John Mayer jmay3
Now what I think would be great, if the query returned the same 3 fields and also a boolean if there is more than one person with the same name and surname combination.
Adding the flag to Daniel's answer...
SELECT NAME, SURNAME, USERNAME, DECODE(COUNT(*) OVER (PARTITION BY NAME, SURNAME), 1, 'N', 'Y')
FROM
YOUR_TABLE;
Please note that Oracle SQL has no support for booleans (sigh...)
This can be easily done with a count over partition:
SELECT NAME, SURNAME, USERNAME, COUNT(*) OVER (PARTITION BY NAME, SURNAME)
FROM
YOUR_TABLE;

SQL wildcard matching excluding a specific pattern

sorry about the question I am a newbie to sql. i am attempting to create a search query for our database and I was wondering how would you filter certain words from your query for example:
Here is the sample data (the name column): Jean, Jain, Joan, Jorn, Juan, John, Juin
Lets say that we are searching for the names that start with "J" and end with "n" but we don't want to include "John".
SELECT id, name
FROM tblusers
WHERE name LIKE 'j__n'
WHERE name NOT LIKE 'John'
Obviously the above will have an error, so I was wondering how do I correctly write the above.
Thanks in advance.
SELECT id, name
FROM tblusers
WHERE name LIKE 'j%n'
AND name NOT LIKE 'John'