How to put If Else condition to check one coulmn value is present in another set of column values in sql 2008

  • [font="Courier New"]Hi All,

    I have main data set which consists of lacks of records and below is my query

    DECLARE @USER_DT DATETIME

    SET @USER_DT = '2013-08-01'

    DECLARE @RANGE_DATE DATETIME

    SELECT distinct @RANGE_DATE = RANGE_DATE

    FROM

    dbo.TABLE

    WHERE

    RANGE_DATE >= (CONVERT(VARCHAR(8), DATEADD (M, -2, GETDATE()), 21)+ '26')

    AND

    RANGE_DATE <= (CONVERT(VARCHAR(8), DATEADD (M, -1, GETDATE()), 21)+ '25')

    IF(@USER_DT = @RANGE_DATE )

    BEGIN

    SELECT*

    FROMMAIN_TABLE

    WHERE

    USER_DATE = @USER_DT

    AND

    RANGE_DATE >= (CONVERT(VARCHAR(8), DATEADD (M, -2, GETDATE()), 21)+ '26')

    AND

    RANGE_DATE <= (CONVERT(VARCHAR(8), DATEADD (M, -1, GETDATE()), 21)+ '25')

    END

    BEGIN

    SELECT*

    FROMMAIN_TABLE

    WHERE

    USER_DATE = @USER_DT

    AND

    RANGE_DATE >= (CONVERT(VARCHAR(8), DATEADD (M, -1, GETDATE()), 21)+ '26')

    AND

    RANGE_DATE <= (CONVERT(VARCHAR(8), DATEADD (M, 0, GETDATE()), 21)+ '25')

    END

    i.e. @USER_DT is for user selection in SSRS Report window. Now, let say he has enter '2013-08-01' now second parameter @RANGE_DT is created for storing distinct dates values now my problem is I want to check that user entered date in @Range_DT and if it is present as shown in the above query i will get desired output. But thing is we can check only single value in If-Else and I wanna check single date value is present or not and displya output accordingly. Is there any way to do this (This later i am going to put in SSRS report).[/font]

  • You probably want something like:

    IF EXISTS (SELECT *

    FROM dbo.TABLE

    WHERE RANGE_DATE >= (CONVERT(VARCHAR(8), DATEADD (M, -2, GETDATE()), 21)+ '26')

    AND RANGE_DATE <= (CONVERT(VARCHAR(8), DATEADD (M, -1, GETDATE()), 21)+ '25')

    AND RANGE_DATE = @USER_DT)

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

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

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