Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to put If Else condition to check values

Author  Topic 

bhushan_juare
Starting Member

45 Posts

Posted - 2013-08-31 : 01:39:48
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 *
FROM MAIN_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 *
FROM MAIN_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).

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-08-31 : 04:55:01
no need of variable in that case. just use a IF EXISTS check like

IF EXISTS (SELECT 1
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
)
...
ELSE
..
END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -