Set variable =NULL

  • In my stored proc, I'm trying to set/pass a datetime variable to NULL. Then I want to check if that variable is NULL. if yes then do A else do B. For some reason even though the value of the variable appears to be NULL it's evaluting the the NULL as false.

    here's my code

    spTest (@currentDate datetime, @BegDate datetime=NULL, @EndDate datetime =NULL)

    If @BegDate=NULL

           Select * From TableA Where EntryDate Between @BegDate and @EndDate

    Else

        Select * From TableA Where EntryDate Between @CurrentDate and @EndDate

     

    any ideas?

    sam

     

  • CREATE PROC spTest (@currentDate datetime, @BegDate datetime=NULL, @EndDate datetime =NULL) AS

    If @BegDate IS NULL

           Select * From TableA Where EntryDate Between @BegDate and @EndDate

    Else

        Select * From TableA Where EntryDate Between @CurrentDate and @EndDate

  • Try this:

    DECLARE @BegDate datetime

    SET @BegDate = NULL

    IF @BegDate = NULL

         PRINT 'NULL equals NULL'

    IF @BegDate IS NULL

         PRINT 'NULL IS NULL'

    ELSE IF ISNULL( @BegDate, '01/01/1900') = '01/01/1900'

         PRINT 'NULL does not equal NULL'

     

    Did not see SQL ORACLE's post.  This basically shows you that NULL does not equal NULL. 

    I wasn't born stupid - I had to study.

  • it worked

     

    thank u

    sam

  • From BOL

    When SET ANSI_NULLS is OFF, the Equals (=) and Not Equal To  [<>] comparison operators do not follow the SQL-92 standard. A SELECT statement using WHERE column_name = NULL returns the rows with null values in column_name

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • It didn't work

    Capture

    Thx

    sam

     

    • This reply was modified 5 years ago by  remitos.
    • This reply was modified 5 years ago by  remitos.
    Attachments:
    You must be logged in to view attached files.
  • remitos wrote:

    It didn't work

    Capture

    Thx

    sam

    First of all, that's an Oracle error.  Oracle and SQL Server haven a whole lot of differences.  PL/SQL <> T-SQL.  Let's hope that whoever "SQL Oracle" is, also knows Oracle instead of just claiming to be an "oracle" at SQL.

    Second, there are multiple answers on this thread.  You need to post the code that caused the error.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply