romo
May 25, 2015, 11:21am
1
Dear All
I have a query where having condition A='POP' AND B=1.
The value '1' appears to B column when 'A' value is equal to 'POP' only , otherwise 'B' shouldn't have any values.
'B' is a integer data type column.
Please help me on this.
Thanks
select case when A='POP' then 1 end
romo
May 26, 2015, 8:49am
3
Then what value get pass if A<>'POP'.
If A<>'POP , I need to pass all values to B
Try this
select case when A='POP' then 1 else B end as B from table
Or Can you post some sample data with expected result?
romo
May 26, 2015, 10:19am
5
Hi Madivanan
Actualy the B take multiple values from a table (which consist data 1...10).
Ok. Have you tried the code I provided?
SELECT col1
FROM table1
WHERE (A = @A AND @A <> 'POP' OR B=1)
romo
May 27, 2015, 6:42am
8
@Madhivanan - yes, It process when A='POP' only and not taking other values from table when A<>'POP'
This is how my query looks alike
select ...... from table1
where tid= (case when @para='pop' then 1 else end)
table1 table2
tid value tid def
1 10 1 AAA
2 15 2 BBB
3 20 3 CCC
1 16
2 25
tid is connector for both table.
when @para is 'pop' then i have to pass only 1 as tid and if its not 'pop then I need to pass 1,2,3 for tid field.
Thanks
romo
May 27, 2015, 6:44am
9
table1
tid value
1 10
2 15
3 20
1 16
2 25
table2
tid def
1 AAA
2 BBB
3 CCC
Try This
select ...... from table1where tid= (case when @para='pop' then 1 else tid end)
romo
June 4, 2015, 6:11am
11
Thanks for your reply Viggnesh..
As per yours it won't take other values if @para='pop' .
NULL value only get pass to else condition when @para<>'pop
No Romo....
If @para is pop then you should return records having tid is 1.
if @para is not pop then you should return all the records from the table.
is this is the case?
then (case when @para='pop' then 1 else tid end) this condition should work. incase of null please use isnull