how to delete everything after a blank space in access - ms-access

I have a table->column first-name with values:
Juan
Manuel l.
Richard Wit
I'm trying to do a query that return
Juan
Manul
Richard
I want to eliminate after a space.

Assuming you want SQL, try
SELECT MID(name + " a", 1, INSTR(name + " a", " ")-1) AS FirstName FROM myTable
In VBA it would be similar, since Mid() and InStr() work exactly the same as in Access-SQL
Really, though, it would be better to just store first and last name in separate fields so you don't have to do this sort of finagling in the SQL.

SELECT Left([First-Name], InStr([First-Name] & ' ', ' ') - 1) As CleanFirstName
FROM table
Keep in mind that running this will be inefficient since you will be executing VBA functions against every record in your query. Depending on how you are using this, you may want to have the query return the full field and do the processing after the fact.

Related

MySQL Search by combination of first and last name

I have a database like this:
first_name last_name
Susan Jones
Captain Kirk
Luke Skywalker
I'm trying to write a query like this:
var search = 'Luke Sky';
SELECT * FROM users WHERE first_name + last_name LIKE '%Luke Sky%'
But I don't think that syntax is correct.
In MySQL you combine strings with CONCAT(), not +, so it's:
SELECT * FROM users WHERE CONCAT(first_name, ' ', last_name') LIKE '%Luke Sky%'
What you ultimately want is something like:
WHERE ... LIKE ?
Then supply a placeholder value constructed in your application layer that has the requisite % values added on the start and end.
As always, remember that "first name" and "last name" are hazy concepts at best. Some cultures have family name first and others don't really have a last name at all.
You can do this in a couple of ways. Firstly you could split the search string on " " to give you separate first and last names and then you could do where first_name like '%Luke%' and last_name like '%sky%
Or you could use the concat function so something like concat(first_name, " ", last name) like '%Luke sky%
Personally I'd go for the first one because then it lets you match each name partially. I.e "lu sky" whereas the second way would require the first name matches fully.

query with IIF statement

I am responsible for producing a set of name badges for an upcoming class reunion. Have everything set up and have been able to produce the correct output for those attending the reunion.
What I want to do is place parenthesis around the maiden names of the married women attending the reunion. Right now the maiden name is being displayed on the badge, but without parenthesis.
I have placed the following expression in the MaidenName field of the query I have written, but nothing is happening, at least this expression did not produce any error messages when I ran it.
IIf([MaidenName]="IsNull",[MaidenName]=" ",([MaidenName]=("("+[MaidenName]+")")))
When I entered the expression the first time the IsNull was without quotes. When I ran the
the query the quotes were place around the IsNull statement. The query ran, but there were no parenthesis around the maiden name on the output.
You would probably like to use the IsNull() function, and clean a little bit that Iif syntax:
IIf(IsNull([MaidenName]), " ", "(" & [MaidenName] & ")")
I recommend using the SQL-native is null comparison instead of the IsNull() function. A SQL-native way will always be faster and more portable than a VBA function.
iif(MaidenName is null, '', ' (' & MaidenName & ')')
Plus I think it's easier to read.

Text manipulation of strings containing "The "

I have a table in a MySQL database which contains data like this;
ID text
1 Action Jackson
2 The impaler
3 The chubby conquistador
4 Cornholer
I want to display them in alphabetical order minus the leading "The ". This is what I've come up with which works.
SELECT ID, CASE LEFT(l.text, 4) WHEN "The " THEN CONCAT(RIGHT(l.text, LENGTH(l.text) - 4), ", The") ELSE l.text END AS "word"
FROM list l
This solution seems a little clunky, does anyone have a more elegant answer?
I think this is what you are looking for:
SELECT ID,
text
FROM list l
ORDER BY TRIM(LEADING 'The ' FROM text);
If you can at all, I would think of restructuring your data a bit.. Its hundreds of times better to rely on mysql indexes and proper sorting instead of doing it dynamically like this.
How about adding a field that drops the 'The ', and sort on that? You could make sure that this secondary field is always correct with a few triggers.
SELECT TRIM(LEADING 'The' FROM text) as word
FROM list
ORDER BY TRIM(LEADING 'The' FROM text)

MySQL search within the last 5 characters in a column?

My user table has a column "name" which contains information like this:
Joe Lee
Angela White
I want to search for either first name or last name efficiently. First name is easy, I can do
SELECT * FROM user WHERE name LIKE "ABC%"
But for last name, if I do
SELECT * FROM user WHERE name LIKE "%ABC"
That would be extremely slow.
So I am thinking about counting the characters of the input, for example, "ABC" has 3 characters, and if I can search only the last three characters in name column, that would be great. So I want something like
SELECT * FROM user WHERE substring(name, end-3, end) LIKE "ABC%"
Is there anything in MySQL that can do this?
Thanks so much!
PS. I cannot do fulltext because our search engine doesn't support that.
The reason that
WHERE name LIKE '%ith'
is a slow way to look for 'John Smith' by last name is the same reason that
WHERE Right(name, InStr(name, ' ' )) LIKE 'smi%'
or any other expression on the column is slow. It defeats the use of the index for quick lookup and leaves the MySQL server doing a full table scan or full index scan.
If you were using Oracle (that is, if you worked for a formerly wealthy employer) you could use function indexes. As it is you have to add some extra columns or some other helping data to accelerate your search.
Your smartest move is to split your first and last names into separate columns. Several other people have pointed out good reasons for doing that.
If you can't do that you could try creating an extra column which contains the name string reversed, and create an index on that column. That column will have, for example, 'John Smith' stored as 'htimS nhoJ'. Then you can search as follows.
WHERE nameReversed LIKE CONCAT(REVERSE('ith'),'%')
This search will use the index and be decently fast. I've had good success with it.
You're close. In MySQL you should be able to use InStr(str, substr) and Right(str, index) to do the following:
SELECT * FROM user WHERE Right(name, InStr(name, " ")) LIKE "ABC%"
InStr(name, " ") returns the index of the Space character (you may have to play with the " " syntax). This index is then used in the Right() function to search for only the last name (basically; problems arise when you have multiple names, multiple spaces etc). LIKE "ABC%" would then search for a last name starting with ABC.
You cannot use a fixed index as names that are more than 3 or less than 3 characters long would not return properly as you suggest.
However, as Zane said, it's a much better practise to use seperate fields.
If it is a MyIsam table, you may use Free text search to do the same.
You can use the REGEXP operator:
SELECT * FROM user WHERE name REGEXP "ABC$"
http://dev.mysql.com/doc/refman/5.1/en/regexp.html

How to ignore spaces in mysqbl column?

I have a table of artist names such as Lady Gaga, Jason Mraz, Death Cab For Cutie, etc.
I want to be able to search for artists with no spaces. For instance, search the artists table where name is jasonmraz.
Is there any way to do this with mysql? Or should I create a new column in my table to hold these types of names?
SELECT REPLACE(your_col, " ", "") ...
or
SELECT your_col
WHERE REPLACE(your_col, " ", "") = "somestringsansspaces"
You can use the REPLACE function
SELECT REPLACE('Lady Gaga', ' ', '');
Will return LadyGaga
Using REPLACE(), as suggested, will work.
Bear in mind, however, that this is more work for your server, so if you have a large table of names (which I doubt you do, but never mind) you might want to consider adding a field of the names with no spaces.