Complex Sql query

Posted on 2007-07-26
Last Modified: 2010-03-19
I have a interesting scenario here, not able to figure out how to fix this issue.

 if exists(select MasterID from ReqFormula where charindex(Ltrim(rTrim(str(@MasterID))),Formula) > 0)

  Print 'Error Master course used in ReqFormula'
      Return 2
The above statement doesn't work because if @MasterID = 29 and if there is {#329} in 'Formula' field then Charindex returns > 0 which is incorrect because i'm looking for 29 not 329.

This is what my table(ReqFormula) looks like
MasterID   Formula                                                                    FormulaText
1               {(}{#70}{AND}{#74}{)}{OR}{#14}                             (DAA115 AND DAA230) OR CV131
2               {(}{#63}{AND}{#69}{)}{OR}{(}{#7}{AND}{#13}{)}    (DAA100 AND DAA110) OR (CV101)
5               {#7}{AND}{#253}                                                     CV101 AND HU120
6               {#329}{AND}{#253}                                                 MBT101 AND HU120

In the above table under formula column each number is a MasterID. Look at MasterID 6(Last column), this records gets picked up because under Formula column there is 329 even thought our variable @MasterID is actually looking for 29.

I think my above query is correct but i need to filter it little more so that @masterID matches with number in the formula column. I would like see if i can separate each numeric value using the course separator "AND" or  "OR" and then do a string comparison individually so that my @masterID matches with that exact number in the formula column/field.

Let me know if there is a way to do this.

Question by:onebite2
    1 Comment
    LVL 142

    Accepted Solution

    so you are actually looking for {#29} ?

    then, make it as easy as this:
     if exists(select MasterID from ReqFormula where Formula like '%{#' + cast(@MasterID as varchar(200)) +'}%' )

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    This article describes some very basic things about SQL Server filegroups.
    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now