Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Patindex Expand / Collapse
Author
Message
Posted Wednesday, September 10, 2008 11:31 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, July 25, 2014 5:21 AM
Points: 1,205, Visits: 923
Comments posted to this topic are about the item Patindex

Manie Verster
Developer
Johannesburg
South Africa

I can do all things through Christ who strengthens me. - Holy Bible
I am a man of fixed and unbending principles, the first of which is to be flexible at all times. - Everett Mckinley Dirkson (Well, I am trying. - Manie Verster)
Post #567477
Posted Thursday, September 11, 2008 1:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, September 11, 2008 1:31 AM
Points: 1, Visits: 2
i have a job on my server which takes data from linked server which generally takes 10-15 mins but
from past few days it takes 1 hour to complete or the job fails giving the below error

Message
Executed as user: NT AUTHORITY\SYSTEM. Cannot fetch a row from OLE DB provider "SQLNCLI" for linked server "MYSERVER". [SQLSTATE 42000] (Error 7330) OLE DB provider "SQLNCLI" for linked server "MYSERVER" returned message "Query timeout expired". [SQLSTATE 01000] (Error 7412). The step failed.
Post #567511
Posted Thursday, September 11, 2008 4:53 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Sunday, May 4, 2014 7:48 PM
Points: 369, Visits: 217
"So if you want to memorize functions that you can't remember the syntaxes for, write an article for SQLServerCentral.com. "

Thats good advice.
Post #567607
Posted Thursday, September 11, 2008 5:00 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, July 11, 2014 8:44 AM
Points: 69, Visits: 68
The PATINDEX function provide you with the exact position of a pattern in the expression e.g. if you want to know where "f" is in the expression "abcdef" then the answer will be 5 using the PATINDEX function.


Returns 6 doesnt it?

Post #567610
Posted Thursday, September 11, 2008 7:35 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, May 20, 2014 8:49 AM
Points: 200, Visits: 613
Yes, 6 it is.
Post #567709
Posted Thursday, September 11, 2008 7:35 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 6:02 AM
Points: 2,668, Visits: 19,229
Your solution works if the original set is small, like your example of 'January:February:March', but as you pointed out, gets ugly the larger your string gets.

Two things -
1) use CHARINDEX, not PATINDEX, because you can specify a starting point in CHARINDEX. Then it's just a matter of finding the previous ':' and going from there. By the way, you don't actually need the '%' in your search string when using CHARINDEX, you can just search for CHARINDEX(':',monthname)
2) you can use a loop to find these values (see below) but the best way to do this, especially if you have either an undetermined length or a large value, is to use a Tally or Numbers table. See Jeff Moden's article on Tally tables here:http://www.sqlservercentral.com/articles/TSQL/62867/, including splitting a string.

Here's a sample loop that will accomplish this for the year's worth of months, not nearly as efficient as a tally table, but does the job.
IF OBJECT_ID('tempdb..#concatmonth') IS NOT NULL BEGIN DROP TABLE #concatmonth END

create table #concatmonth(monthname varchar(255))
insert #concatmonth(monthname)select 'January:February:March:April:May:June:July:August:September:October:November:December'

-- create a table to store results in
IF OBJECT_ID('tempdb..#MyHead') IS NOT NULL BEGIN DROP TABLE #MyHead END
create table #MyHead (PK int identity(1,1),
monthnames varchar(255))
--original solution
select monthname, LEFT(monthname,PATINDEX('%:%',monthname)-1) as firstmonth,
SUBSTRING(monthname,PATINDEX('%:%',monthname)+1,LEN(monthname)) secondpart,
LEFT(SUBSTRING(monthname,PATINDEX('%:%',monthname)+1,LEN(monthname)),PATINDEX('%:%',SUBSTRING(monthname,PATINDEX('%:%',monthname)+1,LEN(monthname)))-1) secondmonth,
SUBSTRING(SUBSTRING(monthname,PATINDEX('%:%',monthname)+1,LEN(monthname)),PATINDEX('%:%',SUBSTRING(monthname,PATINDEX('%:%',monthname)+1,LEN( monthname)))+1,LEN(SUBSTRING(monthname,PATINDEX('%:%',monthname)+1,LEN(monthname)))) thirdmonth
from #concatmonth

