September 8, 2008 at 9:55 am
I would like to retreive the last string from this pattern
,B110,A104,AUSL,
,L101,A101,01,
which means i want to retrieve AUSL and 01
thanks
September 8, 2008 at 9:56 am
will you columns always end in ',' ?
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
September 8, 2008 at 9:58 am
yes,
here is the pattern we have, i need to retrieve the right most field
,,,01,
,,,01,
,B110,A104,AUSL,
,B135,,DEN,
,,,03,
,,,AZ,
,,,MALA,
,,,BUR,
thanks
September 8, 2008 at 10:02 am
HI there,
Here is my solution but I have a feeling there is better one I'll keep working at it:
DECLARE @Tmp VARCHAR(100)
SET @Tmp = ',ad,af,ag,ah,bb,'
SELECT SUBSTRING(@tmp,LEN(@tmp) -
PATINDEX('%,%',REVERSE(LEFT(@tmp,LEN(@tmp)-1)))+1,
PATINDEX('%,%',REVERSE(LEFT(@tmp,LEN(@tmp)-1)))-1)
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
September 8, 2008 at 10:26 am
Hi Chris
It's a kinda cross-post, Matt is dealing with it too:
http://www.sqlservercentral.com/Forums/Topic565481-8-1.aspx
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 8, 2008 at 10:28 am
Thanks Chris
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
September 8, 2008 at 10:51 am
Reverse would give the correct results assuming the patterns are close enough. Tally table is another option.
I'd probably go with a CLR function though (would likely be faster in this case, since we're dealing with string ops vs a REVERSE function, which is kind of a dog perf-wise)
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
September 8, 2008 at 11:25 am
some other option
DECLARE @string varchar(200)
SET @string = ',B110,A104,AUSL,'
SELECT
REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',',REVERSE(@string),2)-2))
SET @string = ',L101,A101,01,'
SELECT
REVERSE(SUBSTRING(REVERSE(@string),2,CHARINDEX(',',REVERSE(@string),2)-2))
September 8, 2008 at 11:50 am
can you tell me what tally table is? thanks
September 8, 2008 at 3:34 pm
shuzi (9/8/2008)
can you tell me what tally table is? thanks
Sure - here's a good article on it:
http://www.sqlservercentral.com/articles/TSQL/62867/[/url]
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
September 9, 2008 at 8:21 am
so how to retrieve the middle value, e.g. A104 and A101
thanks
September 9, 2008 at 8:44 am
shuzi (9/9/2008)
so how to retrieve the middle value, e.g. A104 and A101thanks
The middle value of how many values? Will it always be three?
Looks like this is going to be a tally-table job, as Matt suggests:
DROP TABLE #Temp
CREATE TABLE #Temp (RowID int, String VARCHAR(20))
INSERT INTO #Temp (RowID, String)
SELECT 1, ',B110,A104,AUSL,' UNION ALL
SELECT 2, ',L101,A101,01,'
DECLARE @Delimiter VARCHAR(20)
SET @Delimiter = ','
SELECT t.RowID, number, SUBSTRING(t.String+@Delimiter, number,
CHARINDEX(@Delimiter, t.String+@Delimiter, number) - number)
FROM Numbers n, #Temp t
WHERE number <= LEN(REPLACE(t.String,' ','|'))
AND SUBSTRING(@Delimiter + t.String, number, LEN(REPLACE(@Delimiter,' ','|'))) = @Delimiter
ORDER BY RowID, number
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 9, 2008 at 9:16 am
no, it varies with the length
September 9, 2008 at 10:15 am
Then use the starting point Chris gave you with the Tally, and use that as a "base table", excluding the first and last "new rows" based on the initial rowID. Once you do that - you have all of your "middle values".
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
September 19, 2008 at 10:09 am
I tried this but it gives me error Number is invalid table
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply