Text before space in Access query - ms-access

How would I get the text in a string before a space - and avoid an error if there is no space in the string?
For example, if I have a FirstName field with names that looks like this:
John S
Sally Q
Thomas R
Robert
Mary J
I want the field in the query to return:
John
Sally
Thomas
Robert
Mary
I tried the following field in my query but it is still returning the space and character after the space:
FName: Left([FirstName],IIf(Len(InStr([FirstName]," ")=0),Len([FirstName]),InStr([FirstName]," ")-1))
Thank you!

Consider:
Left(FirstName, IIf(InStr(FirstName, " ")=0, Len(FirstName), InStr(FirstName," ")-1))
Or
IIf(InStr(FirstName, " "), Left(FirstName, InStr(FirstName, " ")-1), FirstName)
This assumes all entries follow the example patterns provided, and not something like: Mary Jo R. Gets complicated if you want Mary Jo.

This will work:
Left([First Name], IIf((InStrRev([First Name], " ", -1)-1)<= 0, Len([First Name]), InStrRev([First Name], " ", -1)-1))

Related

return mid string and count

I have a concatenated string in a field that I now need to pull statistics from.
What is needed is a count of how many records are from each county in Maryland.
fieldname county_city is stored as follows:
Frederick,MD - Frederick County - 21701
//State
trim(substring(SUBSTRING_INDEX(county_city,',',-1),1,3)) as state
//city
SUBSTRING_INDEX(county_city,'-',1) as city_state
//zip code
SUBSTRING_INDEX(county_city,'-',-1) as zipcode,
but getting the county has been eluding me!
I have an idea that getting the count will elude me as well.
With string functions is a 2 step procedure:
set #s = 'Frederick,MD - Frederick County - 21701';
SELECT TRIM(SUBSTRING_INDEX(SUBSTRING(#s, LENGTH(SUBSTRING_INDEX(#s, '-' ,1)) + 2), '-', 1));
See the demo.
Result:
Frederick County

Trim string in MS access query

please help me to fix the below query.
I wanted to trim space in between the string. I have column called Name and it has spaces in between. For instance in data source The name format is “John Steve Miller” (more than one space in between) and I want to trim it as “John Steve Miller”( with only one space). Thank you in Advance
Whilst you could use the Replace function to replace every pair of two spaces with a single space:
?Replace("John Steve Miller", " ", " ")
John Steve Miller
This will not account for instances in which you have more than two consecutive spaces, e.g.:
?Replace("John Steve Miller", " ", " ")
John Steve Miller
?Replace("John Steve Miller", " ", " ")
John Steve Miller
As such, I would suggest the following function to handle any number of consecutive spaces:
Function TrimSpace(strStr As String) As String
Dim strRtn As String: strRtn = Replace(strStr, " ", " ")
If strRtn = strStr Then
TrimSpace = Trim(strRtn)
Else
TrimSpace = TrimSpace(strRtn)
End If
End Function
?TrimSpace("John Steve Miller")
John Steve Miller
?TrimSpace("John Steve Miller")
John Steve Miller
?TrimSpace("John Steve Miller")
John Steve Miller
?TrimSpace("John Steve Miller")
John Steve Miller

How to get information on one line in my Expression

My expression shows just the Name, SSN, DOB and Phonenumber.
Here's my expression:
=Fields!FST_NAME.Value & vbCrLf & Fields!LAST_NAME.Value & vbCrLf & Fields!SOC_SECURITY_NUM.Value & vbCrLf & Fields!BIRTH_DT.Value & vbCrLf & Fields!ATTRIB_43.Value
I want it to show like this
Name: John Smith
Right Now it just shows John Smith
If you write one big expression your textbox will look like this:
This is hard to work with both in terms of layout and making corrections to the formula. A good alternative is to use placeholders with labels in your textbox. So you would type in "Name: " and then right click after it and select "Create Placeholder". Set the properties like this:
And the textbox can be nice to read and work with:
You can even control the formatting of the placeholders independently which comes in handy if you need to include dates or numbers.
If all of your fields are strings, then this works:
=Fields!FST_NAME.Value + " " +
Fields!LAST_NAME.Value + " " +
Fields!SOC_SECURITY_NUM.Value + " " +
Fields!BIRTH_DT.Value + " " +
Fields!ATTRIB_43.Value
However, if Birth_Dt, or any other field is not a valid string (DateTime datatype, for example) then you will need to use CStr(Fields!BIRTH_DT.Value) to convert it to a string so it can be correctly concatenated.
In an example I can do locally my expression is as follows:
=CStr(Fields!ExpMonth.Value) + " " +
Fields!ItemName.Value + " " +
Fields!ItemClass.Value
The first three rows of the result it produces look like this:
1 1 Year Membership 1 Year 1 1 Year Membership 1 Year
2 1 Year Membership 1 Year 2 1 Year Membership 1 Year
3 1 Year Membership 1 Year 3 1 Year Membership 1 Year
To add text to the string, you can encapsulate it in quotes in your concatenated string, like this:
="This is added text" + " " +
CStr(Fields!ExpMonth.Value) + " " +
Fields!ItemName.Value + " " +
Fields!ItemClass.Value
The above results now look like this:
1 1 Year Membership 1 Year This is added text 1 1 Year Membership 1 Year
2 1 Year Membership 1 Year This is added text 2 1 Year Membership 1 Year
3 1 Year Membership 1 Year This is added text 3 1 Year Membership 1 Year

Detect double value in MySQL

I have an InnoDB table with fields
firstname, lastname
While displaying names, usually only firstname is enough. Sometimes users have the same first name; so I have to get firstname and first letter of lastname:
CONCAT(firstname, ' ', SUBSTRING(lastname, 1, 1), '.')
Is there a (performant) way to only display the first letter of the last name in case of a double first name? Something like
WHEN isDouble(Firstname) THEN
CONCAT(firstname, ' ', SUBSTRING(lastname, 1, 1), '.')
ELSE firstname
/* edit */
Forgot to mention the solution I was thinking of:
Creating a column 'double_firstname', with value 1 or 0, and use a CASE statement to select. Then update the double_firstname column on user create and delete.
You can of course ask mysql if the number of entries for that firstname is bigger than one, so:
select IF( (select count(*) as cnt from person where firstname = p.firstname) > 1
, concat(firstname, " ", substring(lastname, 1))
, firstname)
from person
where id = 4711
;
But that is not very quick.
Better for a higher number of persons is a stable mark on person how to "call" her. That could be "firstname lastname" initially and then get more personally with reducing to firstname by a cronjob. It also could mean to call "John Doe" just John, because he entered early, and "John DaSecond" call "John D.", and "JohnDaThird" call "John DaT.".
JohnD is not unique in that scenario.
Is John Doe informed about being renamed to "John D." in your Scenario?
You asked for good performance as well as the ability to do it. If you have an index on names(firstname, lastname) the following should perform well:
select (case when exists (select 1
from names n2
where n2.firstname = n.firstname and n2.lastname <> n.lastname
)
then concat(firstname, ' ', left(lastname, 1))
else firstname
end)
from names n

Microsoft Access Data Base .. Select Query

i am doing queries practice in Microsoft access. i want to concatenate the first name with the fax number.. the fax number is like (123)555-0103 ..
i`m doing that
select [first name] +' ''s Fax Number is' +str(fax number) as [Mariya`s Fax Number]
from employees where id =4;
but it is giving error..
That would be:
select [first name] & " ''s Fax Number is " & [fax number] as [Mariya`s Fax Number]
from employees where id =4
You should use & to concatenate
You should use '' for each single quote
You should use double quotes (") for strings.