Trim string in MS access query - ms-access

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

Related

Text before space in Access query

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))

Fit multiple results returned from one dataset in single textbox

I have a data set which returns results like below:
SELECT
[Name]
,[Count]
FROM [dbo].[TestTable1]
ID Name Count
------------------------------------------
1 International school 100
2 World school 200
3 Universe school 400
I have one text box in which I would like to show the count.
Here is the international school count: «Expr»
Here is the world school count: «Expr»
Here is the Universe school count: «Expr»
I'm seeking an expression in which the result should return like below:
Here is the international school count: 100
Here is the world school count: 200
Here is the Universe school count: 400
Here is my example expression :
=IIF(First(Fields!Name.Value, "CountinOneBox")="International school",(Fields!Count.Value, "CountinOneBox"),"")
Note: sum(Fields!Count.Value, "CountinOneBox") provides 700
Hope I have explained this correctly. How can I get this results? Thanks.
I would do this in SQL. I've replicated your sample data here and then just dropped the resulting field in a simple report
DECLARE #t table(ID int, [Name] varchar(100), [Count] int)
INSERT INTO #t VALUES
(1, 'International school', 100),
(2, 'World school', 200),
(3, 'Universe school', 400)
DECLARE #s nvarchar(max) = ''
DECLARe #crlf char(2) = char(13) + char(10)
SELECT #s =
#s + 'Here is the '
+ [Name]
+ ' count: '
+ CAST([COUNT] as varchar(10))
+ #crlf
FROM #t
SELECT #s as Result
Results looks like this. (I've set a border on the text box so you can see it's not wrapping, it's using the CR/LF we added.
You need to write everything in one expression if you want to have it all in one textbox. Like this:
Lets say the follwing expression returning your 100, 200, 400.
=Sum(Fields!Result1.Value) ' 100
=Sum(Fields!Result2.Value) ' 200
=Sum(Fields!Result3.Value) ' 400
="Here is the international school count: " & Sum(Fields!Result1.Value) & VbNewLine &
"Here is the world school count: " & Sum(Fields!Result2.Value) & VbNeLine &
"Here is the Universe school count: " & Sum(Fields!Result3.Value)

Mysql query not inserting values

I am trying to use the query below to insert a concatenated converted set of integers to string for use on a datetime field in my table.
TABLE
Field Type
empID int(11)
time_stamp datetime
in_out char(3)
am_pm char(2)
QUERY
Dim query As String = "INSERT INTO attendance VALUES(" & empID.Text & _
"STR_TO_DATE(CONCAT("& empYear.Text & ",'-'," & empMonth.Text & ",'-'," & _
empDay.Text & ",' '," & empHour.Text & ",':'," & empMin.Text & ",':'," & _
empSec.Text & ",'%Y-%m-%d %H:%i:%s'),'out','pm')"
There is no problem with the connection and the values. I have tried to insert the values into a test column of string type and the output is this:
133201712311827
I am pretty sure it's with how I use these characters: '' "" "," - :. I just can't figure out how.
First problem I see, here
& empID.Text & "STR_TO_DATE(. . . .
you're missing comma after first value
& empID.Text & "***,*** STR_TO_DATE(. . . .
Second issue, I identified when I've replaced your text values with hard coded values - You are missing closing parenthesis for str_to_date. Here ,'%Y-%m-%d... should be ), '%Y-%m-%d...
STR_TO_DATE(CONCAT(1999,'-',01,'-',01,' ',10,':',25,':',30***)***,'%Y-%m-%d %H:%i:%s')
As you see- my replacement shows that you have no issues with concatenation, single quote and :. Theo only other variable here is quality of data in text boxes.
Update
This answer (above) is correct. Using sql fiddle I created schema and when replaced text box values with hard-coded ones - all worked. My suggestions to add comma and parenthesis hold true. Your claim about problems with single quotes are false.
create table xxx (empID int(11), time_stamp datetime, in_out char(3), am_pm char(2));
INSERT INTO xxx VALUES(123,
STR_TO_DATE(CONCAT('2017','-','1','-','23',' ','10',':','35',':','40'),'%Y-%m-%d %H:%i:%s'),
'out','pm');
commit;
Select * from xxx
empID | time_stamp | in_out | am_pm
123 | January, 23 2017 10:35:40 | out | pm
End Update
On top of that, you could do it much better by parameterizing, which will look like something like this
command.CommandText = "insert into ... values (#1, #2, #3, #4)"
command.Parameters.AddWithValue("#1", Convert.ToInt32(empID.Text))
dim date as new DateTime(Convert.ToInt32(empYear.Text), Convert.ToInt32(empMonth.Text), . . . . )
command.Parameters.AddWithValue("#2", date)
. . . . . .
command.ExecuteNonQuery()
Parameterizing will make it easy to work with dates and strings

Keep leading zero when inserting a string into a SELECT (MySql)

Here is my string (that is string NOT int):
string code = "0101";
Here is my SQL:
string select = "SELECT " + code + " AS code" ...
Here is what I get in the table:
code
101
101
101
...
Here is what I need:
code
0101
0101
...
P.S. Tried both CAST and CONVERT.
0101 is a numeric literal, and when displaying numbers leading zeros are removed. You could treat it as a string literal by surrounding it with single quotes ('):
string code = "0101";
string select = "SELECT '" + code + "' AS code" ...

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