Match first 3 characters of a field from two different tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crossls28
    New Member
    • May 2017
    • 5

    Match first 3 characters of a field from two different tables

    Hi,

    I have two different tables with forenames, surnames and DOB. I have done a straight forward match but wanted to see a match where Surname and DOB Matched exactly but match the forename from the first 3 characters (some are spelt slightly differently)

    How would I go about this?

    Thanks,
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Hi

    Without seeing you query it is a bit difficult to suggest a definitive answer, but I imagine somewhere you will need something like this
    Code:
    WHERE Left(tblOne.Forename,3) = Left(tblTwo.Forename,3) AND tblOne.Surname = tblTwo.Surname AND tblOne.DOB = tblTwo.DOB
    ??

    HTH


    MTB

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32636

      #3
      I would suggest something on similar lines :
      Code:
      SELECT [tblOne].*
           , [tblTwo].*
      FROM   [tblOne]
             INNER JOIN
             [tblTwo]
        ON   [tblOne].[Surname]=[tblTwo].[Surname]
       AND   [tblOne].[DoB]=[tblTwo].[DoB]
      WHERE  (Left([tblOne].[Forename],3)=Left([tblTwo].[Forename],3))

      Comment

      • crossls28
        New Member
        • May 2017
        • 5

        #4
        Code:
        SELECT [Missing UPNs].*
             , [PupilOnRoll]
             , *
        FROM   [Missing UPNs]
               INNER JOIN
               [PupilOnRoll]
          ON   [Missing UPNs].[Surname]=[PupilOnRoll].[Surname]
         AND   [Missing UPNs].[DateofBirth]=[PupilOnRoll].[DoB]
        WHERE  (Left([MissingUPNs].[Forename],3)=Left([PupilOnRoll],3))
        I gave this a go but it's asking me to enter a parameter. I just need it to list all those that match. I'm not searching for ones in particular i.e. all that start with ste

        Thanks,
        Sam
        Last edited by NeoPa; May 9 '17, 11:18 PM. Reason: Reformatted SQL without changing effect

        Comment

        • MikeTheBike
          Recognized Expert Contributor
          • Jun 2007
          • 640

          #5
          Hi

          You do no say which parameter is requested, but I thing this
          Code:
          Left([PupilOnRoll],3)
          should be like this
          Code:
          Left([PupilOnRoll].[Forename],3)
          ??

          MTB

          Comment

          • crossls28
            New Member
            • May 2017
            • 5

            #6
            Syntax error in query expression '(Left([MissingUPNs].[Forename],3)=Left([PupilOnRoll].[Forename],3);'

            Just got this returned..

            Comment

            • MikeTheBike
              Recognized Expert Contributor
              • Jun 2007
              • 640

              #7
              Without seeing the full query difficult to be sure, but you may be missing a final closing bracket, ie
              Code:
               WHERE (Left([MissingUPNs].[Forename],3)=Left([PupilOnRoll].[Forename],3))
              ??

              MTB

              Comment

              • crossls28
                New Member
                • May 2017
                • 5

                #8
                Code:
                SELECT [Missing UPNs].*
                     , [PupilOnRoll]
                     , *
                FROM   [Missing UPNs]
                       INNER JOIN
                       [PupilOnRoll]
                  ON   [Missing UPNs].[Surname]=[PupilOnRoll].[Surname]
                 AND   [Missing UPNs].[DateofBirth]=[PupilOnRoll].[DoB]
                WHERE  (Left([MissingUPNs].[Forename],3)=Left([PupilOnRoll].[Forename],3));
                This is the full query, I put in the extra bracket. Upon pressing run it prompted for parameters
                Last edited by NeoPa; May 9 '17, 11:26 PM. Reason: Fixed up display again without changing effect.

                Comment

                • MikeTheBike
                  Recognized Expert Contributor
                  • Jun 2007
                  • 640

                  #9
                  AS I said before you do not say which parameter it request, but I think you have missed the space from the table name.
                  This
                  Code:
                  WHERE (Left([MissingUPNs].[Forename],3)=Left([PupilOnRoll].[Forename],3))
                  should be this
                  Code:
                  WHERE (Left([Missing UPNs].[Forename],3)=Left([PupilOnRoll].[Forename],3))
                  ??

                  It is not considered good practice to have spaces in table or field names!

                  MTB

                  Comment

                  • crossls28
                    New Member
                    • May 2017
                    • 5

                    #10
                    It's not my Table i'm just having to make do with what i've been given unfortunately. At present when i press run it's asking for a parameter for the following
                    • Missing UPNs
                    • PupilOnRoll
                    • Missing UPNs.DateofBirt h


                    Thanks in advance

                    Comment

                    • MikeTheBike
                      Recognized Expert Contributor
                      • Jun 2007
                      • 640

                      #11
                      If anything inside square brackets is not recognised then you will be asked to provide a value manually.

                      However this can get confusing if there are other syntax errors. For instance in your early post I have just noticed this
                      [PupilOnRoll],*
                      has a comma when a period is required.

                      If that does not progress it any, then I suggest you (temporarily) simplify the whole thing to and just include the field/tables it does not recognise in the SELECT statement (without the WHERE clause).

                      The other thing we do not know is how/where this query is being used, which may or may not have a bearing on it!


                      MTB

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32636

                        #12
                        Hi Sam.

                        I've changed your posts #4 & #8 to show the SQL in a tidy format and you can easily see the problem. Tidiness and formatting are not simply nice to have pretty features. Without them you will easily miss obvious errors. How much time could have been saved?

                        Let us know if you are still having problems with your SQL once the obvious typo has been sorted out. If so, we'd prefer properly formatted and displayed SQL to work with.

                        It's easy to see such mistakes as wasting our time, but frankly, until you understand the importance of these things, how would you know? So, no worries about past errors. Just see how it goes forward from here.

                        BTW that tip is for general work and isn't restricted to when you post your questions in here.

                        Good luck.

                        NB. Mike is an experienced Access expert so when he gives advice, such as generally avoiding embedded spaces in table ( or other object) names, it's worth paying attention. Most here would give the same advice.
                        Last edited by NeoPa; May 9 '17, 11:29 PM.

                        Comment

                        Working...