I am trying to search all instances of an expression used in stored procedures and have this:
SELECT name, type_desc, create_date, modify_date
FROM sys.procedures
WHERE OBJECT_DEFINITION(object_id) LIKE '%ufn_TurnFlagOn(5,ColumnA)%'
ORDER BY name
But this will not return the sps that may have a space between the function parameters like:
ufn_TurnFlagOn( 5,ColumnA )
ufn_TurnFlagOn(5, ColumnA)
ufn_TurnFlagOn( 5,ColumnA)
Please note:
I cannot use the "_" wildcard like this LIKE
'%ufn_TurnFlagOn(_5_,_ColumnA_)%' because it matches exactly a
single character, hence it will not match
ufn_TurnFlagOn(5,ColumnA) and some more.
I do not have CLR Integration (nor can I register it now) hence I
cannot use regexes.
Alternative is to run the query multiple times with all possibilities, but is there a better way to accomplish this in 1 query?
If spaces are the only problem, you could just use REPLACE to remove them, and then have:
WHERE REPLACE(OBJECT_DEFINITION(object_id), ' ', '')
LIKE '%ufn_TurnFlagOn(5,ColumnA)%'
Related
Using SQL Server 2008, without using full-text indexing or CLR integration, is there a better way to search a table column for arbitrary text than the following:
declare #SubString nvarchar(max) = 'Desired substring may contain any special character such as %^_[]'
select * from Items where Name like '%' +
replace(
replace(
replace(
replace(
replace(
replace(
#SubString
,';',';;')
,'%',';%')
,'[',';[')
,']',';]')
,'^',';^')
,'_',';_')
+ '%' escape ';'
That works, but it seems unnecessarily barbaric. Is there any simpler, clearer, or more efficient way, given the above limitations?
There are no constraints on what the Name column might contain, nor what the SubString we're searching for might contain.
(You can use charindex for straight string searches such as finding every firstname that contains Ben with:
select * for customers where charindex('Ben', firstname) > 0
or you can create all kinds of powerful pattern searching with patindex:
SELECT PATINDEX ('%[0-9].%[0-9].%[0-9].%','278.2.6.49');
which returns the number before the first dot or 0 if there aren't three sets of numbers in the string.
For a very good introduction of using patterns (its not regular expressions) in TSQL (from where the above example was copied) please see:
https://www.simple-talk.com/sql/t-sql-programming/patindex-workbench/
The only thing I can think of is to turn your replace(replace(replace... into a user-defined function. Then you could streamline your query into:
SELECT * FROM Items WHERE Name LIKE CleanMyString(#Substring) escape ';'
It's not exactly "simpler". It's not likely to be any more efficient. But it does clean up the syntax and make it cleaner-looking.
I am writing a simple query in SQL Server:
SELECT *
FROM Table1
WHERE CONTAINS(Column1, 'MY')
but it doesn't return any results. While using like it returns results.
Is there any specific reason why the keyword 'MY' doesn't work?
Update:
If I use other keywords, it works, only the specific 'MY' seems to be that I cannot used. My column is already set into fulltext index. Also for performance purposes I prefer to use CONTAINS.
you could do something like
select * from Table1 where Column1 like '%MY%'
When you use linq you can use the statements like you use in C#. It will be translated to SQL, a language which database can execute. So, try something like this:
string sql = "SELECT * FROM Table1 WHERE Column1 like #Column1";
And in your command, try to add parameters to replace the #Column1
command.Parameters.AddWithValue("#Column1", "%MY%");
The % char, represents anything. So, if you want to do a command like StartWith like we do from string, just use MY%.
i want to select name that only contain one word with SQL wildcards..
i have tried
select name from employee where name not like '% %'
it works,but i wonder if there are other ways to do it using SQL wildcards
note : i am a college student,i am studying wildcards right now . i was just wonder if there are other ways to show data that only contain one word with wildcards except the above..
Your method makes proper use of wildcards, alternatively you could do it with CHARINDEX or similar function depending on RDBMS
select name
from employee
where CHARINDEX(' ',name) = 0
Likewise the patindex function or similar use wildcards, but that's pretty much the same as CHARINDEX, just allows for patterns, so if looking for multiple spaces it would be helpful. I don't think there's much in the way of variation from your method for using wildcards.
If you have large database I would suggest to create new indexed column word_count which would be autofilled by insert/update trigger. Thus you will be able to search for such records more efficiently.
That's the way I'd do it using wildcards. The other way would be:
select name
from employee
where charindex(' ', name) = 0
I need to match on a partial string but can't turn full-text indexing on so can't use contains. I've looked at Levenstein's function for determining the distance between two strings but I'm not looking for fuzzy matching but that every character in the column exists in the string.
I.e. If the string being passed is something like AB_SYS_20120430.TXT I want to match on any columns containing AB_SYS. The like predicate isn't getting me there. I really need the equivalent of the .NET contains feature but as mentioned turning on full text indexing isn't an option to be turned on. Thought I would see if there were any other possible work arounds.
Are you looking for the LIKE function?
http://www.w3schools.com/sql/sql_like.asp
... WHERE MyColumn LIKE '%AB_SYS%'
That may not be optimal, but it seems like it answers your question... If you can search from only the left or right side that could further optimize.
That is functionally similar to String.Contains
http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx
EDIT: How will you parse the input text into the "relevant" substring?
EDIT: To search the same LIKE condition but reverse, from your partial column to the complete literal, simply append the wildcard characters:
... WHERE 'AB_SYS_20120430.TXT' LIKE '%' + MyColumn + '%'
EDIT: You have suggested that you can't get it to work. If you add the schema do your question then I can help you further but consider this:
You have a table called MyTable
In that table there is a column called MyColumn
Some rows in that table have the data 'AB_SYS' in MyColumn
Given the parameter 'AB_SYS_20120430.TXT' you want to return all matching rows
CREATE PROCEDURE MyTestProcedure
#pFullNameString nvarchar(4000) = '' -- parameter passed in, like AB_SYS_20120430.TXT
AS
BEGIN
SELECT
*
FROM
MyTable
WHERE
#pFullNameString LIKE '%' + MyTable.[MyColumn] + '%'
END
GO
You could use CHARINDEX
WHERE CHARINDEX(StringToCheckFor, StringToCheckIn) > 0
I have this query:
select * from table where column like '%firstword[something]secondword[something]thirdword%'
What do I replace [something] with to match an unknown number of spaces?
Edited to add: % will not work as it matches any character, not just spaces.
Perhaps somewhat optimistically assuming "unknown number" includes zero.
select *
from table where
REPLACE(column_name,' ','') like '%firstwordsecondwordthirdword%'
The following may help: http://blogs.msdn.com/b/sqlclr/archive/2005/06/29/regex.aspx
as it describes using regular expressions in SQL queries in SQL Server 2005
I would definitely suggest cleaning the input data instead, but this example may work when you call it as a function from the SELECT statement. Note that this will potentially be very expensive.
http://www.bigresource.com/MS_SQL-Replacing-multiple-spaces-with-a-single-space-9llmmF81.html