SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 If condition in Where clause
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

R Senthil Kumar
Starting Member

3 Posts

Posted - 03/28/2014 :  07:53:37  Show Profile  Reply with Quote
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

India
164 Posts

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

Veera

Edited by - VeeranjaneyuluAnnapureddy on 03/28/2014 08:14:57
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4608 Posts

Posted - 03/28/2014 :  12:55:58  Show Profile  Reply with Quote
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

Edited by - Lamprey on 03/28/2014 12:56:35
Go to Top of Page

R Senthil Kumar
Starting Member

3 Posts

Posted - 03/31/2014 :  05:02:58  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8760 Posts

Posted - 03/31/2014 :  05:17:00  Show Profile  Visit webfred's Homepage  Reply with Quote
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

Japan
65 Posts

Posted - 03/31/2014 :  05:18:05  Show Profile  Reply with Quote
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 - 03/31/2014 :  07:11:27  Show Profile  Reply with Quote
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 - 03/31/2014 :  08:08:41  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000