Select Records Expert equivalent for SSRS - sql-server

Crystal Reports has the Select Records Expert which allows you to use variables to build WHERE conditions. Is there something similar to the select records experts in SSRS?
If the variable #Restriction below is equal to one, the column name CFE_EDI.ETAT = 'ENV' or CFE_EDI.ETAT = 'OUV'. I tried to mimic the functionality with a SQL query but due to AND and OR precedence, the WHERE condition is not functioning properly.

You can handle this within the SQL of your SSRS Dataset where clause buy using case statements that return a 1 or a 0 depending on whether or not the criteria is met:
where case when #Restriction = '1'
then case when CFE_EDI.ETAT in('ENV','OUV') then 1 else 0 end
when #Restriction = '0'
then case when CFE_EDI.ETAT not in('ENV','OUV') then 1 else 0 end
else case when CFE_EDI.ETAT <> '' then 1 else 0 end
end = 1

Related

How to modify multiple CASE statements

It's me again. I would just like to ask your opinion on how shall I modify my SELECT statament query. So basically I created a user defined field called "Force Schedule2", if this checkbox is tick (equals 1), then even if the item is on schedule 1, it will display on schedule 2. How shall I add it on my existing case statement?
my current SELECT statement is:
SELECT
CASE
WHEN
WorkOrder.DateCreated <
(
CASE
WHEN
(DATEPART(dw, dbo.ToBeScheduled_InProgress.Start) = 2)
THEN
(ToBeScheduled_InProgress.Start + 0.625) - 3
ELSE
(ToBeScheduled_InProgress.Start + 0.625) - 1
END
)
THEN
1
ELSE
2
END
AS ScheduleTime
and my user defined field for "Force Schedule2 is:
dbo.AdditionalInfo.UserDefined3 AS ForceSched
It's not obvious what you're trying to do here with your date operation. You could do something simple like this:
CASE
WHEN dbo.AdditionalInfo.UserDefined3 = 1
THEN 2
WHEN WorkOrder.DateCreated < (CASE WHEN (DATEPART(dw, dbo.ToBeScheduled_InProgress.Start) = 2) THEN (ToBeScheduled_InProgress.Start + 0.625) - 3 ELSE (ToBeScheduled_InProgress.Start + 0.625) - 1 END)
THEN 1
ELSE 2
END AS ScheduleTime
Basically it hits the condition and exits the statement. If you flipped it so your date operation was first it would return 1, so you still need to think about the order you're entering things into case statement.

SQL CASE when text field is NULL not working

I'm trying to run the following case statement in SQL:
CASE
WHEN P.Photos_Cloned_From IS NULL
THEN 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.ProductCode+'-2T.jpg'
ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.Photos_Cloned_From+'-2T.jpg'
END AS image_link,
The statements works, but only for those records that are not NULL. For items that are NULL, the statement is not returning the THEN condition.
Any suggestions?
Try this and let us know what happens.
'http://www.toolboxbarn.com/v/vspfiles/photos/'+
COALESCE(P.Photos_Cloned_From,P.ProductCode,'DEFAULT')+'-2T.jpg'
AS image_link,
Using coalesce here is better than the case statement. Some platforms can optimize coalesce and it lets you easily make a default value.
#hogan's Suggestion is a great one to save code, but ultimately it should have the same result as your case statement if you don't introduce his new 'default' case. It is most likely that the ProductCode is also NULL is that a possibility?
Why string + NULL = NULL because NULL is an unknown in sql most db platforms will nullify the entire value when null is aggregated or concatenated.
So Think of the following test cases:
Id ProductCode Photos_Cloned_From
1 1 ClonedFrom
2 2 Null
3 NULL Null
The results of your case expression and Hogan's Suggestion would be:
1) ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/ClonedFrom-2T.jpg'
2) WHEN 'http://www.toolboxbarn.com/v/vspfiles/photos/2-2T.jpg'
3) WHEN NULL
CASE WHEN ISNULL(P.Photos_Cloned_From,'') = '' THEN 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.ProductCode+'-2T.jpg'
ELSE 'http://www.toolboxbarn.com/v/vspfiles/photos/'+P.Photos_Cloned_From+'-2T.jpg'
END AS image_link,
SELECT
F.Name
, F.Address
, F.InDate
, ( CASE WHEN (F.Date_Down IS NULL and F.Cod_Down = 0 )THEN 0 ELSE -1 END) as 'sn_Down'
FROM
Folders F

case when but two columns instead of one column

