im getting a strange error when running it
Major Error 0x80040E14, Minor Error 25501
Main Topics
Browse All TopicsHi I have a field that values consist of an id like either xxx-yyy-zzz-ff, or sometimes just xxx-yy-zz.
There typically is only 3 or four parts to an id, and always separated by a hyphen however the is not always 3 characters in each section. I am looking to split this into the 3 or 4 sections using a substring or something. Any ideas? I played around with charindex a little but got strange results when using it inside a function.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
yes ,here is the complete message
Major Error 0x80040E14, Minor Error 25501
> CREATE function [dbo].[ParseString]
(
@String varchar(8000)
)
returns @tbl table ( s varchar(1000))
as
begin
declare @i int, @j int
select @i = 1
while @i <= len(@String)
begin
select @j = charindex('-', @String, @i)
if @j = 0
begin
select @j = len(@String) + 1
end
insert @tbl select substring(@String, @i, @j - @i)
select @i = @j + 1
end
return
end
There was an error parsing the query. [ Token line number = 1,Token line offset = 8,Token in error = function ]
I did try it without changes, I believe the first time I may have hit backspace which threw the first error. I coped again and am running as is, but getting hte new error of:
Server: Msg 444, Level 16, State 2, Procedure ParseItem, Line 10
Select statements included withjin a function cannot return data to a client
sample data would look like the following. I literally am only interested in each piece so if I could even do something like the following I would
select substring(secttion 1 from left to first dash),
substring(second - to next -)
and so one I would be ecstatic.
my_id
xxx-ttt-yys-dd-ccc
xx-rrt-das-ff
ff-sss-rrr-rrr
ss-sss-sss-ss
ss-sss-sss-ddd
thanks for your help in advance...
just to clarify i would expect output to be : (using first id as example) xxx-ttt-yys-dd-ccc
xxx as my first returned col, then tttm then yys etc...
sample data would look like the following. I literally am only interested in each piece so if I could even do something like the following I would
select substring(secttion 1 from left to first dash),
substring(second - to next -)
and so one I would be ecstatic.
my_id
xxx-ttt-yys-dd-ccc
xx-rrt-das-ff
ff-sss-rrr-rrr
ss-sss-sss-ss
ss-sss-sss-ddd
Use this:
create function dbo.split(@s varchar(500)) returns table as /* Note we're not declaring the table here, so it's an inline function */
return
(
with
N0 as (select 1 as n union all select 1)
,N1 as (select 1 as n from N0 t1, N0 t2)
,N2 as (select 1 as n from N1 t1, N1 t2)
,N3 as (select 1 as n from N2 t1, N2 t2)
,N4 as (select 1 as n from N3 t1, N3 t2)
,nums as (select row_number() over (order by (select 1)) as num from N4)
select [1],[2],[3],[4]
from
(
select substring(@s, num, charindex('-',@s+'-',num)-
from nums
where substring('-' + @s,num,1) = '-'
and num <= len(@s)
) t
pivot
(max(s) for colnum in ([1],[2],[3],[4])) p
)
;
And then you have a few options for calling it:
declare @str varchar(500);
set @str = '123-456-7890-abc';
select *
from dbo.split(@str);
or for rows in a table, use CROSS APPLY.
create table dbo.testvals (somestring varchar(500));
insert testvals (somestring) select '123-456-7890-abc';
insert testvals (somestring) select '234-567-890-abcd';
select *
from testvals t
cross apply
dbo.split(t.somestring) s
;
Just for something different, and maybe there might be NULLs (maybe not) have a look at the following...
It uses some "magic numbers" which are contained in the system table spt_values as described in the Article :
http://www.experts-exchang
Can you try this?
select my_id,
parsename(col1,4) as col1,
parsename(col1,3) as col2,
parsename(col1,2) as col3,
parsename(col1,1) as col4,
col5
from (
select my_id,
case when (LEN(my_id) - LEN(REPLACE(my_id, '-', ''))) > 3
then reverse(substring(reverse(
else null end as col5,
replace(case when (LEN(my_id) - LEN(REPLACE(my_id, '-', ''))) > 3
then reverse(substring(reverse(
else my_id end,'-','.') as col1
from YourTable) as t1
Business Accounts
Answer for Membership
by: aneeshattingalPosted on 2009-11-04 at 10:31:27ID: 25742302
CREATE function [dbo].[ParseString]
yy-zzz-ff' )
(
@String varchar(8000)
)
returns @tbl table ( s varchar(1000))
as
begin
declare @i int, @j int
select @i = 1
while @i <= len(@String)
begin
select @j = charindex('-', @String, @i)
if @j = 0
begin
select @j = len(@String) + 1
end
insert @tbl select substring(@String, @i, @j - @i)
select @i = @j + 1
end
return
end
GO
SELECT * from [dbo].[ParseString]('xxx-y