Searching for a blank field in an SQL statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shredder249
    New Member
    • Jan 2008
    • 22

    Searching for a blank field in an SQL statement

    Hi, im trying to run an update SQL statement which sets the flag (queryident) to ticked if the track title field in that record is blank. However using '' to indicate a blank field does not work. Any ideas? The statment is below.


    Code:
    DoCmd.RunSQL ("UPDATE [TRACK] SET [QueryIdent] = True WHERE [Track Title] = ''")
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Originally posted by shredder249
    Code:
    DoCmd.RunSQL ("UPDATE [TRACK] SET [QueryIdent] = True WHERE [Track Title] = ''")
    Hi. You may have null values in your fields rather than empty strings. As nulls will not be found by testing for string length I have used Nz in the code below to return an empty string if the field is null. It also explicitly test for empty strings using the Len function:
    Code:
    DoCmd.RunSQL ("UPDATE [TRACK] SET [QueryIdent] = True WHERE Len(Nz([Track Title])) = 0;"
    -Stewart

    Comment

    • shredder249
      New Member
      • Jan 2008
      • 22

      #3
      Thanks! All working fine now.

      Originally posted by Stewart Ross Inverness
      Hi. You may have null values in your fields rather than empty strings. As nulls will not be found by testing for string length I have used Nz in the code below to return an empty string if the field is null. It also explicitly test for empty strings using the Len function:
      Code:
      DoCmd.RunSQL ("UPDATE [TRACK] SET [QueryIdent] = True WHERE Len(Nz([Track Title])) = 0;"
      -Stewart

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32668

        #4
        Testing for Null in SQL is best done using the Is Null construct.
        Code:
        DoCmd.RunSQL ("UPDATE [TRACK] SET [QueryIdent] = True WHERE [Track Title] Is Null;"
        If you want to test for (Null or "") then use :
        Code:
        DoCmd.RunSQL ("UPDATE [TRACK] SET [QueryIdent] = True WHERE Nz([Track Title], '') = '';"
        The second version is very little different from the previous post. The first is recommended where possible as it runs more efficiently

        NB. The quotes within the SQL string are (') rather than ("). I think you understand this already but for anyone else...

        Comment

        • Stewart Ross
          Recognized Expert Moderator Specialist
          • Feb 2008
          • 2545

          #5
          Originally posted by NeoPa
          Testing for Null in SQL is best done using the Is Null construct....
          Hi NeoPa. IsNull in this case would leave the potential for table lines which contain truly zero-length strings not being updated, hence the suggested use of Nz instead (copes with both cases, even if one predominates, or is the only situation at present).

          -Stewart

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32668

            #6
            Indeed you're right Stewart.
            A couple of small points worth noting though :
            1. I was referring to the "Is Null" SQL construct rather than the IsNull() VBA function. This runs faster (as it's a SQL construct) for larger, more complicated queries. Only useful for Null checks though.
            2. My second example was very similar to yours except I excluded the Len() part. This (either of them - using Nz()) is a good example of covering the situation where Nulls OR empty strings must be handled.

            I would have to say that, in my experience, it's very rare for tables to allow both empty strings AND nulls.

            By the way Stewart, welcome to TheScripts. We've not crossed paths much before, but I've already heard ABOUT you. Keep up the good work.

            Comment

            Working...