Left and SQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • artemetis
    New Member
    • Jul 2007
    • 76

    Left and SQL

    Stupid question I'm sure.....

    Field entryType has values of ##/y and ###/y.

    In a simple Access SQL I'd like to trim, from the right up to the "#".
    I don't need/want the "/y"

    Thanks!
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    try ...
    Code:
    left(len([fieldname])-2)

    Comment

    • artemetis
      New Member
      • Jul 2007
      • 76

      #3
      Thanks!
      I tried this and received an #Error?

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        OK change the query view to SQL and post the full query here.

        Comment

        • nico5038
          Recognized Expert Specialist
          • Nov 2006
          • 3080

          #5
          Guess you worded your question a bit vague.
          I think you want to get the ## or ### and ignore the /y
          Then use:
          Code:
          left([fieldname],instr([fieldname],"/")-1)
          When the "/y" is a literal you could also use:
          Code:
          Replace([fieldname],"/y","")
          Nic;o)

          Comment

          • artemetis
            New Member
            • Jul 2007
            • 76

            #6
            Thanks Guys!

            Nico', that worked perfectly!!! Sorry my initial question wasn't quite clear.

            To take it one step further....ther e are several records that are missing data in the field. For these records, the query returns the #Error code. Is there a way (IIF?) I could display a 0 rather than the err?

            I'm trying to get this on my own using IIF, not sure if this is the right route.....

            Comment

            • MMcCarthy
              Recognized Expert MVP
              • Aug 2006
              • 14387

              #7
              There are a couple of ways you could do this but try ...
              Code:
              IIf(NZ([fieldname],"")<>"",left([fieldname],instr([fieldname],"/")-1),"")

              Comment

              • nico5038
                Recognized Expert Specialist
                • Nov 2006
                • 3080

                #8
                Good sample Mary, but it can be done a bit shorter:
                Code:
                IIf(IsNull([fieldname],"",left([fieldname],instr([fieldname],"/")-1))
                ' or better when also empty fields or fields without a "/" exist:
                IIF(Instr(NZ([fieldname),"/")>0,left([fieldname],instr([fieldname],"/")-1),"")
                I would use the second proposal as that's really 100% "safe".

                Nic;o)

                Comment

                • MMcCarthy
                  Recognized Expert MVP
                  • Aug 2006
                  • 14387

                  #9
                  I like the second one too :D

                  It allows for all eventualities.

                  Comment

                  • artemetis
                    New Member
                    • Jul 2007
                    • 76

                    #10
                    I used Msquared's code - it worked fine!

                    I'm using this IIF in an Append Query.
                    Is it possible, to insert/display a 0 when the field is blank?

                    Comment

                    • MMcCarthy
                      Recognized Expert MVP
                      • Aug 2006
                      • 14387

                      #11
                      Just change the last set of empty double quotes in the IIf statement to 0.

                      Comment

                      Working...