Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Extracting a String Within Delimeters - Part 2


Extracting a String Within Delimeters - Part 2

Author
Message
Lashams
Lashams
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 48
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/colu



Antares686
Antares686
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: Moderators
Points: 8424 Visits: 780

There is a typo in the SELECT code, needs to be

 

SELECT Particulars,
     LTRIM(RTRIM(SUBSTRING(
       /* <text>> */ Particulars,
       /* <start > */   CHARINDEX(';',Particulars,1) + 1,
       /* <length> */   CHARINDEX(';',
          SUBSTRING(Particulars, CHARINDEX(';', Particulars, 1) + 1, 99) + ';') - 1
      ) ) ) AS Result_string
FROM Sample_table





Code Monkey-158952
Code Monkey-158952
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 5

The innermost SUBSTRING will yield this:

 LUC20031026901 ; Vehicle
 Consolidated
 SUB35030172701 ; Building
 LIQ200310121 ; Liquor

and when the semicolon is added, it will look like this:

 LUC20031026901 ; Vehicle                                                                          ;
 Consolidated                                                                                      ;
 SUB35030172701 ; Building                                                                         ;
 LIQ200310121 ; Liquor                                                                             ;

 




Best Regards,

Code Monkey

Adam Machanic
Adam Machanic
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1141 Visits: 714
Wouldn't it be easier to just use:

SELECT RIGHT(RTRIM(Particulars), LEN(RTRIM(Particulars)) - CHARINDEX(';',Particulars) - 1)
FROM Sample_table

Also, you could remove the RTRIMs if you used VARCHAR instead of CHAR...

--
Adam Machanic
SQL Server MVP
SQLblog.com: THE SQL Server Blog Spot on the Web
Norbert L. Muth, M.A.
Norbert L. Muth, M.A.
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 13

drop table Sample_table
CREATE TABLE #Sample_table (Particulars varchar(120))
GO
INSERT INTO #Sample_table VALUES('LDR ; LUC20031026901 ; Vehicle')
INSERT INTO #Sample_table VALUES('LDR ; Consolidated                                                     ')
INSERT INTO #Sample_table VALUES('LDR ; SUB35030172701 ; Building')
INSERT INTO #Sample_table VALUES('LDRR ; LIQ200310121 ; Liquor')

select * from #Sample_table

SELECT Particulars,
--    quotename( -- enable this to see, that's just the string without spaces around
      LTRIM(RTRIM(
  SUBSTRING(Particulars,
   /*from*/CHARINDEX(';',Particulars,1)+1,   -- search first delimiter and then offset 1 from first deliminter

   /* to */CHARINDEX(';',                    -- search for the next delimter as the end-of-string
     SUBSTRING (Particulars,
         /*from*/    (CHARINDEX (';', Particulars,1)+1) -- the first character after first delimiter
         /* to */   , datalength(Particulars)     -- use datalength for not beeing bound to specific length
         ) + ';'  -- for any case add a delimiter (this was a good idea from Denis Oliynik !!), if it's an additonal, it's just lost
      ) - 1  -- delimiter Position minus one    
        )
     ) -- rigth parenthesis for lrim
       ) -- rigth parenthesis for rtrim
--  rigth parenthesis for quotename
FROM #Sample_table

/* commet out ltrim/rtrim to extract the whole string with spaces
This is a very general select not delimited to programmed stringlenthes or varchar's etc. It just extracts the first delimited string within another string, not more or less.

sincerely
Norbert L.Muth, M.A.
DBA of KNO-VA.de
Norbert.Muth@gmx.de
*/


Lashams
Lashams
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 48
Thanks for the feedback. The typo occurred in translation from the submitted MS/Word document to the HTML page and was an accident by the publisher that I did not pick up on. Please accept my apologies for this.



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search