--new solution
DECLARE @currentLocation int, @currentString varchar(255)
SET @currentLocation = 1
SET @currentString = (SELECT monthname FROM #concatmonth)

WHILE charindex(':',@currentString,@currentLocation)>0
BEGIN
INSERT INTO #MyHead
SELECT substring(@currentString,1,charindex(':',@currentString,@currentLocation)-1)

SELECT @currentString = substring(@currentString,charindex(':',@currentString,@currentLocation)+1,len(@currentString)-charindex(':',@currentString,@currentLocation)+1)
END
-- one last time to catch last iteration without ending ':'
INSERT INTO #myHead
SELECT @currentString

SELECT * FROM #MyHead ORDER BY PK



---------------------------------------------------------
How best to post your question
How to post performance problems
Tally Table:What it is and how it replaces a loop

"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."
Post #567710
Posted Thursday, September 11, 2008 8:01 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 10, 2013 2:38 PM
Points: 14, Visits: 59
I'd be a a little concerned about the performance implications of using this function, what's been your experience on cpu impact.
Post #567734
Posted Thursday, September 11, 2008 8:20 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, May 12, 2014 1:27 PM
Points: 1,386, Visits: 824
i would have been more interested to see a unique application of the PATINDEX function, rather than examples that could have more easily been CHARINDEX situations. Searching for PATINDEX('%ber%',monthname) > 0 for example.
Post #567756
Posted Thursday, September 11, 2008 8:45 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, August 14, 2014 3:06 AM
Points: 505, Visits: 388
I'm always splitting concatenated strings into rows/whatnot so I knocked the following function up to give me the (n)th value in a delimited string. Bung it in a loop and you get your insert-all-rows done in a few lines of mini-code... may be useful for some of you. Could substitute charindex for patindex to keep this fully on-topic I suppose!

ufn_midinstance(@string, @delimiter, @instance, @length)

@string = string to parse
@delimiter = obvious...
@instance = zero-based value in the delimiter string to return
@length = number of chars to return for the value, or use 0 to get the whole value up to the next delimiter

eg: select ufn_midinstance('hello:world:what:a:boring:example', ':', 2,0)
returns: 'what'

eg: select ufn_midinstance('hello:world:what:a:boring:example', ':', 2,2)
returns: 'wh'

Then you can wrap 'er up in a loop to insert with something like:

declare @string varchar(100)
set @string = 'hello:world:what:a:boring:example'
declare @loop int
set @loop = 0
while @loop <= len(@string) - len(replace(@string,':',''))
begin
insert into SomeTable (SomeColumn)
select dbo.ufn_midinstance(@string, ':', @loop, 0)
set @loop = @loop+1
end




-------ufn_midinstance--------
create function ufn_midinstance(@string nvarchar(4000), @delimiter char, @instance int, @length int)
returns nvarchar(4000)
as
begin

declare @i int
set @i = 0

while @i < @instance
begin

set @string = substring(@string, charindex(@delimiter, @string,1)+1, len(@string))
set @i = @i+1
end

declare @string2 nvarchar(4000)
if charindex(@delimiter, @string, 1)>=1 set @string2 = substring(@string, 1, charindex(@delimiter, @string,1)-1)
else set @string2 = @string

set @string = substring(@string, 1, @length)

declare @return nvarchar(4000)

if @length = 0 set @return = @string2 else set @return = @string

if @return = '' set @return = null
return @return

end
----------------------
Post #567805
Posted Thursday, September 11, 2008 9:01 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 6:34 AM
Points: 1,566, Visits: 1,851
Some time ago, I checked BOL to read about the PATINDEX string function, because I wanted to use a regular expression instead of CHARINDEX with a character string.
Unfortunately, the examples shown in BOL looked to me like CHARINDEX usage.
I didn't see comparison of when to use PATINDEX rather than CHARINDEX, except for a note that CHARINDEX cannot be used on text, ntext or image datatypes.

Digging deeper, I DID find the explanation of wildcards, including the use of brackets like [0-9] for "any number" or [^0-9] for "not a number".
I also learned that you could use these "regular expression"-type wildcards with CHARINDEX and LIKE, which is handy information. CHARINDEX has the added feature of being able to specify a starting position for the search.

As near as I could see, the only time you need to use PATINDEX is for text, ntext datatypes.
Does anyone know if there is an advantage to using PATINDEX?

(Furthermore, the only WHERE conditions that you can use on text columns are LIKE, IS NULL, and PATINDEX. Makes me wonder if PATINDEX was designed for use with text, ntext datatypes as opposed to char, nchar, varchar, etc.)
Post #567824
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse