Dynamic Case Expression Based on Dates - sql

I have a SQL query that has multiple conditions that will change the WHERE clause. I have three case expressions that dynamically update this WHERE clause. The last two case expressions are working well. However, the first one is more dynamic than the last two and I am having issues with it.
Here is the portion of the table that is applicable to this exercise
(Image of Table)
I need it to work as follows:
When my variable #selection = 1, I need it to sort my return based on two dates being passed in.
When my variable #selection = {any other int}, I need it to return all dates.
Query with Description
...
WHERE
{Start Date} = (case when #selection = 1 then {return all values where Start Date is between two dates that are passed in} else {return all Start Dates})
...
Entire Query
DECLARE #selection integer ;
DECLARE #items integer ;
DECLARE #washTypes integer ;
SET #selection = {Root Container.Selection Group.Selection Checkbox.controlValue} ;
SET #items = {Root Container.Items Group.Items Checkbox.controlValue} ;
SET #washTypes = {Root Container.Types of Wash Group.Wash Types Checkbox.controlValue} ;
SELECT
RAW_CIP_records_ndx as 'Index',
start as 'Start',
stop as 'End',
total_duration as 'Total Duration',
item as 'Item',
wash_type as 'Type of Wash',
operator as 'CIP Operator',
program_complete as 'Program Fully Completed?'
FROM RAW_CIP_records
WHERE
start = (case when #selection = 1 then (BETWEEN '{Root Container.Start Date.formattedDate}' and '{Root Container.End Date.formattedDate}') else start end)
and
item = (case when #items = 0 then 'Receiving Bay 1' when #items = 1 then 'Receiving Bay 2' when #items = 2 then 'Receiving Bay 3' else item end)
and
wash_type = (case when #washTypes = 0 then 'Regular' when #washTypes = 1 then 'Sanitize' when #washTypes = 2 then 'Acid' else wash_type end)
I can get a simple WHERE clause to work with these two dates and the BETWEEN function. However, I can't figure out how to pass all of this into a CASE expression.

You could do it a WHERE condition in one query
WHERE (
#selection = 1 AND (start BETWEEN #startDate and #endDate)
OR #selection <> 1
)
But I would suggest you are better off with two separate queries as it will be more likely to use an index.
IF #selection = 1
SELECT ...
ELSE
SELECT ...

Maybe something like this:
WHERE
1 = (case when #selection = 1 then
case when start BETWEEN '{Root Container.Start Date.formattedDate}' and '{Root Container.End Date.formattedDate}' then 1 else 0 end
else 1
end)
and
item = ...
Warning: this will most likely not make use of any indexes, so only use if performance is acceptable in your case.

Related

Add column name to a variable and use it in later calculation in WHERE clause

I have a problem. I need to determine the name of the column under which the calculations will continue. So I wrote a select:
DECLARE #column VARCHAR(MAX)
DECLARE #ColumnA VARCHAR(MAX)
DECLARE #ColumnB VARCHAR(MAX)
SET #ColumnA = 'RegistrationDate'
SET #ColumnB = 'EntryDate'
SET #column = CASE
WHEN CONVERT(DATE,GETDATE()) NOT IN (
'2021-08-04','2021-08-05','2021-08-06','2021-08-07','2021-08-08','2021-08-09','2021-08-10','2021-09-07','2021-09-08','2021-09-09','2021-09-10','2021-09-11',
'2021-09-12','2021-09-13','2021-10-05','2021-10-06','2021-10-07','2021-10-08','2021-10-09','2021-10-10','2021-10-11','2021-11-09','2021-11-10','2021-11-11','2021-11-12','2021-11-13','2021-11-14','2021-11-15','2021-12-07',
'2021-12-08','2021-12-09','2021-12-10','2021-12-11','2021-12-12','2021-12-13'
) THEN
QUOTENAME(#Column)
ELSE
QUOTENAME(#ColumnB)
END
SELECT #column
which returns me [RegistrationDate] or [EntryDate] and stores this in variable #column. Now, when I know under which column should I calculate, I want to insert this variable #column in to my main select one of the WHERE clause:
DECLARE #column VARCHAR(MAX)
DECLARE #ColumnA VARCHAR(MAX)
DECLARE #ColumnB VARCHAR(MAX)
SET #ColumnA = 'RegistrationDate'
SET #ColumnB = 'EntryDate'
SET #column = CASE
WHEN CONVERT(DATE,GETDATE()) NOT IN (
'2021-08-04','2021-08-05','2021-08-06','2021-08-07','2021-08-08','2021-08-09','2021-08-10','2021-09-07','2021-09-08','2021-09-09','2021-09-10','2021-09-11',
'2021-09-12','2021-09-13','2021-10-05','2021-10-06','2021-10-07','2021-10-08','2021-10-09','2021-10-10','2021-10-11','2021-11-09','2021-11-10','2021-11-11','2021-11-12','2021-11-13','2021-11-14','2021-11-15','2021-12-07',
'2021-12-08','2021-12-09','2021-12-10','2021-12-11','2021-12-12','2021-12-13'
) THEN
QUOTENAME(#Column)
ELSE
QUOTENAME(#ColumnB)
END
SELECT
CASE WHEN final.Branch IS NULL THEN 'Total'
ELSE final.Branch
END AS 'Branch',
final.TR
FROM
(
SELECT
CASE
WHEN main.BRANCHNO = 1 THEN 'One'
WHEN main.BRANCHNO = 2 THEN 'Two'
WHEN main.BRANCHNO = 3 THEN 'Three'
WHEN main.BRANCHNO = 4 THEN 'Four'
WHEN main.BRANCHNO = 5 THEN 'Five'
WHEN main.BRANCHNO = 6 THEN 'Six'
END AS 'Branch',
COUNT(*) AS 'TR'
FROM
(
SELECT
*
FROM
[TABLE]
WHERE
Status = 100
AND
BRANCHNO IN (1,2,3,4,5,6)
AND
Type = 'TR'
AND
**#column** = CONVERT(DATE, CASE WHEN DATENAME(dw, getdate()) = 'Monday' THEN getdate()-3 ELSE getdate()-1 END
)
) AS main
GROUP BY
main.BRANCHNO WITH ROLLUP
) AS final
But when I execute query it returns me an error:
Msg 241, Level 16, State 1, Line 11 Conversion failed when converting
date and/or time from character string.
I imagined everything very simple: I put a column name into a variable, and then, that name placed at the beginning of the WHERE clause will be recognized as the column name and then *= CONVERT(DATE, CASE WHEN DATENAME(dw, getdate()) etc will do all work.
But that did not happen. Maybe someone knows why and maybe they know how to solve this task?
You can't use a variable to reference a column name. #column is just a piece of data, which just so happens to contain a column name as a string, but it's still just a string, not actually a reference to a column in a table.
Some options you have seem to be...
AND CASE #column WHEN 'RegistrationDate' THEN RegistrationDate
WHEN 'EntryDate' THEN EntryDate
END
=
CONVERT(DATE, CASE WHEN DATENAME(dw, getdate()) = 'Monday' THEN getdate()-3 ELSE getdate()-1 END)
Or, have two queries which only differ in the column being referenced...
IF (#column = 'RegistrationDate')
<query1>
ELSE IF (#column = 'EntryDate')
<query2>
Or "Dynamic SQL" where you build up a new string with your SQL code and execute that by call sp_executesql (assuming this is SQL Server, which it appears to be).
I recommend reading this : https://www.sommarskog.se/dyn-search.html
EDIT: A pure SQL alternative, assuming SQL Server
DECLARE #mode INT = CASE
WHEN CONVERT(DATE,GETDATE()) NOT IN (
'2021-08-04','2021-08-05','2021-08-06','2021-08-07','2021-08-08','2021-08-09','2021-08-10','2021-09-07','2021-09-08','2021-09-09','2021-09-10','2021-09-11',
'2021-09-12','2021-09-13','2021-10-05','2021-10-06','2021-10-07','2021-10-08','2021-10-09','2021-10-10','2021-10-11','2021-11-09','2021-11-10','2021-11-11','2021-11-12','2021-11-13','2021-11-14','2021-11-15','2021-12-07',
'2021-12-08','2021-12-09','2021-12-10','2021-12-11','2021-12-12','2021-12-13'
) THEN
0
ELSE
1
END;
DECLARE #filter_date DATE = CONVERT(DATE, CASE WHEN DATENAME(dw, getdate()) = 'Monday' THEN getdate()-3 ELSE getdate()-1 END;
WITH
source AS
(
SELECT
*
FROM
[TABLE]
WHERE
Status = 100
AND BRANCHNO IN (1,2,3,4,5,6)
AND Type = 'TR'
),
filtered_source AS
(
SELECT 0 AS mode, * FROM source WHERE RegistrationDate = #filter_date
UNION ALL
SELECT 1 AS mode, * FROM source WHERE EntryDate = #filter_date
)
SELECT
COALESCE(
CASE
WHEN BRANCHNO = 1 THEN 'One'
WHEN BRANCHNO = 2 THEN 'Two'
WHEN BRANCHNO = 3 THEN 'Three'
WHEN BRANCHNO = 4 THEN 'Four'
WHEN BRANCHNO = 5 THEN 'Five'
WHEN BRANCHNO = 6 THEN 'Six'
END,
'Total'
)
AS 'Branch',
COUNT(*) AS 'TR'
FROM
filtered_source
WHERE
mode = #mode
GROUP BY
GROUPING SETS (
(mode),
(mode, BRANCHNO)
);
By always including mode in the GROUPING SETS, the optimiser might be able to yield a better execution plan for the two scenarios.
Still read the link given above though, at the very least to understand why this is necessary, or perhaps why it doesn't quite manage to yield the best execution plan.

Using different set of WHERE clauses in stored procedure depending on Parameter value

I have 2 stored procedures which return the same columns that I am trying to merge into a single procedure. They both have a different set of parameters and both have different WHERE clauses, but they use the same tables and select the exact same rows.
WHERE clause 1: (uses #UIOID, and #Level)
WHERE ( #UIOID = CASE WHEN #Level = 'Single' THEN C.C_UIOID_PK
WHEN #Level = 'Children' THEN CLC.UIOL_P
WHEN #Level = 'Parent' THEN CLP.UIOL_C
END
OR ( #UIOID = '0'
AND #Level = 'All'
)
)
Where clause 2: (Uses #TeamCode, #Year, #IncludeQCodes)
WHERE C.C_IsChild = 0
AND C.C_MOA <> 'ADD'
AND #TeamCode = C.C_OffOrg
AND C.C_Active = 'Y'
AND ( #Year BETWEEN dbo.f_GetAcYearByDate(C.C_StartDate) AND dbo.f_GetAcYearByDate(C.C_EndDate)
OR #Year = 0 )
AND ( C.C_InstCode NOT LIKE 'Q%'
OR #IncludeQCodes = 1 )
Ideally I want to add a new parameter which basically tells it which of the two WHERE clauses to run, but I can't seem to recreate that with CASE statement because as far as I can tell, they only work for a single WHERE clause, not a whole set of different clauses
I want to do this without having to repeat the select statement again and putting the whole thing in IF statements, and i don't want to put the query into a string either. I just want one select statement ideally.
The problem with using temp tables is the query itself takes a while to run without any parameters and is used in a live website, so I don't want it to have to put all records in a temp table and then filter it.
The problem with using a CTE is you can't follow it with an IF statement, so that wouldn't work either.
Here is the sort of logic I am trying to achieve:
SELECT A
B
C
FROM X
IF #WhichOption = 1 THEN
WHERE ( #UIOID = CASE WHEN #Level = 'Single' THEN C.C_UIOID_PK
WHEN #Level = 'Children' THEN CLC.UIOL_P
WHEN #Level = 'Parent' THEN CLP.UIOL_C
END
OR ( #UIOID = '0'
AND #Level = 'All'
)
)
ELSE IF #WhichOption = 2 THEN
WHERE C.C_IsChild = 0
AND C.C_MOA <> 'ADD'
AND #TeamCode = C.C_OffOrg
AND C.C_Active = 'Y'
AND ( #Year BETWEEN dbo.f_GetAcYearByDate(C.C_StartDate) AND dbo.f_GetAcYearByDate(C.C_EndDate)
OR #Year = 0 )
AND ( C.C_InstCode NOT LIKE 'Q%'
OR #IncludeQCodes = 1 )
Save the following process in a procedure. You can also directly insert into a physical table.
declare #varTable Table (columns exactly as Procedures return)
if(condition is met)
begin
insert into #varTable
exec proc1
end
else
begin
insert into #varTable
exec proc2
end
Add the parameter that you said that it would indicate what filter apply :
select XXXXX
from XXXXX
where (#Mode = 1 and ( filter 1 ))
or
(#Mode = 2 and ( filter 2 ))
option(recompile)
If the #Mode parameter is 1 then it will evaluate the filter 1, otherwise it will evaluate the filter 2.
Add an option(recompile) at the end of the statement, so the SQL engine will replace the variables with their values, eliminate the filter that won't be evaluated, and generate an execution plant for just the filter that you want to apply.
PS: Please notice that although these catchall queries are very easy to code and maintain, and generate a perfectly functional and optimal execution, they are not advised for high-demand applications. The option(recompile) forces the engine to recompile and generate a new execution plan at every execution and that would have a noticeable effect on performance if your query needs to be executed hundreds of times per minute. But for the occasional use it's perfectly fine.
Try to use dynamic SQL:
DECLARE #sql NVARCHAR(max), #where NVARCHAR(max), #WhichOption INT = 1;
SET #sql = 'SELECT A
B
C
FROM X';
IF #WhichOption = 1
SET #where = 'WHERE ( #UIOID = CASE WHEN #Level = ''Single'' THEN C.C_UIOID_PK
WHEN #Level = ''Children'' THEN CLC.UIOL_P
WHEN #Level = ''Parent'' THEN CLP.UIOL_C
END
OR ( #UIOID = ''0''
AND #Level = ''All''
)
)';
ELSE IF #WhichOption = 2
SET #where = ' WHERE C.C_IsChild = 0
AND C.C_MOA <> ''ADD''
AND #TeamCode = C.C_OffOrg
AND C.C_Active = ''Y''
AND ( #Year BETWEEN dbo.f_GetAcYearByDate(C.C_StartDate)
AND dbo.f_GetAcYearByDate(C.C_EndDate)
OR #Year = 0 )
AND ( C.C_InstCode NOT LIKE ''Q%''
OR #IncludeQCodes = 1 ) ';
SET #sql = CONCAT(#sql,' ', #where)
PRINT #sql
EXECUTE sp_executesql #sql

SQL - want to write unique error value per IF condition

I'm handing lots of data and error checking it to ensure that it follows a correct format. So I'm creating a lot of IF conditions and so far I can group lots of conditions but only output one kind of error .. or i end up writing a heck of a lot of code per condition, just to have a crystal clear error output.
Is there a way to put into the IF condition a specific error output string?
Here is my code:
-- set the initial #IntHosp_SUM value
SELECT #IntHosp_SUM = 0
-- Store the sum value to #IntHosp_SUM
SELECT #IntHosp_SUM =
(
(COALESCE (CASE WHEN #Int1_5hosp = 1 THEN COUNT (Int1_5) ELSE NULL END, 0)) +
(COALESCE (CASE WHEN #Int2_5hosp = 1 THEN COUNT (Int2_5) ELSE NULL END, 0)) +
(COALESCE (CASE WHEN #Int3_5hosp = 1 THEN COUNT (Int3_5) ELSE NULL END, 0)) +
(COALESCE (CASE WHEN #Int4_5hosp = 1 THEN COUNT (Int4_5) ELSE NULL END, 0)) +
(COALESCE (CASE WHEN #Int5_5hosp = 1 THEN COUNT (Int5_5) ELSE NULL END, 0)) +
(COALESCE (CASE WHEN #Int6_5hosp = 1 THEN COUNT (Int6_5) ELSE NULL END, 0))
)
FROM ASSSIST2_M0Teacher
-- Check RESUSE_SUM Value against Intervention Entries
SELECT #ErrorID = IsNull(Max(ErrorID) + 1, 1)
FROM ErrorTemp
WHERE (Quest = #DB + #Quest) AND (ValidateID = #ValidateID)
SELECT #Error = 0
IF (#IntHosp_SUM = 1 and #Hosp1_info is null )
or (#IntHosp_SUM = 2 and (#Hosp1_info is null or #Hosp2_info is null))
or (#IntHosp_SUM >=3 and (#Hosp1_info is null or #Hosp2_info is null or #Hosp3_info is null))
or (#IntHosp_SUM = 0 and (#Hosp1_info is not null or #Hosp2_info is not null or #Hosp3_info is not null ))
or (#IntHosp_SUM = 1 and (#Hosp2_info is not null or #Hosp3_info is not null ))
or (#IntHosp_SUM >= 2 and #Hosp3_info is not null)
BEGIN
SELECT #Error = 1
END
BEGIN
IF #Error = 1
INSERT INTO ErrorTemp
(Quest,
ValidateID,
ErrorID,
ErrorType,
ErrorDesc,
Field)
VALUES (#DB + #Quest,
#ValidateID,
#ErrorID,
'Validation',
'Too much or no info present, so incorrect',
'Hosp Info - admissions')
SELECT #ErrorID = #ErrorID + 1
END
I have declared all variables and where they are in the DB, but didn't want to clutter with extra code. You can see that I sum a series of fields if they contain the value 1 .. I don't want to add any other values, just those with '1'.
My IF conditions then check if that if there is a sum of 1, there is one entry. If 2, then two entries .. we have to be thorough and check the inverse of that, so if the sum value is 0, then no entries are made and so forth.
Is there a way to pass a string from the IF / OR condition to the error output so that I can write a unique error per condition?
SQL Server Management Studio 17.2

SSRS - Empty value

I have a parameter #Destinataire in SSRS that has a list of values plus an empty string
I've created a query that I set as available values, which gives me the drop down list
SELECT code_name FROM tableA UNION ALL SELECT ''
When running the reports with the empty string, I have no results
I tried to set the parameter as text box and it does not do anything too
Yet when running the sql query I'm using to run this report, things are fine as I have all my rows retrieved (see query below)
DECLARE #DateCmt DATE = '05/09/2015',
#DateFin DATE = '05/09/2016',
#Restriction INT = 1,
#Destinataire VARCHAR(5) = ''
--
;
--SELECT #DateCmt,#DateFin
SELECT DISTINCT
CFE_EDI.IU_LIASSE
,CFE_EDI.ETAT
,CFE_EDI.DATHRMAJ -- nouveau
,CFE_EDI.ESP_APPLI
,CFE_EDI.NOM_RS
,PARTENAIRES.LIBEL
,PARTENAIRES.CODE_INSEE
,CFE_EDI.DATHR_ENV -- nouveau
,CFE_EDI.DATHR_MEF -- nouveau
,CFE_EDI.DATHR_PRE -- nouveau
,CFE_EDI.DATHR_OUV -- nouveau
--,CFE_EDI.DATEHR_DEPOT-- mettre l'heure
,CFE_EDI.GESTDEL
--,CFE_SERVICE_DEST.IU_DEST
--,CFE_SERVICE.IU_LIASSE
,CASE WHEN CFE_EDI.ETAT = 'MEF' THEN 'En Attente le'
WHEN CFE_EDI.ETAT = 'PRE' THEN 'Préparé le'
WHEN CFE_EDI.ETAT = 'ENV' THEN 'Envoyé le'
WHEN CFE_EDI.ETAT = 'OUV' THEN 'Réceptionné le'
WHEN CFE_EDI.ETAT = 'NRM' THEN 'Non remis le'
WHEN CFE_EDI.ETAT = 'NAQ' THEN 'Non acquitté le'
END AS ChampEtat
,CASE WHEN CFE_EDI.ETAT = 'OUV' THEN 'Date d''envoi : ' + CONVERT(VARCHAR,CFE_EDI.DATHR_ENV,103)
END AS Date_Envoi,
CASE
WHEN CFE_EDI.ETAT='MEF' THEN CONVERT(VARCHAR,CFE_EDI.DATHR_MEF,103)
WHEN CFE_EDI.ETAT='PRE' THEN CONVERT(VARCHAR,CFE_EDI.DATHR_PRE,103)
WHEN CFE_EDI.ETAT='ENV' THEN CONVERT(VARCHAR,CFE_EDI.DATHR_ENV,103)
WHEN CFE_EDI.ETAT='OUV' THEN CONVERT(VARCHAR,CFE_EDI.DATHR_OUV,103)
ELSE CONVERT(VARCHAR,CFE_EDI.DATHR_DEPOT,103) END AS DateMaj ,
CASE
WHEN CFE_EDI.ETAT='MEF' then CONVERT(VARCHAR,CFE_EDI.DATHR_MEF,108)
WHEN CFE_EDI.ETAT='PRE' then CONVERT(VARCHAR,CFE_EDI.DATHR_PRE,108)
WHEN CFE_EDI.ETAT='ENV' then CONVERT(VARCHAR,CFE_EDI.DATHR_ENV,108)
WHEN CFE_EDI.ETAT='OUV' then CONVERT(VARCHAR,CFE_EDI.DATHR_OUV,108)
ELSE CONVERT(VARCHAR,CFE_EDI.DATHR_DEPOT,108) END AS HeureMaj,
PARTENAIRES.LIBEL + '(' + CFE_EDI.CODE_INSEE + ')' AS LibelDestinataire
--,CASE WHEN #Restriction = 1 THEN '1'
-- WHEN #Restriction = 0 THEN '0' END AS Restriction
,CASE WHEN #DateCmt != #DateFin AND #DateCmt < #DateFin THEN 'Diffusion Xml du ' + CONVERT(VARCHAR,(#DateCmt),103) + ' au ' + CONVERT(VARCHAR,(#DateFin),103) ELSE
'Diffusion EDI Xml du ' + CONVERT(VARCHAR,#DateCmt,103) END AS Plage_Diffusion
-- INTO
FROM
(PARTENAIRES
INNER JOIN dbo.CFE_EDI ON PARTENAIRES.CODE_INSEE = CFE_EDI.CODE_INSEE)
INNER JOIN dbo.CFE_SERVICE ON CFE_EDI.IU_LIASSE = CFE_SERVICE.IU_LIASSE
INNER JOIN dbo.CFE_SERVICE_DEST ON (PARTENAIRES.IU_PART = CFE_SERVICE_DEST.IU_PART_CFE)
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
AND
CFE_EDI.CODE_INSEE IS NOT NULL AND CFE_EDI.CODE_INSEE != ''
AND CASE --WHEN CFE_EDI.CODE_INSEE IS NOT NULL AND CFE_EDI.CODE_INSEE !=''
--THEN CASE
WHEN #Destinataire != '' AND (#Destinataire) IS NOT NULL
THEN CASE WHEN CFE_EDI.CODE_INSEE = #Destinataire THEN 1 ELSE 0 END
ELSE CASE WHEN CFE_EDI.CODE_INSEE = PARTENAIRES.CODE_INSEE
AND cfe_edi.dathrmaj > #DateCmt AND cfe_edi.dathrmaj < #DateFin
AND CFE_EDI.GESTDEL = '1' THEN 1 ELSE 0 END
END = 1
First question would be to know if there is way to setup the parameter without using my stupid trick.
Second question is why the query with parameter with an empty string does the trick and once you use SSRS, nothing.
Thanks in advance for your help
Update I tried to set the WHEN LEN(#Destinataire) > 0 with the #Destinataire = '' but no luck on that one.
Update 2 My aim now is to have a solution that will retrieve all the datas, in case the #Destinataire is equal to '' or NULL. However, thinking about it, this solution is equivalent to having all the values populated in #Destinataire. So one way or another, I would say.
Final update I've recreated everything from scratch and oh! magic, the grouping or the everything option worked as wish. I still don't know what was wrong but I'm fine with the results. Many thanks for your help and support.
looking at your query and where you pass #Destinataire you should be able to pass a null value according to the where clause and get the same effect as passing ''
CASE --WHEN CFE_EDI.CODE_INSEE IS NOT NULL AND CFE_EDI.CODE_INSEE !=''
--THEN CASE
WHEN #Destinataire != '' AND (#Destinataire) IS NOT NULL
THEN CASE WHEN CFE_EDI.CODE_INSEE = #Destinataire THEN 1 ELSE 0 END
ELSE CASE WHEN CFE_EDI.CODE_INSEE = PARTENAIRES.CODE_INSEE
AND cfe_edi.dathrmaj > #DateCmt AND cfe_edi.dathrmaj < #DateFin
AND CFE_EDI.GESTDEL = '1' THEN 1 ELSE 0 END
END = 1
I would try to just set that particular paramter to allow nulls in the ssrs report which can be found as a checkbox in your parameter settings window.
There is a hybrid option, where you can keep your visible parameter just as you have it, but then use a hidden cascaded parameter to reference in the query. So if you really are having trouble with formulating your query with blank or null values as a parameter, this will work around that. Here are the steps:
Create a new dataset.
A slight catch, you do need to make sure that the changing the selection always adds a new value to force a refresh of the cascaded parameter, but that's easy to do:
SELECT code_name FROM tableA WHERE #Destinataire='' OR #Destinataire=code_name
--Dummy value needed to force update of parameter value in some cases:
UNION SELECT 'zz' + #Destinataire AS code_name
Set up a new parameter, #MultiDestataire. Allow it to accept multiple values and use the new dataset as its available and default values. Set its visibility to Hidden.
Running the report before hiding the new parameter shows how it works:
Edit the query to use the new multivalue parameter in its WHERE clause.
From here on, this is just altering the query to use the IN statement, which should look like this:
CFE_EDI.CODE_INSEE IS NOT NULL AND CFE_EDI.CODE_INSEE != ''
AND CFE_EDI.CODE_INSEE IN (#MultiDestinataire)
...
Attempting this may end up being an intermediary step if you've got some other issue in the query that's responsible for unexpected results, as you should be able to test for an empty value there.
This is just my two cents, basically you have two options here.
Multivalue parameter Option
Define your parameter to allow multivalues, use the same dataset in Available values and Default values properties. So when your user doesn't select any value the parameter will be populated with all values. Otherwise your parameter will be populated only with values your users select.
In this case you will have to use IN operator since your parameter represent multiple values seleted or not by your user.
...
WHERE CFE_EDI.CODE_INSEE IN (#Destinataire)
...
When you use Default values property all drop down list values are selected by default and your report run without any filter being applied (at least if your user doesn't select any value or values).
Additional, I'd avoid use '' (blank), it is uninformative and your user might think it is some kind of error or your parameter was not populated properly.
Why use a single value parameters when you want to show data from more than one value (in your case all)?
Single value Option (No sense IMO)
In order to this works you have to set your parameter to Allow blank value ("") and Allow null value. Your query should look like this:
WHERE
CASE WHEN #Destinataire = '' OR #Destinataire is null THEN 1 ELSE 0 END = 1
OR
CFE_EDI.CODE_INSEE = #Destinataire
In your query I think it could be something like this:
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
AND cfe_edi.code_insee IS NOT NULL
AND cfe_edi.code_insee != ''
AND ( CASE
--WHEN CFE_EDI.CODE_INSEE IS NOT NULL AND CFE_EDI.CODE_INSEE !=''
--THEN CASE
WHEN ( #Destinataire = ''
OR #Destinataire IS NULL )
AND cfe_edi.code_insee = partenaires.code_insee
AND cfe_edi.dathrmaj > #DateCmt
AND cfe_edi.dathrmaj < #DateFin
AND cfe_edi.gestdel = '1' THEN 1
ELSE 0
END = 1
OR cfe_edi.code_insee = #Destinataire )
Also consider this evaluation AND cfe_edi.code_insee = partenaires.code_insee, is it necessary even if your JOIN operator is forcing to meet the condition? INNER JOIN dbo.CFE_EDI ON PARTENAIRES.CODE_INSEE = CFE_EDI.CODE_INSEE
A third option could be use a hidden parameter to clean the null and '' option of the user to produce a parameter populated with all values. Try the options above before get involved with hidden/internal parameter approach.
Let me know if this helps.

Sql Server conditional Contains for free text search handling of Null parameter

I have been struggling for quite some time to get this query going.
In short my query searches by fileno and/or searchfield
DECLARE #pSearchFor AS NVARCHAR(100);
-- I am here testing with null value, ' ' , or seperate words
SET #pSearchFor = null -- '"marsa" and "mosta"';
IF ISNULL(#pSearchFor,'') = '' SET #pSearchFor = '""' ;
declare #fileNo nvarchar(50) = 'e/e'
select top 1000 r.FileId, r.FileNo, fs.SearchField, #pSearchFor
from regfile as r
left outer join FileSearchFields as fs on r.FileId = fs.FileID
where r.FileNo like
CASE
WHEN Len(#fileno) > 1 THEN '%'+#fileNo+'%'
ELSE r.FileNo
END
AND
1 =
CASE WHEN ISNULL(#pSearchFor, '') = '' THEN 1 ELSE 0 END
or CONTAINS(fs.SearchField, #pSearchFor)
I am getting nothing returned if #pSearchFor is null otherwise works great.
I need to return all instances if a null
One possible solution might be to call 2 seperate sps or use if /else but probably exists a better method.
I really do appreciate your help!
First you set #pSearchFor to "":
IF ISNULL(#pSearchFor,'') = '' SET #pSearchFor = '""' ;
That means this will never return 1:
CASE WHEN ISNULL(#pSearchFor, '') = '' THEN 1 ELSE 0 END
You need to either use a different variable, or use the same type of CASE expression in the select list, instead of changing the value from NULL to "".
SELECT TOP 1000 r.FileId, r.FileNo, fs.SearchField,
CASE WHEN COALESCE(#pSearchFor, '') = '' THEN '""' ELSE #pSearchFor END
Also you use SELECT TOP but no ORDER BY ... if you want a subset, don't you care which subset you get?
I have solved the problem. Maybe this may be of some help to others!
This is a snippet of my stored procedure.
#fileNo nvarchar(50) = null ,
#fields nvarchar(100) = '""',`enter code here`
#datefrom date = null,
#dateto date = null,
...
AS`enter code here`
BEGIN
if (#fields = null or LEN(#fields) < 1 ) set #fields = '""'
select top 1000 r.*,
(CASE
WHEN fs.SearchField IS NULL THEN CONVERT(NVarChar(1),'')
ELSE CONVERT(NVarChar(MAX),fs.SearchField)
END) AS [Search]
from regfile as r
left outer join FileSearchFields as fs on r.FileId = fs.FileID
where r.FileNo like
CASE
WHEN Len(#fileno) > 1 THEN '%'+#fileNo+'%'
ELSE r.FileNo
END
and
r.Date between
CASE
WHEN #datefrom != '' THEN #datefrom
ELSE '1900-1-1'
END
and
CASE
WHEN #dateto != '' THEN #dateto
ELSE '9999-1-1'
END
and
((LEN(#fields) > 2 and contains(fs.SearchField,#fields))or (LEN(#fields) <= 2))
--NB: <= 2 as we have added the "" characters in #fields!
end