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*
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
Author - SQL Server T-SQL Recipes
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
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply