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

How to replace first occurrance of pipe delimeter Expand / Collapse
Author
Message
Posted Tuesday, November 16, 2010 3:43 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 30, 2013 12:56 PM
Points: 146, Visits: 613
I have the following string that starts off with a pipe delimeter:

| hello | world | I love sql server

How do I strip off the first | pipe delimeter?

so that my output is like this:

hello | world | I love sql server

Post #1021839
Posted Tuesday, November 16, 2010 3:44 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 30, 2013 12:56 PM
Points: 146, Visits: 613
some other records also come in w/o a pipe as the first character, so I don't want to strip off the first character if its not a pipe.
Post #1021840
Posted Tuesday, November 16, 2010 9:35 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 4:19 AM
Points: 2,370, Visits: 4,415
You can use the SUBSTRING and LEN function as below..

DECLARE	@strVariable VARCHAR(100)

SELECT @strVariable = '| hello | world | I love sql server'

SELECT CASE
WHEN SUBSTRING( @strVariable, 1, 1 ) = '|'
THEN SUBSTRING( @strVariable, 2, LEN( @strVariable ) - 1 )
ELSE @strVariable
END Variable




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1021902
Posted Wednesday, November 17, 2010 6:01 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 30, 2013 12:56 PM
Points: 146, Visits: 613
Thank you very much! Really appreciate it.
Post #1022101
Posted Thursday, November 18, 2010 3:58 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, March 22, 2013 3:37 PM
Points: 79, Visits: 1,317
For fun 2 slight variations based on Kingston's.

DECLARE @strVariable VARCHAR(100)

SELECT @strVariable = '| hello | world | I love sql server'

SELECT CASE SUBSTRING(@strVariable, 1, 1)
WHEN '|'
THEN SUBSTRING(@strVariable, 2, LEN(@strVariable) - 1)
ELSE @strVariable
END Variable

SELECT CASE CHARINDEX('|', @strVariable)
WHEN 1
THEN SUBSTRING(@strVariable, 2, LEN(@strVariable) - 1)
ELSE @strVariable
END Variable


Post #1023213
Posted Thursday, November 18, 2010 5:04 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 4:29 AM
Points: 1,337, Visits: 4,026
And one more for luck:

If you are looking to strip a local variable:

DECLARE @strVariable VARCHAR(100)

SELECT @strVariable = '| hello | world | I love sql server'

--== Only strip out the first character if it is a pipe.

SELECT @strVariable = STUFF(@strVariable,1,1,'')
WHERE LEFT(@strVariable,1) = '|'

SELECT @strVariable

If you are stripping a column in a table:
--== If the first character is a pipe, stuff it in the bin otherwise don't

SELECT CASE
WHEN LEFT(YourColumn,1)='|' THEN STUFF(YourColumn ,1,1,'')
ELSE YourColumn
END as StrippedPipe
FROM YourTable



MM




Post #1023231
Posted Monday, December 27, 2010 1:53 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, December 21, 2012 6:44 AM
Points: 26, Visits: 133
What about a simple "SUBSTRING" & "PATINDEX"

DECLARE @str1 VARCHAR(100), @str2 VARCHAR(100), @str3 VARCHAR(100)
SET @str1 = 'hello | world | I love sql server'
SET @str2 = '| '+@str1
SET @str3 = '| | | ' + @str1

SELECT '*'+@str1+'*', '*'+SUBSTRING(@str1,PATINDEX('%[0-z]%', @str1), LEN(@str1))+'*'
SELECT '*'+@str2+'*', '*'+SUBSTRING(@str2,PATINDEX('%[0-z]%', @str2), LEN(@str2))+'*'
SELECT '*'+@str3+'*', '*'+SUBSTRING(@str3,PATINDEX('%[0-z]%', @str3), LEN(@str3))+'*'

*hello | world | I love sql server* *hello | world | I love sql server*
*| hello | world | I love sql server* *hello | world | I love sql server*
*| | | hello | world | I love sql server* *hello | world | I love sql server*
Post #1039574
Posted Monday, December 27, 2010 2:10 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:21 PM
Points: 6,388, Visits: 8,290
If it will always be the first two characters that you want removed, just use the STUFF function:

declare @str varchar(500);
set @str = '| hello | world | I love sql server';
select stuff(@str, 1, 2, '');



Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1039579
Posted Monday, December 27, 2010 2:11 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, June 17, 2013 1:45 PM
Points: 15,442, Visits: 9,572
WayneS (12/27/2010)
If it will always be the first two characters that you want removed, just use the STUFF function:

declare @str varchar(500);
set @str = '| hello | world | I love sql server';
select stuff(@str, 1, 2, '');



And if you want to do that to rows in a table, just add a Where clause that checks "Where MyStringColumn like '|%'".


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1039581
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse