Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 If condition in Where clause

Author  Topic 

R Senthil Kumar
Starting Member

3 Posts

Posted - 2014-03-28 : 07:53:37
Dear All,

I given my requirment below, Pls help me out.

select * from table where (here if account_no = '123456' then name <> 'KING')

Regards,
R Senthil

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2014-03-28 : 08:09:34
select * from table where name in( case when accoun_no = '123456' then (select top 1 name from table where name <> 'King' end)

Veera
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-03-28 : 12:55:58
Unfortunately, your query doesn't make sense. If Veera's solution doesn't work for you can you describe what you are trying to do? I suspect that a sub-query like that is very inefficient anyway and it should be a join. But, that aside, maybe post some sample data and expected output to help illustrate the problem.

Here are some links to help with that:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

R Senthil Kumar
Starting Member

3 Posts

Posted - 2014-03-31 : 05:02:58
Hi,

select * from table where if account_no = ‘123456’ then name <> ‘King’

Exp:
If the account_no is 123456 then only the name condition should work as name not equal to 'King', otherwise the query can run normally.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2014-03-31 : 05:17:00
What means "can run normally"?
Can you show us the "normally" running query please?



Too old to Rock'n'Roll too young to die.
Go to Top of Page

nagino
Yak Posting Veteran

75 Posts

Posted - 2014-03-31 : 05:18:05
select * from table where not (account_no = '123456' and name = 'King')


-------------------------------------
From Japan
Sorry, my English ability is limited.
Go to Top of Page

R Senthil Kumar
Starting Member

3 Posts

Posted - 2014-03-31 : 07:11:27
quote:
Originally posted by webfred

What means "can run normally"?
Can you show us the "normally" running query please?



Too old to Rock'n'Roll too young to die.



If account_no = '123456' then only the name not equal to 'King'.
Other wise the query can retreive a row where name is 'King'
Go to Top of Page

Robowski
Posting Yak Master

101 Posts

Posted - 2014-03-31 : 08:08:41
quote:
Originally posted by R Senthil Kumar

quote:
Originally posted by webfred

What means "can run normally"?
Can you show us the "normally" running query please?



Too old to Rock'n'Roll too young to die.



If account_no = '123456' then only the name not equal to 'King'.
Other wise the query can retreive a row where name is 'King'



SELECT *
FROM Table
WHERE (account_no = '123456' and name <> 'king')
or account_no <> '123456'
Go to Top of Page
   

- Advertisement -