AIN102S
Access Query Design
Samples Module – String Functions
P.O. Box 6142
Laguna Niguel, CA 92607
949-489-1472
http://ww...
Admissions AIN102S Strings Module2
AIN102S Contact Information
P.O. Box 6142
Laguna Niguel, CA 92607
949-489-1472
http://w...
AIN102S Notes
 This material was prepared by Mindfire
Solutions for Orange Coast Database
Associates, Inc. Reproduction f...
AIN102S Resources
 Sample database with all queries can be
downloaded at box.net.
 Manuals can be downloaded from
SlideS...
Admissions AIN102S Strings Module5
AIN102 Sequence
 Session 1 – Review of Relational Databases and
Single Table Retrieval...
Admissions AIN102S Strings Module6
AIN102S
 Lecture/discussion with demonstrations
– Mostly practical, but a dose of “the...
Admissions AIN102S Strings Module7
Microsoft Access Query Design
 Focus of this class will be on the Query
Designer, with...
Admissions AIN102S Strings Module8
Access Query Design and SQL
 The SQL engine Access uses is called JET
 It support bot...
Admissions AIN102S Strings Module9
Relational Database Basics
 Storage
 Databases
 Tables
 Rows
 Columns
 Indexes
 ...
Admissions AIN102S Strings Module10
Storage
 In Access one file with extension accdb
