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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 separate clauses

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2009-12-30 : 10:16:16
Hello,
Looking at the sql below, how can I separate the and clause and the OR clause
( ID = '1' OR ID = '2' OR ID = '3' OR ID = '4' OR ID = '5' OR ID = '14' AND f.Account = '4' )

Result:

( ID = '1' OR ID = '2' OR ID = '3' OR ID = '4' OR ID = '5' OR ID = '14') AND (f.Account = '4' )

Thanks

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-12-30 : 10:31:42
just this would do
where ID in ('1','2','3','4','5','14') and (f.Account = '4' )
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2009-12-30 : 10:34:38
I think you mis-understood.
I am building the sql dynamically and so I would like to separate the AND and OR clauses.
Any thoughts please?
Thanks
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-12-30 : 10:37:32
quote:
I am building the sql dynamically and so I would like to separate the AND and OR clauses.

Can you explain this a li'l better..you want to add the "AND" clause dynamically based on some other condition?
Go to Top of Page

DP978
Constraint Violating Yak Guru

269 Posts

Posted - 2009-12-30 : 10:39:27
I think you may have to supply a bit of code showing how it is currently being built, if you are adjoining new 'OR's and 'AND's from a condition you would just need to add another condition to check if the left 2 characters are OR and if not close the paranthesis and begin the AND.
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2009-12-30 : 10:40:51
I am building the query based on several fields, operators, etc...
the sql I placed above is what gets built. Now I want to make sure there is a bracket around the fields with OR and there is a bracket around the fields with AND. This way I will be able to separate the OR fields with the AND fields as indicated in the query I posted under the result section. Thanks
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-12-30 : 10:44:52
I don't know if anyone can suggest a solution without knowing how you are building the query.
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2009-12-30 : 10:47:06
I think I can do this by adding extra columns for ( and )
Thanks anyway.
Go to Top of Page

DP978
Constraint Violating Yak Guru

269 Posts

Posted - 2009-12-30 : 10:50:08
again..you probably have a navarchar field being built like...

@mySql = @mySql + @myCondition

Before doing that step do something like...

If Left(ltrim(@mycondition), 3) = 'AND'
@mysql = mySql + ')' +... --Need to replace AND with AND (

Else
@mysql = @mySql + @myCondition
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2009-12-30 : 11:01:06
I have solved the problem by using two extra fields. one for ( and another for ) where appropriate.
Thanks anyway.
Go to Top of Page
   

- Advertisement -