case in where condition

  • i have this query

    select id from sale_Invoice where invoiceDate<=@OnDate and (case when @OBDate is null then invoiceDate>=@OBDate else 1=1 end)

    but

    i want this condition 'invoiceDate>=@OBDate' in query only when @OBDate is not null

  • 01)

    select id from sale_Invoice

    where invoiceDate <= @OnDate

    and invoiceDate >= isnull(@OBDate,invoiceDate)

    02)

    select id from sale_Invoice

    where invoiceDate<=@OnDate

    and invoiceDate >= case when @OBDate is null then invoiceDate else @OBDate end

    03)

    declare @sql nvarchar(100),

    @sql1 nvarchar(100),

    @sql2 nvarchar(100)

    set @sql1 = 'select id from sale_Invoice

    where invoiceDate <= '+ @OnDate

    set @sql2 = 'and invoiceDate >= '+@OBDate

    if @OBDate is null

    begin

    set @sql = @sql1

    exec sp_executesql @sql

    end

    else

    begin

    set @sql = @sql1+@sql2

    exec sp_executesql @sql

    end

  • thank you:-)

Viewing 3 posts - 1 through 2 (of 2 total)

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