(2007 and 2010 databases) or mdb (20...
Admissions AIN102S Strings Module11
Databases
 In the Access world refers to all objects
stored in the mdb.
– Tables
– Qu...
Admissions AIN102S Strings Module12
Relational Database Table
Admissions AIN102S Strings Module13
Sample Access Database
Admissions AIN102S Strings Module14
Database Relationships
Admissions AIN102S Strings Module15
Approaching Query Design
 Try to build queries a step at a time
 Save existing queri...
Admissions AIN102S Strings Module16
Sample Database
 Before we continue…
 Load the sample database if you haven’t
already
Admissions AIN102S Strings Module17
Query Designer
Right click anywhere
for SQL View
Double click or drag
to add a field
D...
Admissions18
String Functions
 The String functions include
– Asc()
– Chr()
– InStr()
– Left()
– Mid()
– Right()
– InStrR...
Admissions19
String Functions (Cont’d)
 The String functions include
– LCase()
– UCase()
– LTrim()
– RTrim()
– Space()
– ...
LCase(), UCase()
Admissions AIN102S Strings Module20
LCase: Returns a string that has been
converted to lowercase.
UCase...
LCase(), UCase()
 Create a sample query and save as
qrySapmle_LCase_UCase.
 Add Diagnostics table.
 Select Descr.
 Bui...
LCase(), UCase()
Admissions AIN102S Strings Module22
Result
Admissions AIN102S Strings Module23
Upper Case
Lower Case
Left()
 Returns a Variant (String) containing a specified
number of characters from the left side of a string.
 Syntax: ...
Right()
Admissions AIN102S Strings Module25
 Returns a Variant (String) containing a specified
number of characters from ...
Mid()
 Returns a Variant (String) containing a specified
number of characters from a string.
 Syntax: Mid(string, start[...
Left(), Right() and Mid()
Admissions AIN102S Strings Module27
 Create a sample query and save as
qrySample_Left_Right_Mid...
Left(), Right() and Mid()
Admissions AIN102S Strings Module28
Result
Admissions AIN102S Strings Module29
3 From Left 3 From Right
All From
2nd
position
Admissions AIN102S Strings Module30
Left(), Right() and Mid()
 Create a sample query and save as
qrySample_Left_Right_Mid...
Result
Admissions AIN102S Strings Module31
2 From
2nd
position
Len()
 Returns a Long containing the number of characters
in a string.
 Syntax: Len(string)
- string: Any valid string e...
Len()
Admissions AIN102S Strings Module33
Result
Admissions AIN102S Strings Module34
Length of
Description
LTrim(),RTrim(), Trim()
 Returns a Variant (String) containing a copy of a specified string
without leading spaces (LTrim...
LTrim(),RTrim(), Trim()
Admissions AIN102S Strings Module36
Result
Admissions AIN102S Strings Module
37
One space
added at left
and 2 space
in right
Length after
space added
Length a...
StrReverse()
 Returns a string in which the character order of a
specified string is reversed.
 Syntax: StrReverse(expre...
StrReverse()
Admissions AIN102S Strings Module39
Result
Admissions AIN102S Strings Module40
Reverse
String
InStr()
 Returns a Variant (Long) specifying the position of the
first occurrence of one string within another.
 Syntax:...
InStrRev()
 Returns the position of an occurrence of one string within
another, from the end of string.
 Syntax: InstrRe...
InStr() and InStrRev()
 Create a sample query and save as
qrySample_InStr_InStrRev.
 Add Diagnostics table.
 Build: InS...
Result
Admissions AIN102S Strings Module44
1stPosition of
“C” from left
1st
Position of
“C” from right
of reverse string
1...
Replace()
 Returns a string in which a specified substring has been replaced
with another substring a specified number of...
Replace()
Admissions AIN102S Strings Module46
 Create a sample query and save as
qrySample_Replace.
 Add Diagnostics tab...
Result
Admissions AIN102S Strings Module47
Replace
Space by “-”
StrConv()
 Returns a Variant (String) converted as specified.
 Syntax: StrConv(string, conversion, LCID)
- string: Strin...
StrConv()
Admissions AIN102S Strings Module49
 Create a sample query and save as
qrySample_StrConv.
 Add Diagnostics tab...
StrConv()
Admissions AIN102S Strings Module50
Result
Admissions AIN102S Strings Module51
StrComp()
 Returns a Variant (Integer) indicating the
result of a string expression.
 Syntax: StrComp(string1, string2[,...
StrComp()
 Create a sample query and save as
qrySample_StrConv.
 Add Diagnostics table.
 Build: StrReverse(Diagnostics....
Result
Admissions AIN102S Strings Module54
Space()
 Returns a Variant (String) consisting of the
specified number of spaces.
 Syntax: Space(number)
 Create a samp...
Result
Admissions AIN102S Strings Module56
5 Space Added
Between Concatenation
String()
 Returns a Variant (String) containing a
repeating character string of the length
specified.
 Syntax: String(nu...
String()
 Create a sample query and save as
qrySample_String.
 Add Diagnostics table.
 Build: String(5,"d") As [String1...
Result
Admissions AIN102S Strings Module59
String(5,"d")
String(3,"Luck") String(5,"54")
Asc() and Chr()
 Asc(): Returns an integer representing the
character code corresponding to the first
letter in a string....
Asc() and Chr()
 Create a sample query and save as
qrySample_Asc_Chr.
 Add Diagnostics table.
 Build: Asc("A") As [ASCI...
Result
Admissions AIN102S Strings Module62
Asc("A") Asc("book") Chr(65) Chr(98)
[end module]
When you want to know more
 ½ day date functions workshop.
 1 day SQL200A Microsoft Access SQL
Class.
Admissions AIN102S...
Admissions AIN102S Strings Module64
End Strings Module
End of Class!
Upcoming SlideShare
Loading in...5
×

AIN102S Access string function sample queries

1,036

Published on

Sample Microsoft Access queries that demonstrate all the string functions.

Published in: Technology
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total Views
1,036
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

AIN102S Access string function sample queries

  1. 1. AIN102S Access Query Design Samples Module – String Functions P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com slides.1@dhdursoassociates.com
  2. 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. 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. 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. 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. 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. 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. 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
  9. 9. Admissions AIN102S Strings Module9 Relational Database Basics  Storage  Databases  Tables  Rows  Columns  Indexes  Views  SQL interface
  10. 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. 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
  12. 12. Admissions AIN102S Strings Module12 Relational Database Table
  13. 13. Admissions AIN102S Strings Module13 Sample Access Database
  14. 14. Admissions AIN102S Strings Module14 Database Relationships
  15. 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. 16. Admissions AIN102S Strings Module16 Sample Database  Before we continue…  Load the sample database if you haven’t already
  17. 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
  18. 18. Admissions18 String Functions  The String functions include – Asc() – Chr() – InStr() – Left() – Mid() – Right() – InStrReverse() Admissions18 AIN102S Strings Module
  19. 19. Admissions19 String Functions (Cont’d)  The String functions include – LCase() – UCase() – LTrim() – RTrim() – Space() – Etc. Admissions19 AIN102S Strings Module
  20. 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. 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
  22. 22. LCase(), UCase() Admissions AIN102S Strings Module22
  23. 23. Result Admissions AIN102S Strings Module23 Upper Case Lower Case
  24. 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. 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. 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. 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.
  28. 28. Left(), Right() and Mid() Admissions AIN102S Strings Module28
  29. 29. Result Admissions AIN102S Strings Module29 3 From Left 3 From Right All From 2nd position
  30. 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.
  31. 31. Result Admissions AIN102S Strings Module31 2 From 2nd position
  32. 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
  33. 33. Len() Admissions AIN102S Strings Module33
  34. 34. Result Admissions AIN102S Strings Module34 Length of Description
  35. 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
  36. 36. LTrim(),RTrim(), Trim() Admissions AIN102S Strings Module36
  37. 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. 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
  39. 39. StrReverse() Admissions AIN102S Strings Module39
  40. 40. Result Admissions AIN102S Strings Module40 Reverse String
  41. 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. 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. 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. 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. 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. 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 “-”.
  47. 47. Result Admissions AIN102S Strings Module47 Replace Space by “-”
  48. 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. 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.
  50. 50. StrConv() Admissions AIN102S Strings Module50
  51. 51. Result Admissions AIN102S Strings Module51
  52. 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. 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
  54. 54. Result Admissions AIN102S Strings Module54
  55. 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
  56. 56. Result Admissions AIN102S Strings Module56 5 Space Added Between Concatenation
  57. 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. 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
  59. 59. Result Admissions AIN102S Strings Module59 String(5,"d") String(3,"Luck") String(5,"54")
  60. 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. 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
  62. 62. Result Admissions AIN102S Strings Module62 Asc("A") Asc("book") Chr(65) Chr(98) [end module]
  63. 63. When you want to know more  ½ day date functions workshop.  1 day SQL200A Microsoft Access SQL Class. Admissions AIN102S Strings Module63
  64. 64. Admissions AIN102S Strings Module64 End Strings Module End of Class!

×