 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 7th, 2007, 03:47 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Get something like SUBSTRING in SQL?
Hi!
I have in my table one column with data like
Is there any chance to get an array (or substrings) of these digits in SQL? In .NET would not be a problem, but i need to do it in SQL.
Best regards,
Aleksandar
e-ducan Global Store
Programmer/Application developer
http://www.e-ducan.com
Belgrade, Serbia
__________________
Aleksandar Dragosavac
Belgrade, Serbia
|
|

May 7th, 2007, 03:57 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
You can write a split function, there are many examples online. You can't have an array in SQL Server but it can return a table with a row for each data item.
--
Joe ( Microsoft MVP - XML)
|
|

May 7th, 2007, 04:30 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
VerbatimBOT,
Did you search here in this forum before posting it here? Take a look at split function equivalent
cheers
_________________________
- Vijay G
Strive for Perfection
|
|

May 7th, 2007, 04:51 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
To be completley honest, I didn't have time to search the forum.
I was in a hurry. Thank you very much for helping me.
All the best!
e-ducan Global Store
Programmer/Application developer
http://www.e-ducan.com
Belgrade, Serbia
|
|

May 8th, 2007, 02:36 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Yes, I used one simular function. If I new for DATALENGTH function, I would write my own. :)
But, it's great when you find out something new.
Cheers!
e-ducan Global Store
Programmer/Application developer
http://www.e-ducan.com
Belgrade, Serbia
|
|

May 9th, 2007, 04:53 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You could do it in PHP, Mysql, Pgsql, or Perl if you have access to those languages, but MSSql does not support the split function.
|
|

May 10th, 2007, 02:15 AM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
I know, but there are other things that could replace the SPLIT function...
e-ducan Global Store
Programmer/Application developer
http://www.e-ducan.com
Belgrade, Serbia
|
|

May 11th, 2007, 01:15 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
the only way I can think of to do it with sql is with a cursor or function that has a cursor in it and itterate through field by field. If you come up with a solution please post it here.
|
|

May 11th, 2007, 05:06 PM
|
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Why would you need a cursor?
I have only one field with digits separated with a # character.
joefawcett was right, there are many examples online.
I used the following function
Code:
CREATE FUNCTION dbo.fnParseList
(
@Delimiter CHAR,
@Text VARCHAR(8000)
)
RETURNS @Result TABLE (RowID SMALLINT IDENTITY(1, 1) PRIMARY KEY, Data VARCHAR(8000))
AS
BEGIN
DECLARE @NextPos SMALLINT,
@LastPos SMALLINT
SELECT @NextPos = 0
WHILE @NextPos <= DATALENGTH(@Text)
BEGIN
SELECT @LastPos = @NextPos,
@NextPos = CASE
WHEN CHARINDEX(@Delimiter, @Text, @LastPos + 1) = 0 THEN DATALENGTH(@Text) + 1
ELSE CHARINDEX(@Delimiter, @Text, @LastPos + 1)
END
INSERT @Result
(
Data
)
SELECT SUBSTRING(@Text, @LastPos + 1, @NextPos - @LastPos - 1)
END
RETURN
END
Programmer/Application developer
Belgrade, Serbia
|
|
 |