SQL Server: WHERE clause with null comparison - sql

I have SP that has a parameter #NumeroTransferencia int.
I need to select rows that match that variable when it is not null.
But when it's null, I have to return the ones which have H.CodigoTransferencia = null
I tried the following but I didn't get a good result:
SELECT
*
FROM RUTEO.HOJADERUTA H
WHERE
(H.CodigoTransferencia IS NULL OR H.CodigoTransferencia = #NumeroTransferencia)
Thanks!

SELECT
*
FROM RUTEO.HOJADERUTA H
WHERE
((H.CodigoTransferencia IS NULL AND #NumeroTransferencia IS NULL) OR H.CodigoTransferencia = #NumeroTransferencia)

You can try this
SELECT
*
FROM RUTEO.HOJADERUTA H
WHERE
(#NumeroTransferencia IS NULL
AND H.CodigoTransferencia IS NULL)
OR (#NumeroTransferencia IS NOT NULL
AND H.CodigoTransferencia = #NumeroTransferencia)

SELECT *
FROM RUTEO.HOJADERUTA H
WHERE (
#NumeroTransferencia IS NULL
AND H.CodigoTransferencia IS NULL
)
OR
(
#NumeroTransferencia IS NOT NULL
AND H.CodigoTransferencia = #NumeroTransferencia
)

SELECT
*
FROM RUTEO.HOJADERUTA H
WHERE
((#NumeroTransferencia IS NULL OR H.CodigoTransferencia = #NumeroTransferencia) H.CodigoTransferencia IS NULL)
This will only perform the search if your variable is not null otherwise it will look for H.CodigoTransferencia is equal to your variable otherwise where H.CodigoTransferencia = null

Related

SQL Server: can you execute a stored procedure with an optional parameter? (See code)

I basically want to either return just one patient that is entered through the parameter or return all patients if no value is passed in the parameter. Is this possible with my code? I keep getting this error:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
My code:
create proc getOutsideLeadPatients
#patient int = null
as
begin
if #patient is not null
return (select *
from tblpatientdemographics p
where exists (select * from tblDBSSurgery s
where s.idpatient = p.idpatient
and s.blnoutside = 1
and p.idpatient = #patient))
else if #patient is null
return select *
from tblpatientdemographics p
where exists (select * from tblDBSSurgery s
where s.idpatient = p.idpatient
and s.blnoutside = 1)
end
go
Here is a little trick:
create proc getOutsideLeadPatients
#patient int = null
as
begin
select *
from tblpatientdemographics p
where exists (select 1 from tblDBSSurgery s
where s.idpatient = p.idpatient
and s.blnoutside = 1
and p.idpatient = COALESCE(#patient, p.idpatient))
end
go
That is - if #patient is NULL, set it equal to p.idpatient. Then the condition will pass every time.
Perhaps try something along this line
SELECT p.*
FROM tblpatientdemographics p
INNER JOIN tblDBSSurgery s ON s.idpatient = p.idpatient
WHERE (ISNULL(#patient,0) = 0 OR p.idpatient = #patient)
AND s.blnoutside = 1
And as Ben mentioned above, you do not have to use Return to send data back.
Wow all I needed to do was get rid of the returns. Thanks Ben!

checking for NULL in a select CASE

When n.ownerid is null, it will never execute this part:
...........,case n.ownerid
when NULL then
(
select systemuserid
from crm_systemuserbase
where firstname = 'CRM' and lastname='Admin'
)...........
Here's more surrounding code:
,case n.ownerid
when NULL then
(
select systemuserid
from crm_systemuserbase
where firstname = 'CRM' and lastname='Admin'
)
when '6e99ff04-f498-e311-93f3-005056a37b31' then
(
select systemuserid
from crm_systemuserbase
where firstname = 'CRM' and lastname='Admin'
)
end as OwnerID
In a SELECT CASE how does one check whether the value of the field question is null?
Use the is operator when comparing to null
case when n.ownerid is null then ...
when n.ownerid = '6e99ff04-f498-e311-93f3-005056a37b31' then ...
end as OwnerID
you could also use the COALESCE expression:
CASE when COALESCE(n.ownerid, '(nullVal)') = '(nullVal)' . . .

Using ISNULL in where Clause doesn't return records with NULL field

Table:
ID AppType AppSubType Factor
1 SC CD 1.0000000000
2 SC CD 2.0000000000
3 SC NULL 3.0000000000
4 SC NULL 4.0000000000
Query:
declare #ast varchar(10)
set #ast = null
select *
from tbl
where AppType = 'SC' and AppSubType = ISNULL(#ast, AppSubType)
Result:
ID AppType AppSubType Factor
1 SC CD 1.0000000000
2 SC CD 2.0000000000
Question:
Shouldn't this query return all 4 records and not just the first 2?
Abviously #ast is null and Isnull would exchange null with other value, so you shouldn't expect #ast to be not null. If your AppSubType is null , so the result become null but AppSubType=null doesn't mean because AppSubType is null is true. Because null is not a value so it cant work with equal.
for your expected result this code will work.
declare #ast varchar(10)
set #ast = null
select *
from tbl
where AppType = 'SC' and (AppSubType = ISNULL(#ast, AppSubType) Or AppSubType is null)
You can write a case condition in where clause as:
declare #ast varchar(10)
set #ast = null
select *
from tbl
where AppType = 'SC' and 1=
case when isnull(#ast ,'') = '' and isnull(AppSubType ,'') = '' then 1
when AppSubType = ISNULL(#ast, AppSubType) then 1
else 0
end
Please understand the behavior of ISNULL explained in below blog.
the very first expression in the isnull function is a column value or the expression of some result.
ISNULL Explored in Detail
The code looks like a search functionality. If the value is not given then replace them with whatever is there in database and if given pull only those records which are matched.

Sql Server Where Case Then Is Null Else Is Not Null

I have a procedure which receive a bit variable called #FL_FINALIZADA.
If it is null or false I want to restrict my select to show only the rows that contain null DT_FINALIZACAO values. Otherwise I want to show the rows containing not null DT_FINALIZACAO values.
Something like this:
SELECT
*
FROM
MyTable
WHERE
...
AND
(
OPE.DT_FINALIZACAO = (
CASE
WHEN (#FL_FINALIZADA <> 1)
THEN NULL
END
) OR
OPE.DT_FINALIZACAO IS NOT NULL
)
In this case I receive the message:
None of the result expressions in a
CASE specification can be NULL.
How can I achieve this?
Thanks in advance.
SELECT
*
FROM
MyTable
WHERE
(ISNULL(#FL_FINALIZADA, 0) = 0
AND
OPE.DT_FINALIZACAO IS NULL
)
OR
(#FL_FINALIZADA = 1
AND
OPE.DT_FINALIZACAO IS NOT NULL
)
Change the AND to be:
AND (((#FL_FINALIZADA <> 1) AND (OPE.DT_FINALIZACAO IS NULL)) OR ( (#FL_FINALIZADA = 1) AND (OPE.DT_FINALIZACAO IS NOT NULL)))
If the bit flag is 1 then DT_FINALIZACAO can't be null.
IF #FL_FINALIZADA IS NULL
SET #FL_FINALIZADA = 0
SELECT * FROM NewsletterSubscribers
WHERE
(#FL_FINALIZADA = 0 AND OPE.DT_FINALIZACAO IS NULL)
OR
(#FL_FINALIZADA = 1 AND OPE.DT_FINALIZACAO IS NOT NULL)
My detailed SQL is a little rusty, but have you tried using 0 insted of NULL? I would expect 0 to evaluate the same as NULL in that select

Syntax for SQL Server when either of parameter is passed

If one of the paramater is passed / or both passed how to match that to single column in WHERE clause.
If both are passed should get result set just with the first one else with second one.
Ex:
--either will be passed
declare #processStepID int = null
declare #PROCESS_StepID int = null
if(#processStepID is null)
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = #PROCESS_StepID
else
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = #processStepID
How can this be written in where clause not using if statement.
thanks
WHERE PROCESS_STEP_ID = coalesce( #processStepID, #PROCESS_StepID )
Use the ISNULL function
SELECT *
FROM CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
WHERE PROCESS_STEP_ID = ISNULL( #processStepID, #PROCESS_StepID )
select * from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where (PROCESS_STEP_ID = #PROCESS_StepID) OR (PROCESS_STEP_ID = #processStepID)
Seems far too obvious. What am I missing?
select *
from CTL.CTRL.CTRL_PROCESS_STEP_ACTIVITIES
where PROCESS_STEP_ID = ISNULL(#PROCESS_StepID,#processStepID)
This should do what you are looking for