I need some advice and suggestions if there is available some way of doing something like this:
SELECT Status=Case
WHEN clflf.product='active'
THEN 'Active' ELSE 'NotActive' END;
this basically creates active or notactive in one column.
Is there some way of doing a case when or some other technique so that I can have two columns, 'Active' and 'NotActive'?
You need separate case statements, like so.
Select StatusActive = Case when clflf.product='active' then 1 ELSE 0 end
,StatusNotActive = Case when clflf.product <>'active' then 1 ELSE 0 end
You can always do
SELECT active,
1 - active AS notactive
FROM clflf
CROSS APPLY (SELECT CASE
WHEN clflf.product = 'active' THEN 1
ELSE 0
END) CA(active)

How to get all values including null in a SQL Server case statement?

I have a big stored procedure, and basically I want to select all values (including null) if my variable #DimBrowserId is set to 0. I am using a case statement, however this is only catching values that actually have something and ignoring the NULL valued fields. Because I am using the = clause in the WHERE I cannot do IS NULL. I do not want to have to write 2 IF statements because the stored procedure would then be enormous, so I want to know how to get null values as well. Here is my code:
SELECT
DATEPART(yy, DATEADD(mi, #Mdelta, d.DimDateValue)),
DisableCount = COUNT(*)
FROM
dbo.FactDisable AS f
JOIN
dbo.DimDate AS d ON f.DimDateId = d.DimDateId
JOIN
dbo.DimDevice AS v ON f.DimDeviceId = v.DimDeviceId
WHERE
d.DimDateValue >= #StartDateGMT
AND d.DimDateValue <= #EndDateGMT
AND f.IsTest = #IncludeTest
AND f.DimProductId = #DimProductId
AND v.DimBrowserId = CASE
WHEN #DimBrowserId = 0 THEN v.DimBrowserId
ELSE #DimBrowserId
END
GROUP BY
DATEPART(yy, DATEADD(mi, #Mdelta, d.DimDateValue))
The code is near the CASE clause.
Thanks
Change that line to be
AND (#DimBrowserID = 0 OR #DimBrowserID = v.DimBrowserId)
If #DimBroserID is 0 then no filtering will be applied for this line.
Use ISNULL:
SELECT DATEPART(yy,DATEADD(mi,#Mdelta,d.DimDateValue)),
DisableCount=COUNT(*)
FROM dbo.FactDisable AS f
JOIN dbo.DimDate AS d ON f.DimDateId = d.DimDateId
JOIN dbo.DimDevice AS v ON f.DimDeviceId = v.DimDeviceId
WHERE d.DimDateValue >= #StartDateGMT AND d.DimDateValue <= #EndDateGMT
AND f.IsTest = #IncludeTest AND f.DimProductId = #DimProductId
AND v.DimBrowserId = CASE WHEN ISNULL(#DimBrowserId,0) = 0 THEN v.DimBrowserId ELSE #DimBrowserId END
GROUP BY DATEPART(yy,DATEADD(mi,#Mdelta,d.DimDateValue))
CASE WHEN COALESCE(#MightBeNull, 0) = 0 THEN ZeroResult ...
will be treated as zero if #MightBeNull is null, and whatever #MightBeNull is if it's not null.
Assuming null means any browser, a better data model for this scenario might be to set an ID that identifies any browser, instead of setting it to null.
You probably know what you are running into is NULL does not equal NULL in a comparison.
Assuming you don't have control of the data model to fix that, one option would be to coalesce your NULL values to an unused id.
The resulting WHERE clause would look like this, assuming -1 is the unused value you choose.
AND COALESCE(v.DimBrowserId, -1) = CASE WHEN #DimBrowserId = 0 THEN COALESCE(v.DimBrowserId, -1) ELSE #DimBrowserId END

How do I handle Error in sql queries?

For sql queries like..
select Quantity_Books/datepart(hour,Rent_Hour) from Rent_Book where (some conditions..)
They will return error when datepart(hour,Rent_Hour) is 0.
If sth like that Happens, I would like to show 0.
I know I should use case when But I am not really sure how..
Or any other better method?
You'd simply test the value first
select
CASE
WHEN datepart(hour,Rent_Hour) = 0 THEN 0
ELSE Quantity_Books/datepart(hour,Rent_Hour)
END
from
Rent_Book where (some conditions..)
Alternatively, use NULL rules
ISNULL((Quantity_Books / (NULLIF(datepart(hour,Rent_Hour), 0))), 0)
select case when datepart(hour,Rent_Hour)<>0 then Quantity_Books/datepart(hour,Rent_Hour) else 0 end as col ...

Resources