|
|
|
SSC-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
|
|
|
|
|
SSC-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.
|
|
|
|
|
SSCrazy
      
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/
|
|
|
|
|
SSC-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.
|
|
|
|
|
SSC 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
|
|
|
|
|
Ten 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
|
|
|
|
|
SSC 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*
|
|
|
|
|
SSCertifiable
       
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
|
|
|
|
|
SSCoach
         
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
|
|
|
|