By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,358 Members | 3,822 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,358 IT Pros & Developers. It's quick & easy.

If Condition in SQL to search

100+
P: 112
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
Apr 10 '08 #1
Share this Question
Share on Google+
2 Replies


code green
Expert 100+
P: 1,726
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?
Apr 10 '08 #2

debasisdas
Expert 5K+
P: 8,127
Are you trying to use IF directly is part of SQL statement ?

It is better not to let the user enter % from frontend.
Apr 10 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.