This document provides an overview and examples of string functions in Microsoft Access queries. It discusses functions like Left, Right, Mid, Len, Trim, Replace, Asc, Chr and more. Examples are provided to demonstrate how to use each function in an Access query and return the results. The document is a training material for a class on Access query design and string functions.
2. Admissions AIN102S Strings Module2
AIN102S Contact Information
P.O. Box 6142
Laguna Niguel, CA 92607
949-489-1472
http://www.d2associates.com
slides.1@dhdursoassociates.com
Copyright 2001-20011 All rights reserved.
3. AIN102S Notes
This material was prepared by Mindfire
Solutions for Orange Coast Database
Associates, Inc. Reproduction for commercial
purposes is prohibited.
Classes covering this material are available
either in San Juan Capistrano, California, or
at your facility.
Admissions AIN102S Strings Module3
4. AIN102S Resources
Sample database with all queries can be
downloaded at box.net.
Manuals can be downloaded from
SlideShare at the link below.
Slides can be viewed on SlideShare…
http://www.slideshare.net/OCDatabases
Admissions AIN102S Strings Module4
5. Admissions AIN102S Strings Module5
AIN102 Sequence
Session 1 – Review of Relational Databases and
Single Table Retrieval, Functions and Operators
Session 2 – Multi-table retrieval, Sub queries,
Unions, Action Queries, Indexes
Additional Modules – Sample Date
(AIN102D) and String Functions
(AIN102S)
6. Admissions AIN102S Strings Module6
AIN102S
Lecture/discussion with demonstrations
– Mostly practical, but a dose of “theory”
Assumes familiarity with MS Access and
creating basic queries.
Hands-on
Exercises (Mostly cumulative)
3 hours in length
7. Admissions AIN102S Strings Module7
Microsoft Access Query Design
Focus of this class will be on the Query
Designer, with SQL used from time-to-time to
explain concepts
Knowledge of query design is an essential
skill for retrieving and manipulating Microsoft
Access data.
A foundation skill for eBusiness and almost
all major business applications that use
relational databases
8. Admissions AIN102S Strings Module8
Access Query Design and SQL
The SQL engine Access uses is called JET
It support both MS Access and Visual Basic
Looking “behind the scenes” at your queries
in SQL can be helpful and we will do this;
however SQL is not necessary except for
UNION queries
10. Admissions AIN102S Strings Module10
Storage
In Access one file with extension accdb
(2007 and 2010 databases) or mdb (2003
and earlier). I.e mydatabase.mdb.
Database splitter can be used to split it into
two parts – a front end and a back end.
Interface to physical storage via the “JET”
database engine. This is the behind-the
scenes-workhorse.
11. Admissions AIN102S Strings Module11
Databases
In the Access world refers to all objects
stored in the mdb.
– Tables
– Queries
– From
– Reports
– Macros
– Code modules
15. Admissions AIN102S Strings Module15
Approaching Query Design
Try to build queries a step at a time
Save existing queries that are working
as you like; modify them with a new
name when building a modified query
Try looking at the generated SQL code
from time-to-time
16. Admissions AIN102S Strings Module16
Sample Database
Before we continue…
Load the sample database if you haven’t
already
17. Admissions AIN102S Strings Module17
Query Designer
Right click anywhere
for SQL View
Double click or drag
to add a field
Double click to add a
table
19. Admissions19
String Functions (Cont’d)
The String functions include
– LCase()
– UCase()
– LTrim()
– RTrim()
– Space()
– Etc.
Admissions19 AIN102S Strings Module
20. LCase(), UCase()
Admissions AIN102S Strings Module20
LCase: Returns a string that has been
converted to lowercase.
UCase: Returns a string that has been
converted to uppercase.
Syntax: LCase(String), UCase(string)
- String: Any valid string expression.
21. LCase(), UCase()
Create a sample query and save as
qrySapmle_LCase_UCase.
Add Diagnostics table.
Select Descr.
Build: LCase(Diagnostics.Descr) as [Lower
Case], UCase(Diagnostics.Descr) as [Upper
Case] . Save.
Admissions AIN102S Strings Module21
24. Left()
Returns a Variant (String) containing a specified
number of characters from the left side of a string.
Syntax: Left(string, length)
- string: String expression from which the leftmost
characters are returned.
- length: numeric expression indicating how many
characters to return. If 0, a zero-length string ("") is
returned. If greater than or equal to the number of
characters in string, the entire string is returned.
Admissions AIN102S Strings Module24
25. Right()
Admissions AIN102S Strings Module25
Returns a Variant (String) containing a specified
number of characters from the right side of a string.
Syntax: Right(string, length)
- string: String expression from which the leftmost
characters are returned.
- length: numeric expression indicating how many
characters to return. If 0, a zero-length string ("") is
returned. If greater than or equal to the number of
characters in string, the entire string is returned.
26. Mid()
Returns a Variant (String) containing a specified
number of characters from a string.
Syntax: Mid(string, start[, length])
- string: String expression from which characters are
returned.
- start: Character position in string at which the part
to be taken begins. If start is greater than the
number of characters in string, Mid returns a zero-
length string ("").
- length(Optional): Number of characters to return.
Admissions AIN102S Strings Module26
27. Left(), Right() and Mid()
Admissions AIN102S Strings Module27
Create a sample query and save as
qrySample_Left_Right_Mid1.
Add Diagnostics table.
Select Descr.
Build: Left(Diagnostics.Descr,3) as [Left],
Right( Diagnostics.Descr,3) as [Right],
Mid(Diagnostics. Descr,2) as [Mid]. Save.
30. Admissions AIN102S Strings Module30
Left(), Right() and Mid()
Create a sample query and save as
qrySample_Left_Right_Mid1.
Add Diagnostics table.
Select Descr.
Build: Left(Diagnostics.Descr,3) as [Left],
Right( Diagnostics.Descr,3) as [Right],
Mid(Diagnostics. Descr,2,2) as [Mid] . Save.
32. Len()
Returns a Long containing the number of characters
in a string.
Syntax: Len(string)
- string: Any valid string expression.
Create a sample query and save as qrySample_Len.
Add Diagnostics table.
Select Descr.
Build: Descr,Len(Diagnostics.Descr) as [Length] .
Save.
Admissions AIN102S Strings Module32
35. LTrim(),RTrim(), Trim()
Returns a Variant (String) containing a copy of a specified string
without leading spaces (LTrim), trailing spaces (RTrim), or both
leading and trailing spaces (Trim).
Syntax: LTrim(string), RTrim(string), Trim(string)
Create a sample query and save as qrySample_Trim.
Add Diagnostics table.
Select Descr, and type DISTINCT " " & Diagnostics.Descr & " "
as [Space Added].
Build: Len([Space Added]) as [Length],Len( LTrim([Space
Added])) as [LTrim],Len( RTrim([Space Added])) as
[RTrim],Len(Trim([Space Added])) as [Trim]. Save.
This will reflect the length after trimming.
Admissions AIN102S Strings Module35
37. Result
Admissions AIN102S Strings Module
37
One space
added at left
and 2 space
in right
Length after
space added
Length after
Left trim
Length after
Right trim
Length after
Trim
38. StrReverse()
Returns a string in which the character order of a
specified string is reversed.
Syntax: StrReverse(expression)
- expression: The expression argument is the string
whose characters are to be reversed.
Create a sample query and save as
qrySample_StrReverse.
Add Diagnostics table.
Build: strReverse(Diagnostics.Descr) as [Reversed
String] . Save.
This will reflect the length after trimming.
Admissions AIN102S Strings Module38
41. InStr()
Returns a Variant (Long) specifying the position of the
first occurrence of one string within another.
Syntax: InStr([start, ]string1, string2[, compare])
- start(Optional): Numeric expression that sets the
starting position for each search. If omitted, search
begins at the first character position.
- string1: String expression being searched.
- string2: String expression sought.
- compare(Optional): Specifies the type of String
expression .
Admissions AIN102S Strings Module41
42. InStrRev()
Returns the position of an occurrence of one string within
another, from the end of string.
Syntax: InstrRev(stringcheck, stringmatch[, start[,
compare]])
- stringcheck: String expression being searched.
- stringmatch: String expression being searched for.
- start(optional): Numeric expression that sets the starting
position for each search.
- compare(Optional): Numeric value indicating the kind of
comparison to use when evaluating substrings. If omitted,
a binary comparison is performed.
Admissions AIN102S Strings Module42
43. InStr() and InStrRev()
Create a sample query and save as
qrySample_InStr_InStrRev.
Add Diagnostics table.
Build: InStr(Diagnostics.Descr, "C") as [InStr For
"C"], InStr(Diagnostics.Descr, "e") as [InStr For
"e"], InStrRev (Diagnostics.Descr, "C") as
[InStrRev For "C"], InStrRev(Diagnostics.Descr,
"e") as [InStrRev For "e“] . Save.
Admissions AIN102S Strings Module43
44. Result
Admissions AIN102S Strings Module44
1stPosition of
“C” from left
1st
Position of
“C” from right
of reverse string
1stPosition of
“e” from left
1st
Position of
“e” from right
of reverse string
45. Replace()
Returns a string in which a specified substring has been replaced
with another substring a specified number of times.
Syntax: Replace(expression, find, replace[, start[, count[,
compare]]])
- expression: String expression containing substring to replace.
- find: Substring being searched for.
- replace: Replacement substring.
- start(Optional): Position within expression where substring search
is to begin.
- count(Optional): Number of substring substitutions to perform.
- compare(Optional): Numeric value indicating the kind of
comparison to use when evaluating substrings.
Admissions AIN102S Strings Module45
46. Replace()
Admissions AIN102S Strings Module46
Create a sample query and save as
qrySample_Replace.
Add Diagnostics table.
Build: Replace(Diagnostics.Descr, " ", "-") as
[Replace Space By "-“]. Save.
This will replace a blank space with “-”.
48. StrConv()
Returns a Variant (String) converted as specified.
Syntax: StrConv(string, conversion, LCID)
- string: String expression to be converted
- conversion: The sum of values specifying the type
of conversion to perform.
- LCID: The LocaleID
The conversion argument settings are: 1-
vbUpperCase, 2- vbLowerCase, 3- vbLowerCase
etc.
Admissions AIN102S Strings Module48
49. StrConv()
Admissions AIN102S Strings Module49
Create a sample query and save as
qrySample_StrConv.
Add Diagnostics table.
Build: StrConv(Diagnostics.Descr, 1) as
[Convert Upper], StrConv(Diagnostics.Descr, 2)
as [Convert Lower] . Save.
52. StrComp()
Returns a Variant (Integer) indicating the
result of a string expression.
Syntax: StrComp(string1, string2[, compare])
- string1: Any valid string expression.
- string2: Any valid string expression.
- compare(Optional): Specifies the type of
string comparison.
Admissions AIN102S Strings Module52
53. StrComp()
Create a sample query and save as
qrySample_StrConv.
Add Diagnostics table.
Build: StrReverse(Diagnostics.Descr) as [String
Reverse], StrComp(Diagnostics.Descr,
Diagnostics.Descr) as [StrComp Same String],
StrComp(Diagnostics.Descr, [String Reverse]) as
[StrComp Different String] . Save.
Admissions AIN102S Strings Module53
55. Space()
Returns a Variant (String) consisting of the
specified number of spaces.
Syntax: Space(number)
Create a sample query and save as
qrySample_Space.
Add Diagnostics table.
Build: Diagnostics.DiagNo & Space(5) &
Diagnostics.Descr As [Five Space Between
Concat] .Save.
Admissions AIN102S Strings Module55
57. String()
Returns a Variant (String) containing a
repeating character string of the length
specified.
Syntax: String(number, character)
- number: Length of the returned string.
- character: Character code specifying the
character or string expression whose first
character is used to build the return string.
Admissions AIN102S Strings Module57
58. String()
Create a sample query and save as
qrySample_String.
Add Diagnostics table.
Build: String(5,"d") As [String1],
String(3,"Luck") As [String2], String(5,"54")
As [String3] .Save.
Admissions AIN102S Strings Module58
60. Asc() and Chr()
Asc(): Returns an integer representing the
character code corresponding to the first
letter in a string.
Chr(): Returns a string containing the
character associated with the specified
character code.
Syntax: Asc(string), Chr(charcode)
Admissions AIN102S Strings Module60
61. Asc() and Chr()
Create a sample query and save as
qrySample_Asc_Chr.
Add Diagnostics table.
Build: Asc("A") As [ASCII of "A"], Asc("book") As
[ASCII of "b"], Chr(65) As [Return "A"], Chr(98)
As [Retrun "b“] .Save.
Admissions AIN102S Strings Module61