ورود

View Full Version : نباز به یک تابع



eyelash
چهارشنبه 06 دی 1385, 22:52 عصر
وقت همه به خیر
من به یک تابع مثل تابع INSTR وی بی احتیاج دارم که وجود یک رشته در رشته ی دیگر را
بررسی کنه ولی نتونستم اون را توی SQL پیدا کنم. ممنون می شو دوستان معرفی کنن.

عفت بزرگه
چهارشنبه 06 دی 1385, 23:03 عصر
PATINDEX ( '%pattern%' , expression )

------
PATINDEX

Returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern is not found, on all valid text and character data types.
Syntax

PATINDEX ( '%pattern%' , expression )
Arguments

pattern
Is a literal string. Wildcard characters can be used; however, the % character must precede and follow pattern (except when searching for first or last characters). pattern is an expression of the short character data type category.
expression
Is an expression, usually a column that is searched for the specified pattern. expression is of the character string data type category.
Return Types

int
Remarks

PATINDEX is useful with text data types; it can be used in a WHERE clause in addition to IS NULL, IS NOT NULL, and LIKE (the only other comparisons that are valid on text in a WHERE clause).
If either pattern or expression is NULL, PATINDEX returns NULL when the database compatibility level is 70. If the database compatibility level is 65 or earlier, PATINDEX returns NULL only when both pattern and expression are NULL.
Examples

A. Use a pattern with PATINDEX

This example finds the position at which the pattern "wonderful" begins in a specific row of the notes column in the titles table.
USE pubsGOSELECT PATINDEX('%wonderful%', notes)FROM titlesWHERE title_id = 'TC3218'GOHere is the result set:
----------- 46 (1 row(s) affected)If you do not restrict the rows to be searched by using a WHERE clause, the query returns all rows in the table and reports nonzero values for those rows in which the pattern was found and zero for all rows in which the pattern was not found.
B. Use wildcard characters with PATINDEX

This example uses wildcards to find the position at which the pattern "won_erful" begins in a specific row of the notes column in the titles table, where the underscore is a wildcard representing any character.
USE pubsGOSELECT PATINDEX('%won_erful%', notes)FROM titlesWHERE title_id = 'TC3218'GOHere is the result set:
------------46(1 row(s) affected)If you do not restrict the rows to be searched, the query returns all rows in the table and reports nonzero values for those rows in which the pattern was found.

eyelash
چهارشنبه 06 دی 1385, 23:09 عصر
ایول
ممنون از جوابتون