Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

IIf() function in SQL statements

arvinarvin Posts: 22Member
hi guys,

i was wondering why this one does not work?

[code]

Select
DateDiff("d",dbo.Table1.regdate,IIf(IsNull(dbo.Table1.compdate),GetDate(),dbo.Table1.compdate)) As Days
Where blah blah...

[/code]

it supposed to get the difference of 2 dates namely regdate and compdate. but if compdate is null replace compdate with the date returned by GetDate() function.

this one works with Access 2000 crosstab queries, but not on SQL Server 7.0.

thanks in advance,

arvin

Comments

  • 0100100101001001 Posts: 22Member
    I haven't tried the code, but if it doesn't work in SQL it might have to do with the way SQL statements differ from Access to SQL. Access on many cases breaks away from standard SQL rules and has it's own way of building SQL statements which do not work in SQL



    : hi guys,
    :
    : i was wondering why this one does not work?
    :
    : [code]
    :
    : Select
    : DateDiff("d",dbo.Table1.regdate,IIf(IsNull(dbo.Table1.compdate),GetDate(),dbo.Table1.compdate)) As Days
    : Where blah blah...
    :
    : [/code]
    :
    : it supposed to get the difference of 2 dates namely regdate and compdate. but if compdate is null replace compdate with the date returned by GetDate() function.
    :
    : this one works with Access 2000 crosstab queries, but not on SQL Server 7.0.
    :
    : thanks in advance,
    :
    : arvin
    :

  • lonuellonuel Posts: 1Member
    ISNULL
    Replaces NULL with the specified replacement value.

    Syntax
    ISNULL ( check_expression , replacement_value )

    Example:

    declare @RegDate as DateTime, @CompDate as DateTime
    set @RegDate = '12/10/2001 20:44'
    set @CompDate = null
    Select DateDiff(d,@RegDate,IsNull(@CompDate,GetDate())) As Days

    I hope this helps,
    Lon
    : hi guys,
    :
    : i was wondering why this one does not work?
    :
    : [code]
    :
    : Select
    : DateDiff("d",dbo.Table1.regdate,IIf(IsNull(dbo.Table1.compdate),GetDate(),dbo.Table1.compdate)) As Days
    : Where blah blah...
    :
    : [/code]
    :
    : it supposed to get the difference of 2 dates namely regdate and compdate. but if compdate is null replace compdate with the date returned by GetDate() function.
    :
    : this one works with Access 2000 crosstab queries, but not on SQL Server 7.0.
    :
    : thanks in advance,
    :
    : arvin
    :

Sign In or Register to comment.