Using case when - Is this possible? - sql

Right off the bat - I'm quite new to 'case when'. I read the following: How do I perform an IF...THEN in an SQL SELECT? however it didn't really answer my question.
Essentially what I'm trying to do is something along the lines of the following:
select
section_name, *
from
property.lease_period lp
where
lp.lease_current_stop_date < getdate() and (lp.lease_status = 'Active' or lp.lease_status = 'Overholding')
and lp.period_id = #period_id
and lp.building_id = #building_id
and not exists
(
select 1
from lease_deal.lease
where lp.suite_name = tenancy_reference
and lp.building_id = building_id
)
case when(#section_name <> 'ALL')
then(and upper(section_name) = upper(#section_name))
end
order by period_id desc
Is this possible? If so what am I doing wrong?
Tl;dr:
Essentially I would like:
and upper(section_name) = upper(#section_name)
To only apply to my where clause when #section_name is not equal to 'ALL'

You just can change your (non-working) CASE to
AND (#section_name = 'ALL' OR upper(section_name) = upper(#section_name))

This can be done in a simpler way without the need to use CASE. It will be something like this:
and ((upper(section_name) = upper(#section_name) and #section_name <> 'ALL') OR #section_name ='ALL')

AND upper(section_name)=CASE WHEN #section_name <> 'ALL' THEN upper(#section_name)
ELSE upper(section_name)
END

Related

Converting SQL to linq - ISNULL [duplicate]

I have a Query in SQL Server :
SELECT * FROM MyTable t
WHERE ISNULL(t.Status,'') = ''
How I can do it in Entity Framework?
EDIT:
Oh Sorry my code was like
WHERE ISNULL(t.Status,'') = ''
Try something like
MyTable.Where( t => (t.Status ?? "") == "CO" )
Although the question is ok, the logic isn't sound.
Because if a value is equal to CO, it can never be equal to either NULL or ''.
In this case you could just easily call it like this:
SQL:
SELECT * FROM MyTable t
WHERE t.Status = 'CO'
Linq:
var items = (from t in db.MyTable
where t.Status == "CO"
select t);
However if you would need it to have a default value when NULL and compare to that value it would make more sense (see example):
SQL:
SELECT * FROM MyTable t
WHERE ISNULL(t.Status, 'CO') = 'CO'
Linq:
var items = (from t in db.MyTable
where (t.Status ?? "CO") == "CO"
select t);
This would give you all items where t.Status is NULL or equal to CO.
This is, of course, just an example.
Note: The generated sql would probably be slightly different, but the result is the same.
It would probably look something like this:
SELECT * FROM MyTable t
WHERE COALESCE(t.Status, 'CO') = 'CO'

conditional where Sql server

I have a query
select idpays,idannonce,idville
from annonces
and i need to make a conditional where, where idpays=1 don't show idville null else for other idpays show idville null
How can i do this please?
regards
You may looking for this
WHERE ( idpays = 1 AND idville NOT NULL) OR idpays > 1
If your idpays column contain NULL value:
select idpays,idannonce,idville
from annonces
where (idpays = 1 AND idville IS NOT NULL) OR (ISNULL(idpays, 0) <> 1)
If you are looking for NOT NULL values for idpays = 1 and only NULL values for idpays <> 1.
select idpays,idannonce,idville
from annonces
where (idpays = 1 AND idville IS NOT NULL) OR (ISNULL(idpays, 0) <> 1 AND idville IS NULL)
You don't specify what, exactly, should be displayed when idpays=1, but here's a basis for what you've asked - you can do this with a case in the SELECT which sounds more like the problem you've described rather than a where clause:
select idpays,idannounce,
case when idpays=1 then idville
else null end
from unknowntable
EDIT based on OP comments- must admit I'm not sure I understand the OP's requirements exactly at this point:
select
from table
where (idpays<>1) or
(idpays=1 and idville not null)
Try using a CASE function. I don't think you need the ELSE statement at all:
SELECT idpays, iddonance,
CASE WHEN idpays = 1 THEN idville END AS idville
FROM annonces

case or if else in a where clause Sql Server

I using the following query and I am using case statement in where cause with parmeters
when report_type = 'All' then I need all rows and no selection is necessary, however if I select 'AR' or 'BD' I only need those respective statements to be included. However when I tried implement the following logic it is throwing me and error near by OR not sure where I am doing wrong. Any help will be greatly appreciated.
select * from aging agedet where
(
'All' = {?Report_Type}
OR
(
CASE
WHEN {?Report_type} = 'AR' THEN
(agedet.bkt_type_ha_c <> 5
AND
agedet.extern_ar_flag_yn <> 'Y'
AND
agedet.bad_debt_flag_yn <> 'Y')
CASE
WHEN {?Report_type} = 'Bd' THEN
(agedet.bkt_type_ha_c = 5
AND
agedet.extern_ar_flag_yn = 'Y'
AND
agedet.bad_debt_flag_yn = 'Y')
END )
)

SQL Where Clause To Match Against Both Conditions Simultaneously

I don't know how to phrase my question title for what I'm about to ask.
I have a SELECT query that must not return any rows if the combination of my where clause is true. Here is my example code:
SELECT
*
FROM
MyTable m1
WHERE
(m1.User != '1' AND m1.Status != '1')
But what I am trying to ask SQL is:
"only return rows when the User is not '1' AND his status is not '1' at the same time. If this combination is not true, then its okay to return those rows".
So if the User is "1" and Status is "2" then that is fine to return those rows.
Seems simple but I can't visualize how to do it... help please?
Just answered my own question.... here is the answer. 'OR' doesn't test for combination of both being true.
Solution:
SELECT
*
FROM
MyTable m1
WHERE NOT
(m1.User = '1' AND m1.Status = '1')
Because both conditions have to be true for it not to return the rows. Both = AND, Either = OR.
You may try this using OR instead of AND:
SELECT
*
FROM
MyTable m1
WHERE
!(m1.User = '1' OR m1.Status = '1')
SELECT
*
FROM
MyTable m1
WHERE
(m1.User <> '1' AND m1.Status <> '1')
There are many ways of doing this :)

Conditional Statements Inside A Where Clause

I have a sproc that runs on many different servers. In the sproc I have a select statement that looks like the following.
select *
from my_table
where id = #my_id
and status in (1,3)
However, for one of the databases I need the second part of that where clause to only be for statuses equal to 1. So essentially...
select *
from my_table
where id = #my_id
and status in (1)
I really don't want to us an if db_name() = 'mydb' statement because the actual select statements are much longer and the code starts to look really inelegant at that point. Is there some way to make the second part of the where clause conditional on the db_name?
Thanks for your help.
You can use a case statement
select *
from my_table
where id = #my_id
and status = CASE
WHEN db_name() = 'mydb'
THEN 1
WHEN db_name() = 'other'
THEN 3
ELSE -1
END
It will be inelegant, really, because it's conditional.
So, if status IN (1, 3) means status = 1 or status = 3...
where id = #my_id
and
(
status = 1
or
(db_name() <> 'mydb' and status = 3)
)
Create a new table.
select my_table.*
from my_table
inner join my_valid_statuses on my_table.status = my_valid_statuses.status
where my_table.id = #my_id
Then make sure that my_valid_statuses is populated appropriately in each database.
Try this: (I think I got your logic right...)
Cause you want it if the status is 1 for any database, but only want the status 3s for the databases that are not that single specified one...
Select ...
Where id = #my_id
And (status = 1 Or
(Db_Name() <> #DbName And status = 3))
How about something like this
select *
from dbo.MyTable
where 1=1
and
(DB_NAME() = 'FirstDatabase' and Status in (1))
OR
(DB_NAME() = 'SecondDatabase' and Status in (1, 3))
A little complex, but it'll do, if DB_NAME() works.
SELECT *
FROM my_table
WHERE id = #my_id and (status in (1) or (DB_NAME() <> 'mydb' AND status = 3))