Results 1 to 9 of 9

Thread: [RESOLVED] SQL WHERE NULL or NOT NULL

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Resolved [RESOLVED] SQL WHERE NULL or NOT NULL

    I'm looking to pull information from a SQL database and have let the user select 4 different options to show only the rows related to them.

    The table has two columns all based on dates (a start date and a finished date)
    The user can choose to view only the startdate, only the enddate, the dates that haven't finnished (are NULL),or all the dates.

    What command can i put in to my SQL search statement to find a field that has content in/not in?

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: SQL WHERE NULL or NOT NULL

    so far...
    PHP Code:
    $sqllook="SELECT * FROM enquiryform WHERE startdate IS NOT NULL AND enddate IS NOT NULL" 
    This is what I have, It shows all the information even if its empty? I've not put anything in the cells and Navicat 8 tells me they are NULL?

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SQL WHERE NULL or NOT NULL

    SQL Database as in SQL Server Database?

    What version?

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL WHERE NULL or NOT NULL

    Using SQL Server:


    StartDate and EndDate between Some Date
    SELECT * FROM enquiryform WHERE 'EnteredDate' BETWEEN StartDate and EndDate

    StartDate But Not Completed
    SELECT * FROM enquiryform WHERE StartDate IS NOT NULL and EndDate IS NULL

    Started after a date
    SELECT * FROM enquiryform WHERE StartDate >= 'SomeDateHere"

    Enddate after a date
    SELECT * FROM enquiryform WHERE EndtDate >= 'SomeDateHere"
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: SQL WHERE NULL or NOT NULL

    MySQL 4.1

    IS NOT NULL doesn't want to work, still shows row when I ask to avoid it
    PHP Code:
    $sqllook="SELECT * FROM enquiryform WHERE enddate IS NOT NULL" 
    Last edited by LingoOutsider; Jun 3rd, 2009 at 07:30 AM.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL WHERE NULL or NOT NULL

    I think that's because dates by nature cannot be null... most DBMSs will stick in a date of something like 12/31/1899 11:59:59pm for a "null" date... it might be that NaviCat is taking this into consideration, and when it comes across the date, it interprets it as NULL....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: SQL WHERE NULL or NOT NULL

    So is there a way for me to Identify if a field has been asigned if i'm looking a dates?

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL WHERE NULL or NOT NULL

    hard to say. I've used a check for "12/31/1899 11:59:59pm" before with success.... I also know that brucevde has also had success using the IS NULL syntax... go figure... I've not has a need for it in mySQL, so I'm not sure.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: SQL WHERE NULL or NOT NULL

    I hate working with dates. I always use unix timestamps instead. if you could switch, then a "null" date would just be equal to 0, and the SQL would be simple:
    Code:
    #has started, but hasn't ended
    SELECT * FROM table WHERE StartDate>0 AND EndDate=0
    this probably won't help you unless you want to switch, though!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width