sql function to query a Table based on searching a column for a substring - sql

I googled this but couldn't find an easy answer:
I need to write a function that takes a substring as parameter and return those rows which have this substring in a certain column:
select * from myTable where myTable.someColumn contains #substring
appreciate your help
thank you

you may use
where myTable.someColumn like '%?%'
with '#substring' as parameter.

Related

How to fetch values from a column where extension should be .jpg only?

I need to display records from a column whose has only ".Jpg" Extension
How can I do so is sql query.
Any help would be helpful
Hello you can make use of wildcard operations in SQL using like clause. % in below represents the name of the file and will pull all records with .jpg extension.
select * from table_name where column_name like '%.jpg'
You can use function CHARINDEX().
The CHARINDEX() function searches for a substring in a string, and returns the position.
If the substring is not found, this function returns 0. It's more better than LIKE especially when you have spaces and other symbols in your column.
SELECT * FROM TABLE WHERE
CHARINDEX('.jpg', column-name) >0

SQL - just view the description for explanation

I would like to ask if it is possible to do this:
For example the search string is '009' -> (consider the digits as string)
is it possible to have a query that will return any occurrences of this on the database not considering the order.
for this example it will return
'009'
'090'
'900'
given these exists on the database. thanks!!!!
Use the Like operator.
For Example :-
SELECT Marks FROM Report WHERE Marks LIKE '%009%' OR '%090%' OR '%900%'
Split the string into individual characters, select all rows containing the first character and put them in a temporary table, then select all rows from the temporary table that contain the second character and put these in a temporary table, then select all rows from that temporary table that contain the third character.
Of course, there are probably many ways to optimize this, but I see no reason why it would not be possible to make a query like that work.
It can not be achieved in a straight forward way as there is no sort() function for a particular value like there is lower(), upper() functions.
But there is some workarounds like -
Suppose you are running query for COL A, maintain another column SORTED_A where from application level you keep the sorted value of COL A
Then when you execute query - sort the searchToken and run select query with matching sorted searchToken with the SORTED_A column

Selection based on string containing one or many other strings

I have a table 'forms' with a column 'title'.
Inside my stored procedure I create a temp table '#temp1' with the column 'searchVals'.
Multiple strings are parsed into the stored procedure, the number of strings is variable.
What I'm trying to achieve is selecting a row if the 'title' contains one or many 'searchVal'. But I haven't been able to achieve this successfully yet.
For example something like this
SELECT title FROM #temp1 WHERE title IN ('%'+(SELECT searchVal FROM #temp1)+'%')
The issue is obviously that IN doesn't allow wildcards but if it did, this is the type of result I'm trying to achieve.
Stacking ORs also doesn't work as the number of rows in #temp1 is variable.
Predicate and free-text searches don't work as the forms database isn't indexed appropriately.
I've come up with what I think is a valid solution but I can't seem to construct it correctly. The function would be called from the WHERE clause and return a boolean. I'm sorry if this has been answered elsewhere, I did search extensively.
Below is my proposed solution with pseudocode code where I'm not sure of the interchangeable SQL code.
DECLARE FUNCTION dbo.checkVal(searchVal column, formTitle varchar)
RETURNS boolean
AS
BEGIN
WHILE (not end of column)
IF formTitle LIKE '%'+searchVal+'%'
RETURN true
END
END
RETURN FALSE
END
Sorry for my inconsistent and incorrect SQL code. I would appreciate any suggestions or corrections that would make my code work. Better solutions are welcome too.
Thank you.
How about to check every row of forms by an EXISTS()?
SELECT *
FROM forms f
WHERE EXISTS
(
SELECT *
FROM #temp1
WHERE f.title LIKE '%' + searchVals + '%'
)
You can use the LIKE operator in a JOIN statement:
SELECT DISTINCT f.*
FROM forms f
JOIN #temp1 t ON f.title LIKE '%' + t.column + '%'
But keep in mind that using the LIKE operator does come with a performance hit so you will want to reduce the number of rows in the forms table using a where clause if possible

Sql Server Contains

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

Use like in T-SQl to search for words separated by an unknown number of spaces

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