Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sub string 1

Status
Not open for further replies.

angela4eva

Programmer
Apr 29, 2005
46
US
I am using sql server 7
i have afield called input in the table getinfo,
the "input field has data like 112e|wee|rer3
112e|wee|rer3
wede|weee|null
null|null|null
NULL
null|wesa|dssew

I want a help with writing a query
all the first parts second parts and thrid parts
for eg:first part should return
112e
112e
wede
null
null
i hope you understand what i mean i should be able to do this for all the three parts...
as you can see"|" is the delimiter

any help appreciated
any help apprecviated
 
here you go:

create this UDF in your database:
Code:
CREATE FUNCTION dbo.Split
(
	@RowData nvarchar(2000),
	@SplitOn nvarchar(6)
)  
RETURNS @RtnValue table 
(
	Id int identity(1,1),
	Data nvarchar(100)
) 
AS  
BEGIN 
	Declare @Cnt int
	Set @Cnt = 1

	While (Charindex(@SplitOn,@RowData)>0)
	Begin
		Insert Into @RtnValue (data)
		Select 
			Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

		Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
		Set @Cnt = @Cnt + 1
	End
	
	Insert Into @RtnValue (data)
	Select Data = ltrim(rtrim(@RowData))

	Return
END

then you call this function as follows:

SELECT Data FROM dbo.split(yourfield,'|')

-DNG

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top