conditional where Sql server - sql

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

Related

How to use an SQL Comparator in the base 'Case' selector in the 'When' logic without having to re-write conditions

I have an SQL query joined on multiple tables (all INNER JOINS).
The below is an example of the query I am trying to run (the ? is to illustrate the position in which I presume the answer to my question will be rectified).
Case
(
SELECT Count(ID)
FROM CPD_Candidates cpdCan
WHERE
cpdCan.CandidateID = can.CandidateID
AND
(
cpdCan.DateEnded >= GETDATE()
OR
coalesce(cpdCan.DateEnded, '') = N'1-Jan-1900'
)
AND
cpdCan.Deleted <> 1
)
When ? > 0 then 'Bigger' else 'Equal or Smaller' End
)
The idea with the above is that instead of the ? the actual value I want to compare against would be Count(ID), if it's greater than 0 I want it to SELECT 'Bigger', otherwise it should SELECT 'Equal or Smaller'. So a more-accurate depiction of what I wish to run would be the below.
Case
(
SELECT Count(ID)
FROM CPD_Candidates cpdCan
WHERE
cpdCan.CandidateID = can.CandidateID
AND
(
cpdCan.DateEnded >= GETDATE()
OR
coalesce(cpdCan.DateEnded, '') = N'1-Jan-1900'
)
AND
cpdCan.Deleted <> 1
)
When
Count(cpdCan.ID) > 0 then 'Bigger' else 'Equal or Smaller' End
)
Of course there is a syntax error above but I am enquiring as to whether it is possible to compare like in the above SQL query structure but replacing Count(cpdCan.ID) > 0 with some other means to achieve that value & logic?
If this is un-achievable in SQL Server 2016 what other means would be a better solution to this XY?
I think that you mean:
case when
(
SELECT Count(ID)
FROM CPD_Candidates cpdCan
WHERE
cpdCan.CandidateID = can.CandidateID
AND (cpdCan.DateEnded >= GETDATE() OR coalesce(cpdCan.DateEnded, '') = N'1-Jan-1900')
AND cpdCan.Deleted <> 1
) > 0
then 'Bigger'
else 'Equal or Smaller'
End

'CASE' expression whether to apply a WHERE condition to a query or not

I've been trying to accomplish something like the code below inside a stored procedure
Select * from TABLE1
CASE WHEN #SPParameter != 0 THEN -- if #SPParameter equals 0 then apply the where condition
WHERE Table1Column = #SPParameter -- apply a where condition
END
This query's goal is to select all rows from TABLE1 if #SPParameter is equal to zero, otherwise filter rows from TABLE1 if #SPParameter is not equal to zero.
Obviously the query above would throw an error message since the syntax is incorrect. Is this possible? Or is an if else statement the only way out?
Just use simple boolean logic1:
Select * from TABLE1
WHERE #SPParameter != 0 OR Table1Column = somevalue
CASE is an expression. It computes a value. It doesn't arbitrarily rearrange the parse tree of the statement it appears in.
1It'll be slightly more complex if we have to deal with NULLs but I've ignored them for now.
You can achieve by using OR condition like this way
WHERE (#SPParameter = 0 OR Table1Column = somevalue)
SELECT Name, JobType
FROM EMP
WHERE 1 = CASE
WHEN JobType = 'VC' THEN 1
WHEN JobType = 'HR' THEN 1
WHEN JobType = 'DEV' THEN 1
ELSE 0
END;
Above example CASE returns if jobtype present in table then it will be return the Name and JobType.
You try using case in where something like below:
Select * from TABLE1
where Table1Column = case when #SPParameter = 0 then #SPParameter else #SPParameter end;
Select * from TABLE1
WHERE Table1Column = CASE
WHEN #SPParameter != 0 THEN #SPParameter
END;
Here WHERE condition gets the value from the CASE statement, if #SPParameter is not equals zero, which means the value is present THEN it will be return the #SPParameter value.

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

SQL Query - Return Text based on numeric value

I am trying to work out a query where the query will perform a count (total) on a specific column. If the count is greater than 0, I want to display YES and display NO if the returned count is zero.
So, if I a query as this:
SELECT COUNT(ProblemID)
FROM dbo.ProblemInfo
WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')
that will actually be a subquery, how do I get the subquery to display "YES" when the returned count is greater than 0 and NO if the count is 0?
I appreciate any insight and help.
select isnull(
SELECT MAX('YES')
FROM dbo.ProblemInfo
WHERE ProblemID IN (100,101,309,305,205,600,500)
AND DEPID = '10866'),
'NO')
This is a trick to return either YES if there's at least one matching row, or null if not.
The wrapping isnull call then turns a null into a NO
Here's an alternate way of querying that.
IF EXISTS(
SELECT *
FROM dbo.ProblemInfo
WHERE (ProblemID IN (100,101,309,305,205,600,500))
AND (DEPID = '10866')
)
BEGIN
SELECT 'Yes'
END
ELSE
BEGIN
SELECT 'No'
END
What I like about this method is that, for enormous data-sets, it should be noticeably faster.
try
SELECT CASE
WHEN (SELECT COUNT(ProblemID) FROM dbo.ProblemInfo WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')) > 0
THEN 'YES'
ELSE 'NO' END
FROM YourTable
you can use case when.
SELECT
case
when COUNT(ProblemID) = 0 then 'NO'
else 'YES'
end
FROM dbo.ProblemInfo WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')

SQL Query - Can I compare using LEN in SELECT clause?

I basically want to do this:
SELECT HasComments = CASE (LEN(Comments) > 1) WHEN 1 THEN 1 ELSE 0 END FROM TableName
In other words, return a boolean telling me whether the length of Comments is greater than 1. This gives me a syntax error.
How can I accomplish this?
SELECT HasComments = CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END
FROM TableName
A better way would be to make Comments NULLable and check for that. Indexes could then be leveraged instead of the table-scan LEN() will cause.
you're missing the when and end
SELECT HasComments = CASE WHEN (LEN(Comments) > 1) WHEN 1 THEN 1 ELSE 0 END
FROM TableName
Since you have no WHERE clause, you're most likely returning a column of data:
SELECT CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END as 'HasComments'
FROM TableName
For newer SQL versions:
SELECT CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END FROM TableName