Converting SQL to linq - ISNULL [duplicate] - sql

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'

Related

How to output 'is null' in a where clause in a case when statement?

The query checks the value of a parameter foo which is passed by a dropdown inside a program. If that paramater contains a certain value, an attribute should only contain null values. Can I manage that without pl/SQL?
select * from table t
where case when $foo$ = 'yes' then t.something is null end
Do you mean this logic?
select something
from table t
where ($foo$ = 'yes' and t.something is null) or ($foo != 'yes')
Just use nvl function :
select *
from mytable t
where nvl($foo$,'yes') = 'yes';

Assign null if subquery retrieves multiple records. How can it be done?

I have the following query. I simplified it for demo purpose. I am using SQL Server - t-sql
Select tm.LocID = (select LocID from tblLoc tl
where tl.LocID = tm.LodID )
from tblMain tm
if the subquery returns multiple records, I like to assign tm.LocID to null else if there is only 1 record returned then assign it to tm.LocID. I am looking for a simple way to do this. Any help would be appreciated.
One way I can see is to have a CASE statement and check if (Count * > 1 ) then assign null else return the value but that would require a select statement within a select statement.
You have the right idea about using a case expression for count(*), but it will not require another subquery:
SELECT tm.LocID = (SELECT CASE COUNT(*) WHEN 1 THEN MAX(LocID) END
FROM tblLoc tl
WHERE tl.LocID = tm.LodID )
FROM tblMain tm
or just use a HAVING clause, like
Select tm.LocID = (select LocID from tblLoc tl
where tl.LocID = tm.LodID
group by locID
having count(*) = 1)
)
from tblMain tm
Your query above (and many of the other answers here) is a correlated subquery which will be very slow since it performs a separate aggregation query on each record. This following will address both your problem and potentially perform a bit better since the count happens in a single pass.
SELECT
CASE
WHEN x.locid IS NOT NULL THEN x.locid
ELSE NULL
END
FROM tblMain m
LEFT JOIN (
SELECT
locid
FROM tblLoc
GROUP BY locid
HAVING COUNT(1) = 1
) x
ON x.locid = m.locid
;
The above is in Postgres syntax (what I'm familiar with) so you would have to make it TSQL compatible.

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 :)

losing null values filtering sql query results using where

I have a complex query which joins more than 7 tables.
After the joins, I would like to filter the result of my query .
Here is something that I observed.
When I do a where clause
where X.Name != 'xxx'
and XY.Product != 1
I get the filtered results , but all the null values for the X.Name and XY.Product also disappear
from my result. I would like to retain the null values.
I also tried :
and X.Name != 'xxx'
and XY.Product != 1
I removed the where clause totally and put in an and , but I dont see the filtering at all by this approach.
Is there a way I can filter my results without losing the null values ??
Try something like:
where (X.Name <> 'xxx' or X.Name is null)
and (XY.Product <> 1 or XY.Product is null)
Since, by definition NULL is an unknown value (bit simplified but OK for this explanation), it will neither equal or not equal a given value - that's why the IS NULL is required here.
This quote is taken from 70-461 training kit.
"T-SQL—again, based on standard SQL—implements only one general purpose mark called NULL for any kind of missing value. This leads to three-valued predicate logic."
Therefore, here are the three logic conditions you can have.
1 - Value matches condition
2 - Value does not match condition
3 - Value is missing.
Here is some sample code to play around with.
-- Create sample table
create table #products
(
my_id int identity (1, 1),
my_name varchar(16)
);
-- Load with sample data
insert into #products (my_name) values
(NULL),
('iPad'),
('Windows Surface');
-- Show the data
select * from #products
-- Show just ipads
select * from #products where my_name = 'iPad'
-- Show just surfaces
select * from #products where my_name <> 'iPad'
-- Show ipads & unknowns
select * from #products where my_name = 'iPad' or my_name is null
Here is the output from the last three select statements.
Why not use this:
where isnull(X.Name,'') != 'xxx' and isnull(XY.Product,0) != 1
SELECT X.NAME,XY.PRODUCT FROM
(SELECT ID,NAME FROM TABLE_X WHERE NAME!=1)X
OUTER JOIN
(SELECT ID,PRODUCT FROM TABLE_XY WHERE PRODUCT!=1)XY
ON X.ID=XY.ID
where (X.Name != 'xxx' or X.name Is Null)
and (XY.Product != 1 or XY.Product Is Null)

Using case when - Is this possible?

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