Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 allowing ? in TSQL equal to _
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

148 Posts

Posted - 11/09/2012 :  10:39:41  Show Profile  Reply with Quote
Hi All:

I have a TSQL stored procedure like this:

ALTER PROCEDURE [dbo].[SearchEntityDataNew]
@SearchText varchar(100) = '*'


IF CHARINDEX('*', @SearchText) = 0
SET @SearchText = '%' + @SearchText + '%'
SET @SearchText = REPLACE(@SearchText, '*', '%')

SELECT 'Entity' as pagetype,EntityID,EntityData,EntityDataID
FROM EntityData
WHERE EntityData LIKE @SearchText
union all
SELECT 'Property' as pagetype,PropertyID,PropertyValue,EntityDataID
FROM EntityDataProperty
WHERE PropertyValue LIKE @SearchText

I want to allow '?' in it which will be equal to '_' operator. How can I allow '?' so that if user types t_ then 'to' is returned. If user types _t then 'It' is returned.

Please suggest me solution to this:

Asif Hameed

Flowing Fount of Yak Knowledge

6065 Posts

Posted - 11/09/2012 :  14:50:39  Show Profile  Reply with Quote
Not sure I understand your request. As you seemed to indicate the underscore <_> can be used with LIKE to wildcard any single character. You already have code to replace a star <*> with a percent <%>. So can't you do the same thing when a user enters a question mark <?> replace it with an underscore <_> ?

SET @SearchText = replace(REPLACE(@SearchText, '*', '%'),'?','_')

Be One with the Optimizer
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000