Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL: finding out if a column is NULL /without/ downloading all the content

SQL: finding out if a column is NULL /without/ downloading all the content

Scheduled Pinned Locked Moved Database
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    moredip
    wrote on last edited by
    #1

    Hi all, Is there any way to write an SQL SELECT statement that will tell me whether a column is NULL or not, without having to download the column's content? I'm working on a content-management system that stores HTML pages as text records in a MS SQL Server table. Some pages have a 'default' page which is displayed by setting the text column to NULL. For the web interface I'd like a way to summarize the state of the content (e.g. whether it has been supplied or not), but it seems ridiculous to download the entire HTML content of a section just to see if it's NULL or not. Oh, and I can't use Stored Procedures for reasons I won't go into :(. TIA, Pete

    E 1 Reply Last reply
    0
    • M moredip

      Hi all, Is there any way to write an SQL SELECT statement that will tell me whether a column is NULL or not, without having to download the column's content? I'm working on a content-management system that stores HTML pages as text records in a MS SQL Server table. Some pages have a 'default' page which is displayed by setting the text column to NULL. For the web interface I'd like a way to summarize the state of the content (e.g. whether it has been supplied or not), but it seems ridiculous to download the entire HTML content of a section just to see if it's NULL or not. Oh, and I can't use Stored Procedures for reasons I won't go into :(. TIA, Pete

      E Offline
      E Offline
      Edbert P
      wrote on last edited by
      #2

      Well, you could always do something like this: SELECT id FROM tblContent WHERE ... AND Content IS NULL Modify it as you want to, the most important thing is the condition "column IS NULL" can be used to find out whether a column is null or not without having to download it. Hope it helps, Edbert

      M 1 Reply Last reply
      0
      • E Edbert P

        Well, you could always do something like this: SELECT id FROM tblContent WHERE ... AND Content IS NULL Modify it as you want to, the most important thing is the condition "column IS NULL" can be used to find out whether a column is null or not without having to download it. Hope it helps, Edbert

        M Offline
        M Offline
        moredip
        wrote on last edited by
        #3

        Hi Edbert, thanks for the reply. I'd thought of that, but I'm not sure how well it works if I'm displaying info about /every/ row in the table. I'd have to do a SELECT for each row. Plus, each row has about 5 different text fields (each 'page' of content has 5 'sub-pages'). So that would mean 5*num_rows SELECTs! I guess a similar approach would be to initially do the SELECT you suggest (for each of the 5 content columns), and then programmatically compare each id to the results. Hmmm, maybe that's what you meant in the first place. It's annoying that there doesn't seem to be a 'cleaner' way to do it, but then I guess SQL isn't really the most powerful thing for this kind of problem... Thanks for you help, Pete

        E 1 Reply Last reply
        0
        • M moredip

          Hi Edbert, thanks for the reply. I'd thought of that, but I'm not sure how well it works if I'm displaying info about /every/ row in the table. I'd have to do a SELECT for each row. Plus, each row has about 5 different text fields (each 'page' of content has 5 'sub-pages'). So that would mean 5*num_rows SELECTs! I guess a similar approach would be to initially do the SELECT you suggest (for each of the 5 content columns), and then programmatically compare each id to the results. Hmmm, maybe that's what you meant in the first place. It's annoying that there doesn't seem to be a 'cleaner' way to do it, but then I guess SQL isn't really the most powerful thing for this kind of problem... Thanks for you help, Pete

          E Offline
          E Offline
          Edbert P
          wrote on last edited by
          #4

          If you post the database structure and how you want your query to be I might be able to help you build the query.

          S M 2 Replies Last reply
          0
          • E Edbert P

            If you post the database structure and how you want your query to be I might be able to help you build the query.

            S Offline
            S Offline
            Scott Chappel
            wrote on last edited by
            #5

            You could use ISNULL( column, valueIfNull ) although you will still end up with the data if the column is not null. Your best bet would be to use a case e.g. CASE WHEN col IS NULL THEN 1 ELSE 0 END AS IsColNull. Please note this is not checked for syntax.

            1 Reply Last reply
            0
            • E Edbert P

              If you post the database structure and how you want your query to be I might be able to help you build the query.

              M Offline
              M Offline
              moredip
              wrote on last edited by
              #6

              Hi guys, I ended up re-normalising the database schema, a side-effect of which was that my NULL-checking problem is no longer an issue. Which is nice. I just wanted to say thanks for your help. Cheers, Pete

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups