473,480 Members | 4,841 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Delimited Fields

m6s
55 New Member
I have a problem that I had once solved with C#.
I have a field with multiple values like, a|b|c|d|e
a,b,c etc are values from some web form. They get apart from the code with guide the | symbol.

Can I make this functionality in SQL? Should I use procedure, or the CLR for SQL2005 ?

Has anyone seen anything like that?

Thank you in advance...
Oct 10 '07 #1
6 1677
iburyak
1,017 Recognized Expert Top Contributor
If you willing to use function created by me, you can pass this parameter to a function and it will return you a table each value in a separate row the same way you create collection in C++.

1. Create function on the server:

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION Split(@String nvarchar(4000), @Delimiter char(1))      
  2. RETURNS @Results TABLE (ID int, Items nvarchar(4000))      
  3. AS      
  4.  
  5. BEGIN      
  6.     DECLARE @INDEX INT      
  7.     DECLARE @SLICE nvarchar(4000)      
  8.     DECLARE @ID int  
  9.  
  10.     SELECT @INDEX = 1, @ID = 1      
  11.     WHILE @INDEX !=0      
  12.  
  13.  
  14.         BEGIN       
  15.          -- GET THE INDEX OF THE FIRST OCCURENCE OF THE SPLIT CHARACTER      
  16.          SELECT @INDEX = CHARINDEX(@Delimiter,@STRING)      
  17.          -- NOW PUSH EVERYTHING TO THE LEFT OF IT INTO THE SLICE VARIABLE      
  18.          IF @INDEX !=0      
  19.           SELECT @SLICE = LEFT(@STRING,@INDEX - 1)      
  20.          ELSE      
  21.           SELECT @SLICE = @STRING      
  22.          -- PUT THE ITEM INTO THE RESULTS SET      
  23.          INSERT INTO @Results(ID, Items) VALUES(@ID, @SLICE)     
  24.          SELECT @ID = @ID + 1   
  25.          -- CHOP THE ITEM REMOVED OFF THE MAIN STRING      
  26.          SELECT @STRING = RIGHT(@STRING,LEN(@STRING) - @INDEX)      
  27.          -- BREAK OUT IF WE ARE DONE      
  28.          IF LEN(@STRING) = 0 BREAK      
  29.         END      
  30.     RETURN      
  31. END      
  32.  
  33.  
  34.  
  35.  
2. execute following statement:

select * from Split('a|b|c|d|e', '|')

Good Luck.
Oct 10 '07 #2
m6s
55 New Member
On one hand, it worked the function. For that thaaank you!
On the other hand, it seems I have some problem with who has credentials...

I have a login as 'm6s'
a user as 'm6s'
the schema is under dbo for ALL the tables in the database and other objects.

It seems that even if I do use the syntax [dbo].split, or [m6s].split the function is not recognised.

Also, this happens to even tempdb!!!!

Similar problem seems to be happen with assemblies. I found a regex tutorial and while built it and pluged it, it can not be found again. I say AUTHORIZATION dbo, but the properties are blank for who is the owner of this assembly too.

Conclusion in sql express i have 'm6s' as login and user in the logins and users, is something more to do so to see the function? or the assembly?

Bottom line, thank you very much for your snippet.
Oct 10 '07 #3
iburyak
1,017 Recognized Expert Top Contributor
You probably have some default Schema.

Try executing:

Expand|Select|Wrap|Line Numbers
  1. sp_help split 
good luck.
Oct 10 '07 #4
m6s
55 New Member
Name Owner Type Created_datetime
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ------------------------------- -----------------------
SplitX dbo table function 2007-10-10 22:49:31.123



Column_name Type Computed Length Prec Scale Nullable TrimTrailingBlanks FixedLenNullInSource Collation
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ----------------------------------- ----------- ----- ----- ----------------------------------- ----------------------------------- ----------------------------------- --------------------------------------------------------------------------------------------------------------------------------
ID int no 4 10 0 yes (n/a) (n/a) NULL
Items nvarchar no 8000 yes (n/a) (n/a) SQL_Latin1_General_CP1253_CI_AI


Identity Seed Increment Not For Replication
-------------------------------------------------------------------------------------------------------------------------------- --------------------------------------- --------------------------------------- -------------------
No identity column defined. NULL NULL NULL


RowGuidCol
--------------------------------------------------------------------------------------------------------------------------------
No rowguidcol column defined.


Parameter_name Type Length Prec Scale Param_order Collation
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ------ ----------- ----------- ----------- --------------------------------------------------------------------------------------------------------------------------------
@STRING nvarchar 8000 4000 NULL 1 Greek_CS_AS
@Delimiter char 1 1 NULL 2 Greek_CS_AS
Oct 10 '07 #5
iburyak
1,017 Recognized Expert Top Contributor
In your schemas do you have functions folder?

I am not sure how to help you here thou. Ask your DBA.

Irina.
Oct 10 '07 #6
m6s
55 New Member
The fun part is that when I run select split(...) all is cool
if I do select split(table.field) FROM table
I get the error...

Anyway, Irena, thank you very much for your code.
It is most helpful.
Dimitris
Oct 10 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
16602
by: shank | last post by:
What's the easiest way to generate CSV or a comma delimited file from an ASP recordset? I've seen a few searching the internet and they appear to be overkill or out of date. thanks
1
9714
by: DCM Fan | last post by:
Access 2K, SP3 on Windows 2K, SP4 All, I have an import spec set up with quoted Identifiers and comma-separated values. The text file is produced by a 3rd-party program of which I have no...
1
3643
by: sparks | last post by:
I have never done this and wanted to ask people who have what is the best way. One person said import it to excel, then import it into access table. but since this will be done a lot, I am...
1
6658
by: John B. Lorenz | last post by:
I'm attempting to write an input routine that reads from a comma delimited file. I need to read in one record at a time, assign each field to a field array and then continue with my normal...
5
6483
by: Karl Irvin | last post by:
I'm using the Write # statement to create a csv export file from Access 2K Some of the data has embedded quotes in it and it doesn't import into QuickBooks correctly. An inventory part with a...
2
2549
by: GAVO-UK | last post by:
Using Ms Access 2003 I am trying to export a query to a delimited txt file containing some CURRENCY fields, which I want to export without the currently symbol and with five decimals. I have...
3
9641
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
3
7072
by: Avi | last post by:
I need to create a text file that has the data from the 10 tables in the database. The number of fields in the tables exceeds 255 and so I cannot make a new table with all the fields and then...
3
2830
by: Bertie Brink | last post by:
I am trying to read comma delimited rows of text. The problem is that some fields may be encapsulated in "" - particularly the text fields but not numeric fields. Is there a simple efficient way...
5
2299
by: RyanL | last post by:
I'm a newbie! I have a non-delimited data file that I'd like to convert to delimited. Example... Line in non-delimited file: 0139725635999992000010100534+42050-102800FM-15+1198KAIA Should...
0
7040
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6905
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4478
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.