Select Statement based on user defined variable - sql

I am trying to do a simple select statement based on the input of the user (SSRS). Help!
select * from Table1
WHERE
Case when #x = 'Yes' then (select * from Table1 where [Column1] < 0) end;
case when #x = 'No' then (select * from Table1 where [Column1] > 0) end;
Thank you in advance
KJ

It should be as simple as
select * from Table1
WHERE
(#x = 'Yes' AND [Column1] < 0)
OR
(#x = 'No' AND [Column1] > 0);
By the way, SELECT * is really bad coding, you really should specify each column you are returning.

Related

Conditionally modify query based on parameter

I have this query (something like a case statement which I can use and fix it)
select *
from mytable
where 1=1
and (isNull(ID, 0) = 0 OR UtilityID IN (9,40))
I also want to add another statement
select *
from mytable
where 1=1
and UtilityID NOT IN (9,40)
Everything is happening in a procedure, so want to use a variable like declare #something so if that is passed as 1, use the first statement and the if 0 is passed, use the latter one.
While I appreciate the genius in Dale's answer I find this more readable:
IF #something = 0
BEGIN
select *
from mytable
where ID IS NULL OR ID = 0 OR UtilityID IN (9,40);
END
IF #something = 1
BEGIN
select *
from mytable
where UtilityID NOT IN (9,40);
END
It's procedure code, so use IF to direct the control flow. Also expanded and simplified your where clauses
I think I understand your logic, ignoring the 1=1 (which does nothing) you want to only allow id = 0 when #something = 1. This should do it:
declare #something bit = 0;
declare #mytable table (ID int, UtilityID int);
insert into #mytable (ID, UtilityID)
select 0, 1 union all
select 1, 2 union all
select 2, 9 union all
select 3, 40;
select *
from #mytable
where (
(#something = 1 and (isnull(ID, 0) = 0 or UtilityID in (9,40)))
or (#something = 0 and (UtilityID not in (9,40)))
);
A more performant approach for a larger query could be:
select *
from #mytable
where (#something = 1 and (isnull(ID, 0) = 0 or UtilityID in (9,40)))
union all
select *
from #mytable
where (#something = 0 and (UtilityID not in (9,40)));
PS: Hopefully your ID cannot ever by null - it should have a constraint on it.

conditional where clause in sql 2012

I have a query as below in my Stored Procedure:
I set the #SUBCONDITION FROM MY CODE BEHIND.
declare #SUBCONDITION VARCHAR(100)= NULL
if(#SUBCONDITION='DEPT')
BEGIN
Select * from table1 where IDDEPT=1
END
ELSE IF (#SUBCONTION='GRADE')
BEGIN
SELECT * FROM TABLE1 WHERE IDGRADE=1
END
ELSE IF(#SUBCONDITION='SECTION')
BEGIN
SELECT * FROM TABLE1 WHERE IDSECTION=1
END
Everything works just fine.
My question is whether I can do this in just one query??? Like using if or case or something in the where clause?
Yes, you can do this in one query. It would look like this:
Select *
from table1
where (#SUBCONDITION = 'DEPT' and IDDEPT = 1) or
(#SUBCONTION = 'GRADE') and IDGRADE = 1) or
(#SUBCONDITION = 'SECTION' and IDSECTION = 1)
Try this:
SELECT *
FROM table1
WHERE 1 = (CASE #SUBCONDITION
WHEN 'DEPT' THEN IDDEPT
WHEN 'GRADE' THEN IDGRADE
WHEN 'SECTION' THEN IDSECTION
ELSE 0
END);
Try this:
Select * from table1
where #SUBCONDITION = 'DEPT' AND IDDEPT=1 OR
#SUBCONDITION = 'GRADE' AND IDGRADE=1
#SUBCONDITION = 'SECTION' AND IDSECTION=1

TSQL where case

I need the correct tsql syntax for this problem :
Select * from table where var_A='10'
select * from table where var_B='10'
When to use var_A or var_B depends on the value of var_C
So when var_c='something' use var_A='10' else var_B='10'
select *
from [table]
where case when var_c = 'something' then var_a else var_b end = '10'
SQL Fiddle with demo.
Ian probably has the best answer available but I thought I would supply another answer for a different (although less flexible) method in case it helps:
IF var_c = 'something' BEGIN
SELECT * FROM [table] WHERE var_a = '10'
END
ELSE BEGIN
SELECT * FROM [table] WHERE var_b = '10'
END
This how you use case statement within where clause.
select * from table
where ( case when var_c = 'something'
then var_a
else
var_b
end
)

How do I check whether data is there or not in more than one sql table in one script?

I have more than 3 sql tables.
now i'm trying to select count(*) from all tables but how can i do this?.
I want to know whether data is present in all tables or not
I need to check the row count from previous business day ~15% for any table and it sends an email alert
I tried like following please help me to complete
PROCEDURE [dbo].[SendEmail_WSOTableDataAlert]
AS
BEGIN
declare #err int
IF NOT EXISTS (select 1 from T1) OR
NOT EXISTS (select 1 from T2)
BEGIN
set #error=1
END
//here i need to show which table is having empty data how can i do this please help
SET #tableHTML = #tableHTML + +
'</TABLE>' + #EmailFooter;
#error =1
then
send mail
END
Select
case when count(*) = 0 then
'No rows'
else
'Has rows'
end
FROM
(
Select * from #table1
UNION ALL
Select * from #table2
UNION ALL
Select * from #table3
) t
UPDATE
This makes sure all of then have at least one row and fail if any of them does not have record
Select
case when count(*) = 0 then
'No rows'
else
'Has rows'
end
FROM
(
Select top 1 1 found from #table1
intersect
Select top 1 1 found from #table2
intersect
Select top 1 1 found from #table3
) t
You can try multiplying the flags indicating zero counts together. If any of them is zero, the result will be zero.
select (case when (select count(*) from table1)=0 then 0 else 1 end
*case when (select count(*) from table2)=0 then 0 else 1 end
*case when (select count(*) from table3)=0 then 0 else 1 end) as no_zeros
If you would like to know which table has all zeros, you could transform the query as follows:
select (case when (select count(*) from table1)=0 then 1 else 0 end
+case when (select count(*) from table2)=0 then 2 else 0 end
+case when (select count(*) from table3)=0 then 4 else 0 end
+case when (select count(*) from table4)=0 then 8 else 0 end) as no_zeros
Use powers of two (1, 2, 4, 8, 16, 32, and so on) as your flags. Ones 1 in the binary representation of the result will tell you which tables have no records.
(select count() from table1 )
union all
(select count() from table2 )
union all
(select count(*) from table3 )
And then loop through the rows of the result
declare #count1 int
select #count1 = count(*)
from table1
declare #count2 int
select #count2 = count(*)
from table2
declare #count3 int
select #count3 = count(*)
from table3
if (#count1 + #count2 + #count3 = 0)
--do something
else
--do something else
You can use the EXISTS keyword to efficiently check if there is any data in a table.
IF NOT EXISTS (SELECT 1 FROM Table1) OR NOT EXISTS (SELECT 1 FROM Table2) OR NOT EXISTS (SELECT 1 FROM Table3)
BEGIN
/* do something */
END

SQL: "... WHERE result<4.0" if 'result' is: "15.2","UNK","1.4"?

How can I make this select only the row with "1.4" if result has values "15.2","UNK","1.4"?
SELECT * FROM tbl WHERE result<4.0
(ideally something that works both for MSSQL and MySQL...)
Here is the 'result' column
CREATE TABLE IF NOT EXISTS `tbl` (
`result` varchar(5) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `tbl` (`result`) VALUES ('15.2'),('UNK'),('1.4');
select *
from tbl
where 1 = case ISNUMERIC(result + 'e0')
when 1 then case when CAST(result as float) < 4.0
then 1
else 0
end
when 0 then 0
end
#ajo, if you want to select only rows with "1.4" if result has values "15.2","UNK","1.4", what about using Like??
SELECT * FROM tbl WHERE result like '1.4'
Does this work for you?
SELECT * FROM table WHERE ISNUMERIC(result) = 1 AND result < 4;
This works for SQL Server:
SELECT *
FROM tbl
WHERE CAST(NULLIF(result, 'UNK') AS DECIMAL(9, 4)) < 4.0;