How to use case function in SQL where clause? - sql

I am converting a Crystal Report to SSRS. In the CR Select Records section there are several filters including an IF statement like this:
(if BeginCompany <> 0 and EndCompany <> 999 then
Company between BeginCompany and EndCompany else 1 = 1)
How can I make an equivalent CASE statement in an SQL WHERE clause in my dataset? Thanks.

Are BeginCompany and EndCompany actually variables? What are they when you don't want to use them as a filter? NULL? Something else?
Maybe you're looking for this (assuming companies are represented by positive integers):
WHERE Company >= COALESCE(#BeginCompany, 0)
AND Company <= COALESCE(#EndCompany, 2147483647)
Which is just another way to express:
WHERE Company >= CASE
WHEN #BeginCompany IS NULL THEN 0 ELSE #BeginCompany END
AND Company <= CASE
WHEN #EndCompany IS NULL THEN 2147483647 ELSE #EndCompany END
You can't use a CASE expression for control-of-flow, e.g.
CASE ... something ... THEN Column BETWEEN x AND y
CASE is an expression that returns a single value. Much more background here:
Dirty Secrets of the CASE Expression

It sounds like the logic wants the Company field to be between the BeginCompany and EndCompany parameters if BeginCompany <> 0 and EndCompany <> 999 to show a record but if they do equal 0 or 999, then show all records. If a users enters 0 for the Begin parameter or 999 for the end, then the query would show all records.
I think in SQL it would be
WHERE (
#BeginCompany <> 0
and #EndCompany <> 999
AND Company between #BeginCompany and #EndCompany
)
OR (
#BeginCompany = 0
OR
#EndCompany = 999
)
If you really wanted to squeeze a CASE statement if there then
WHERE CASE WHEN
#BeginCompany <> 0
and #EndCompany <> 999
AND Company between #BeginCompany and #EndCompany
THEN 1
WHEN
#BeginCompany = 0
OR
#EndCompany = 999
THEN 1
ELSE 0 END = 1

Related

Count(*) return 1 or zero

In one of the usecase I need a query which should return 1 based on condition also if not match it should return 0
In Descpriont column if the 'SAP' count is exactly 1 then the query should return 1 else it should return 0
Note : There might be a chance that SAP could be present any number of times in Description column.
Could someone help me out here !!
Thanks.
I tried below query :
SELECT 1 from TableName where Description ='SAP' having count(*)>1
It is returning 1 but not return 0 if the count is more than 1 or no match found.
Use CASE WHEN to decide whether to show 0 or 1.
select case when count(*) = 1 then 1 else 0 end as sap_count_is_1
from mytable
where description = 'SAP';
use case when
select
case when sum(case when description='SAP' then 1 else 0 end)=1 then 1 else 0 end from table_name

way to have query with case statement

I have table called Numbers in that column I have values from 0 - 10 but I like to keep value of 1-10 only change record of 0 too null
Case numbers
when 0
then ''
but I found this has changed all values and not values that have 0 is there way I can say else leave value as is?
Do you want this?
update t
set number = null
where number = 0;
Or as a select:
select t.*,
(case when number <> 0 then number end)
from t;
SELECT CASE WHEN [column] = 0 THEN NULL ELSE [column] END AS [SomeName]
FROM Numbers

How to add multiple case in sql

I want to add multiple cases in single line. In other words in below example if first item is NOT NULL and second item is equal to 1 then output should be 1 else 0. How can I do it?
MatchbyCatalog= (case when ISNULL(pc.ProductID,0) and tc.RawMatch=1) then 1 else 0 end)
Is this what you want?
CASE WHEN pc.ProductID IS NULL and tc.RawMatch=1 THEN 1 ELSE 0 END
You misunderstood ISNULL() , it's a function that replaces the value if it's NULL, it's not a condition . You were looking for IS NULL
That's all you need.
MatchbyCatalog= (case when ISNULL(pc.ProductID,0)=1 and tc.RawMatch=1 then 1 else 0 end)
when you say both I think you mean pc.ProductID and tc.RawMatch...
I have not you're entire sql query so I have to guess.
You have multiple solutions :
Nearly ss you have wrote when pc.ProductId = 1 and tc.RawMatch=1 then 1 else 0 :
MatchbyCatalog= (case when ISNULL(pc.ProductID,0)=1 and tc.RawMatch=1) then 1 else 0 end)
You could also use a trick : 1 * 1 should be 1 and 1 * 0 =0 so you could write (if tc.Rawmatch is a boolean sort of with values 0 or 1) :
MatchbyCatalog= ISNULL(pc.ProductID,0) * tc.RawMatch
case when {Condition} then {result}
When {Condition} then {result}
Else {result}

SQL query for displaying record with maximum number of non-empty fields

I am looking for a query/set of SQL queries that will give me the record ID of the record which has the "maximum number of non-empty/non-null fields".
I was looking into count() and max() functions, but they seem to be solving problems for the same column, but not for the same row (which is what I am looking for).
Please help.
You could order by the amount of non-empty fields:
select top 1 Record_ID
from YourTable
order by
case when isnull(col1,'') <> '' then 1 else 0 end +
case when isnull(col2,'') <> '' then 1 else 0 end +
case when isnull(col3,'') <> '' then 1 else 0 end +
...
case when isnull(colN,'') <> '' then 1 else 0 end
This is SQL Server syntax. If you're using another database, please amend your question.

query sql returning custom value

I have a table with 3 columns (containing Integer values that assume only values from 0 to 10). I want extract, with a single query, a table with 1 column. This column must assume a value based on the following logic:
If one of these three columns has value 0 ----> the value of column of table generated by query must be 0 too.
If none of the last three columns has value 0 ----> the value of column must assume the value 1.
You are looking for CASE construct or IF function:
SELECT CASE WHEN (t.field1 = 0 OR t.field2 = 0 OR t.field3 = 0) THEN 0
ELSE 1 END AS value
FROM t;
In this specific case you might also use the fact that any member being zero will zero the product:
SELECT CASE WHEN (t.field1*t.field2*t.field3 = 0) THEN 0 ELSE 1 END AS value
FROM t;
Or
SELECT IF((t.field1*t.field2*t.field3)=0, 0, 1) AS value FROM t;
This is a simple case statement. Assuming there are no NULL values, try this:
select (case when col1 = 0 or col2 = 0 or col3 = 0 then 0 else 1 end)
Try this
SELECT
CASE
WHEN column1 = 0 THEN 0
WHEN column2 = 0 THEN 0
WHEN column3 = 0 THEN 0
ELSE 1
END
FROM urtable