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

Extracting a String Within Delimeters - Part 2 Expand / Collapse
Author
Message
Posted Monday, July 26, 2004 4:49 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, August 1, 2011 2:05 PM
Points: 32, Visits: 47
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/colu


Post #128292
Posted Monday, September 13, 2004 6:16 AM
SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: Moderators
Last Login: Friday, November 14, 2014 10:06 AM
Points: 8,370, Visits: 745

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




Post #136587
Posted Monday, September 13, 2004 8:01 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, October 2, 2008 8:14 AM
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

Post #136637
Posted Monday, September 13, 2004 9:46 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, December 10, 2014 11:06 AM
Points: 1,113, Visits: 706
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
Post #136677
Posted Monday, September 13, 2004 4:27 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, November 19, 2013 10:40 AM
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
*/

Post #136765
Posted Tuesday, September 14, 2004 2:49 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, August 1, 2011 2:05 PM
Points: 32, Visits: 47
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.


Post #137042
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse