July 5, 2005 at 8:27 am
They all work fine.. some are just a tad faster... and easier to undertsand .
July 5, 2005 at 8:31 am
You are right Remi and once again thanks for your help!
July 5, 2005 at 10:04 am
What you think about a function?
CREATE FUNCTION noleftzero(@word varchar(20))
RETURNS varchar(20)
AS
BEGIN
WHILE PATINDEX('0%', @word) = 1
BEGIN
set @word = right(@word,len(@word)-1)
END
RETURN(@word)
END
go
/*************/
SELECT dbo.noleftzero('L0001AA') as nozero
I'm not a expert but a think this is good.
From a friend
July 5, 2005 at 10:05 am
CREATE FUNCTION noleftzero(@word varchar(20))
RETURNS varchar(20)
AS
BEGIN
WHILE PATINDEX('0%', @word) = 1
BEGIN
set @word = right(@word,len(@word)-1)
END
RETURN(@word)
END
go
/*************/
SELECT dbo.noleftzero('L0001AA') as nozero
July 5, 2005 at 10:10 am
You seem to be the expert on paterns |
Who me! No way!
Jack of all Trades, me, Master of None
Do you have a tutorial or a list of exemples of real life pattern searches (bol doesn't offer much in real life cases) |
Nope, fraid not. Took syntax from BOL and experimented a lot!
Far away is close at hand in the images of elsewhere.
Anon.
July 5, 2005 at 10:11 am
This function will run on each line which takes more than than writing it in the select. Secondly, David's version will run only once, therefore much faster than yours which will run once for each leading 0 + 1 :
SUBSTRING([name],PATINDEX('%[^0]%',[Name]),LEN([Name]))
July 5, 2005 at 10:13 am
You're the only one who's continuously sending answers with pathindex and they more often than not beat the crap out of what anyone else can come up with. Maybe you should try to write a tutorial and publish it, or at least send it this way.
July 5, 2005 at 2:42 pm
Expanding on David's solution, if you wanted to, you could remove the leading zeroes but keep spaces for these zeroes using the following:
REPLICATE(' ', PATINDEX('%[^0]%', [Name]) - 1) + SUBSTRING([name],PATINDEX('%[^0]%',[Name]),LEN([Name]))
Not usually what you want to do BUT there almost always seems to be a need for (at least once) for every scenario.
hawg
----------------------------------------------------------01010011010100010100110000100000010100110110010101110010011101100110010101110010001000000101001001101111011000110110101101110011
Viewing 10 posts - 16 through 24 (of 24 total)
You must be logged in to reply to this topic. Login to reply