If Condition in SQL to search

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imtmub
    New Member
    • Nov 2006
    • 112

    If Condition in SQL to search

    I have textbox in application. If user type in text box it will search in the table. It basically ItemId

    here is my code

    Select top 1000 ima_itemid as ItemID, ima_itemName as ItemName from ima
    where (ima_itemid LIKE @IMA_ItemID+'%' )

    This code is work fine.

    Some users need to search more specific in the text box

    Example: 110%1

    If user type this id it need to select like 1101 _ _ 11, 110_ _ _21, 110 _ _ 61 etc.,

    So i used if condition on the sql statement, but it doesnt work

    If @IMA_ItemID= N'%'
    then
    Select top 1000 ima_itemid as ItemID, ima_itemName as ItemName from ima
    where (ima_itemid LIKE @IMA_ItemID)
    Else
    Select top 1000 ima_itemid as ItemID, ima_itemName as ItemName from ima
    where (ima_itemid LIKE @IMA_ItemID+'%' )

    It means If the user type with' % ' the in my statement i need to remove. If user type only numbers the i want to include. I think i need to do with programming. But i hope we can do also in the sql statement



    Thanks in advance ur answer
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    You are trying to increase the power of your search engine,
    but you have to think - where else have I seen this power?
    It is synthesised by a "Search within results option"
    This can be achieved mostly in SQL by creating a temporary table.
    Another option, this is just a thought, but what about two text boxes?

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      Are you trying to use IF directly is part of SQL statement ?

      It is better not to let the user enter % from frontend.

      Comment

      Working...