Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 getting specific no of digits

Author  Topic 

mrtanvirali
Starting Member

41 Posts

Posted - 2010-05-26 : 07:14:15
I've field that contains "/23/34/45/67/89/101/" data

now i want to get value between first two slashes e.g. /23/ = 23

Sachin.Nand

2937 Posts

Posted - 2010-05-26 : 07:28:49
is the field value always going to start with "/"?

PBUH
Go to Top of Page

mrtanvirali
Starting Member

41 Posts

Posted - 2010-05-26 : 07:32:53
yea, always starts with '/'
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-05-26 : 07:38:00
declare @x varchar(max)
select @x= '/23/34/45/67/89/101/'

select replace((substring(@x,1,CHARINDEX('/',@x,2))),'/','')

PBUH
Go to Top of Page

mrtanvirali
Starting Member

41 Posts

Posted - 2010-05-26 : 07:41:00
wow, very nice,

thanks very much
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-26 : 07:41:51
I have adapted a function created by dinakar:
CREATE FUNCTION dbo.fnParseArray (@array VARCHAR(1000),@separator CHAR(1))
RETURNS @T Table (occ int,col1 varchar(50))
AS
BEGIN
--DECLARE @T Table (col1 varchar(50))
-- @Array is the array we wish to parse
-- @Separator is the separator charactor such as a comma
DECLARE @separator_position INT -- This is used to locate each separator character
DECLARE @array_value VARCHAR(1000) -- this holds each array value as it is returned
-- For my loop to work I need an extra separator at the end. I always look to the
-- left of the separator character for each array value
declare @occurence int =1 -- to get the Nth occurence
if (left(@array,1)=@separator)
begin
set @array=stuff(@array,1,1,'')
end
if (right(@array,1)<>@separator)
begin
SET @array = @array + @separator
end

-- Loop through the string searching for separtor characters
WHILE PATINDEX('%' + @separator + '%', @array) <> 0
BEGIN
-- patindex matches the a pattern against a string
SELECT @separator_position = PATINDEX('%' + @separator + '%',@array)
SELECT @array_value = LEFT(@array, @separator_position - 1)
-- This is where you process the values passed.
INSERT into @T VALUES (@occurence,@array_value)
set @occurence = @occurence + 1
-- Replace this select statement with your processing
-- @array_value holds the value of this element of the array
-- This replaces what we just processed with and empty string
SELECT @array = STUFF(@array, 1, @separator_position, '')
END
RETURN
END

select * from
dbo.fnParseArray ('/23/34/45/67/89/101/','/')
where occ=1




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-05-26 : 07:44:41
quote:
Originally posted by mrtanvirali

wow, very nice,

thanks very much



You are welcome.

PBUH
Go to Top of Page

mrtanvirali
Starting Member

41 Posts

Posted - 2010-05-26 : 08:11:18
@webfred, very nice attempt its working fine, quite dynamic
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-26 : 08:15:11
welcome
Yes I thought it would be helpful to get Nth occurence out of the string if needed...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-26 : 10:43:30
or


declare @x varchar(max)
select @x= '/23/34/45/67/89/101/'

select substring(@x,2,CHARINDEX('/',@x,2)-2)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -