Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.73 KB | None | 0 0
  1. /*
  2. Hi LeonNewbie,
  3.  
  4. I wrote some code, explanations and examples for you. I also had to add some assumptions - maybe you have to alter the code if I got you wrong :-)
  5. */
  6.  
  7.     /* KeyWord 1 contains 2 phrases */
  8.     --One has the keywords pingu and one has noot as seen below: This is stored in KEYWORD1
  9.     DECLARE @KeyWord1_1 varchar(512)
  10.     DECLARE @KeyWord1_2 varchar(512)
  11.     SET @KeyWord1_1 = 'pingu'
  12.     SET @KeyWord1_2 = 'noot'
  13.    
  14.     /* KeyWord 2 contains 2 phrases */
  15.     --My other string has two keywords such as: Sponge and Bob...
  16.     DECLARE @KeyWord2_1 varchar(512)
  17.     DECLARE @KeyWord2_2 varchar(512)
  18.     SET @KeyWord2_1 = 'sponge'
  19.     SET @KeyWord2_2 = 'bob'
  20.    
  21.     /* Your input-variable */
  22.     --Now when my staff use the program they have an option to enter some free text, they can enter anything e.g...
  23.     DECLARE @teststring varchar(512)
  24.     SET @teststring = 'sponge went noot or pingu goes bob.'
  25.     -- EXAMPLES
  26.     --SET @teststring = 'sponge bob'
  27.     --SET @teststring = 'pingu noot'
  28.     --SET @teststring = 'pingu bob'
  29.     --SET @teststring = 'pingu bob'
  30.     --SET @teststring = 'noot bob'
  31.    
  32.    
  33.     DECLARE @done int
  34.     SET @done = 0 -- we will need this for the further IFs :-)
  35.    
  36.     -- your requirement:
  37.     -- If pingu noot exists then continue (= when the two phrases of keyword are given)
  38.     --, otherwise if sponge bob exists continue  (= when the two phrases of keyword are given)
  39.     --or is pingu and bob exists continue  
  40.     --or if sponge and noot exists continue (also when pingu/sponge and noot/bob are given? (IF YES* IF NO**)
  41.     -- **NO would mean: A full keyword, it can be mixed but it must fit a first and a second phrase
  42.     -- *YES woudl mean: A whole keyword must be found, or a pharse from the first and the second keyword
  43.     -- In my example I will stay with _NO_
  44.     IF   (@teststring LIKE '%'+@KeyWord1_1+'%'  -- First Keyword - Full
  45.       AND @teststring LIKE '%'+@KeyWord1_2+'%')
  46.       AND @done = 0
  47.     BEGIN
  48.         PRINT 'If pingu noot exists then continue'
  49.         SET @done = 1;
  50.     END
  51.     IF (@teststring LIKE '%'+@KeyWord2_1+'%'  -- Second Keyword - Full
  52.       AND @teststring LIKE '%'+@KeyWord2_2+'%')
  53.       AND @done = 0
  54.     BEGIN
  55.         PRINT 'otherwise if sponge bob exists continue'
  56.         SET @done = 1;
  57.     END
  58.    
  59.     IF   (@teststring LIKE '%'+@KeyWord1_1+'%'  -- First Keyword, first Phrase and
  60.       AND @teststring LIKE '%'+@KeyWord2_2+'%') -- Second Keyword, second Phrase
  61.       AND @done = 0
  62.     BEGIN
  63.         PRINT 'pingu and bob exists continue'
  64.         SET @done = 1;
  65.     END
  66.    
  67.     IF (@teststring LIKE '%'+@KeyWord2_1+'%'  -- Second Keyword, first Phrase and
  68.       AND @teststring LIKE '%'+@KeyWord1_2+'%') -- First Keyword, second Phrase
  69.       AND @done = 0
  70.     BEGIN
  71.         PRINT 'sponge and noot exists continue'
  72.         SET @done = 1;
  73.     END
  74.    
  75.     IF @done = 0
  76.     BEGIN
  77.         PRINT 'and i still haven''t found, what i''m looking for...'
  78.     END
  79.    
  80.     -- If you also want to check the order (to prevent "noot pingu" from working) you can try this:
  81.     SET @teststring = 'noot pingu'   -- not working
  82.     SET @teststring = 'pingu noot' -- it's okay
  83.     SET @done = 0
  84.    
  85.     IF   (@teststring LIKE '%'+@KeyWord1_1+'%'  -- First Keyword - Full
  86.       AND @teststring LIKE '%'+@KeyWord1_2+'%')
  87.       AND @done = 0
  88.       AND CHARINDEX(@KeyWord1_1,@teststring,1) < CHARINDEX(@KeyWord1_2,@teststring,1) -- that's the ticket...
  89.     BEGIN
  90.         PRINT 'If pingu noot exists - IN THE CORRECT ORDER then continue'
  91.         SET @done = 1;
  92.     END
  93.  
  94. /*Also consider this is just how you could make it work - it is not meant to be a complete solution.
  95. It is no good way to ensure security and also check if your database is case sensitive!
  96.  
  97. Regards
  98. tgr*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement