SQL Server Full text search Contains function - sql-server

İ have a problem with contains function, when i search with like '%ZAM%' operator, it finds all word that contains ZAM like ZAMANLAMA AZAMI ZAM and etc.. but when I use fts index contains function, it just find ZAM ZAMANLAMA but it doesnt find AZAMI or 123ZAM789. I have also tried CONTAINS (YourColumn, ' "ZAM" ' ) but it doesn't work. Please help me , fts is very fast but it could not find all contains like '%%' operator what should I do ?

You can use "*" before in contain syntax same as like operator . but you need to use double quote before and after the search string.
Try this query once.
SELECT *
FROM YourTable
WHERE CONTAINS(YourColumn,'"*ZAM*"');
(OR)
select * from YourTable where YourColumn like '%ZAM%'

Related

How to search in a column (containing the start of the word) the ole word?

I want to create a select query that return the outcome like this :
A table that contains MatrixCode column with such values like this: 'shampo250', 'condishioner500', and 'AMO200'.
The query will check for each word in the column whether it is contained within text
SELECT *
FROM [t_MO_MatrixCodeMapping]
WHERE 'AMO200IL351972598764' LIKE MatrixCode
I tried to use contain too but it did dot worked.
I can not cut the text from 'AMO200IL351972598764' to 'AMO200'
Are you not after this?
SELECT *
FROM [t_MO_MatrixCodeMapping]
WHERE 'AMO200IL351972598764' LIKE MatrixCode + '%';
LIKE might as well be equivilent to = in your query, as it contains no wild cards. WHERE 'AMO200IL351972598764' LIKE 'AMO200' won't resolve the true, as it is semantically equivalent to 'WHERE 'AMO200IL351972598764' = 'AMO200'.

Access SQL Like * alternative in MS SQL server

I'm working on a project that has a search engine. AS we know in MS ACCESS we could use "*" in Queries under Criteria field to retrieve all records.
In SQL Server I need the same technique. I have tried different LIKE with WHERE Clauses. But I still didn't get the exact result I want.
In this project I have 3 textboxes (Category, Item, Location). If the user leaves any of them empty. I want to retrieve all the records.
I need something like this:
string t1,t2,t3;
if(!String.IsNullOrEmpty(txtCategory.Text))
t1=txtCategory.Text;
else
t1="*";
if(!String.IsNullOrEmpty(txtItem.Text))
t2=txtItem.Text;
else
t2="*"
if(!String.IsNullOrEmpty(txtLoc.Text))
t2=txtLoc.Text;
else
t3="*";
-
-
-
// in a function i have this :
SELECT * FROM Table_Items WHERE Category='"+t1+"' AND Item='"+t2+"' AND Location='"+t3+"'"
in ms sql server you can use the same technique but instead of * you should use %.
for examples:
%: means any
a%: all strings that start with a
%z: all strings that end with z
SO, your code should look like something as below:
// codes here
t3="%";
WHERE ColumnName LIKE t3
or
Where ColumnName LIKE '%'
I hope that will help you.
Change your * to the %
... Where Category Like "'+t1+"' and Item Like '"+t2+"' ...

Concat the values in a string with SQL Server

I want to select a list of items and part numbers for for each item as a string:
SELECT top 100 *
FROM ii
OUTER APPLY
(SELECT def, ( ipr.part_number + ',') as prt
FROM ipr
WHERE ii.item_id = ipr.item_id
FOR XML PATH('') ) PN
The error is:
[Error Code: 8155, SQL State: S0002] No column name was specified for
column 1 of 'PN'.
How can I fix this?
I think that your whole OUTER APPLY statement generates one XML for both default_part_number and concatenated string, which(the whole XML) doesn't have a name.
What you could try to do would be adding alias like this AS PN(TestThis).
However, I don't think that you're expecting result you're going to get. It would better if you'd give us some example data and expected output. It will be easier for us to solve your problem in that case.
The combination of XML and STUFF is funny but perfectly fitting to your needs.
First you concat your strings with the ', ' in front, then you must return your XML with ", TPYE). You must read the result with ".value()" and use STUFF to replace the first ', '.
You'll find a lot of exampels in the net...

Sql server LIKE Operator with comma separated string

My stored procedure receives a parameter which is a comma-separated string:
DECLARE #review_status varchar(200)
SET #review_status='CANCELLED,INSPECTED,REJECTED,UNASSIGNED'
I use this variable with LIKE Operator, when value without comma then perfect but it comes with comma then unable to handle.
Now i use this statement
SELECT * FROM tblReview WHERE
review_status LIKE '%' + #review_status + '%'
I need to make from it this statement
SELECT * FROM tblReview WHERE
review_status LIKE '%CANCELLED%' OR --Pass #review_status
review_status LIKE '%INSPECTED%' OR
review_status LIKE '%REJECTED%'.....
What is the best practice for doing this?
You could use a string splitter to do this. Read this article for one of the fastest splitter there is.
Then you need to use the IN operator to do the filtering:
SELECT *
FROM tblReview
WHERE review_status IN(
SELECT item FROM dbo.DelimitedSplit8K(#review_status,',')
)
Since you're using LIKE, you may want to do JOIN instead of IN:
SELECT *
FROM tblReview t
INNER JOIN dbo.DelimitedSplit8K(#review_status, ',') s
ON t.review_status LIKE '%' + s.Item + '%'
Try to first convert varchar with commas to a table as described here http://blogs.msdn.com/b/amitjet/archive/2009/12/11/sql-server-comma-separated-string-to-table.aspx. Then, use the following:
CREATE TABLE #allPattern (
pattern NVARCHAR(max)
);
INSERT INTO #allPattern VALUES ('%CANCELLED%'), ('%INSPECTED%'), ('%REJECTED%');
SELECT t.* FROM tblReview t JOIN #allPattern a ON (t.review_status LIKE a.pattern);
You can add the pre- and trailing comma to yous string, and then use CHARINDEX().
SET #review_status=',CANCELLED,INSPECTED,REJECTED,UNASSIGNED,'
SELECT * FROM tblReview WHERE
charindex(','+review_status+',', '#review_status') >0
Commas at the beginning and at the end of the string are used to make an exact string finding possible - for example, if you have two statuses - one called CAN and the second called CANCELLED, charindex without side commas will find them both.
It's the simplest way, but not the one with the best performance, so don't use it on long CSVs.

Building dynamic query for Sql Server 2008 when table name contains " ' "

I need to fetch Table's TOP_PK, IDENT_CURRENT, IDENT_INCR, IDENT_SEED for which i am building dynamic query as below:
sGetSchemaCommand = String.Format("SELECT (SELECT TOP 1 [{0}] FROM [{1}]) AS TOP_PK, IDENT_CURRENT('[{1}]') AS CURRENT_IDENT, IDENT_INCR('[{1}]') AS IDENT_ICREMENT, IDENT_SEED('[{1}]') AS IDENT_SEED", pPrimaryKey, pTableName)
Here pPrimaryKey is name of Table's primary key column and pTableName is name of Table.
Now, i am facing problem when Table_Name contains " ' " character.(For Ex. KIN'1)
When i am using above logic and building query it would be as below:
SELECT (SELECT TOP 1 [ID] FROM [KIL'1]) AS TOP_PK, IDENT_CURRENT('[KIL'1]') AS CURRENT_IDENT, IDENT_INCR('[KIL'1]') AS IDENT_ICREMENT, IDENT_SEED('[KIL'1]') AS IDENT_SEED
Here, by executing above query i am getting error as below:
Incorrect syntax near '1'.
Unclosed quotation mark after the character string ') AS IDENT_SEED'.
So, can anyone please show me the best way to solve this problem?
Escape a single quote by doubling it: KIL'1 becomes KIL''1.
If a string already has adjacent single quotes, two becomes four, or four becomes eight... it can get a little hard to read, but it works :)
Using string methods from .NET, your statement could be:
sGetSchemaCommand = String.Format("SELECT (SELECT TOP 1 [{0}] FROM [{1}]) AS TOP_PK, IDENT_CURRENT('[{2}]') AS CURRENT_IDENT, IDENT_INCR('[{2}]') AS IDENT_ICREMENT, IDENT_SEED('[{2}]') AS IDENT_SEED", pPrimaryKey, pTableName, pTableName.Replace("'","''"))
EDIT:
Note that the string replace is now only on a new, third substitution string. (I've taken out the string replace for pPrimaryKey, and for the first occurrence of pTableName.) So now, single quotes are only doubled, when they will be within other single quotes.
You need to replace every single quote into two single quotes http://beyondrelational.com/modules/2/blogs/70/posts/10827/understanding-single-quotes.aspx

Resources