473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ 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 1679
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_datetim e
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ------------------------------- -----------------------
SplitX dbo table function 2007-10-10 22:49:31.123



Column_name Type Computed Length Prec Scale Nullable TrimTrailingBla nks FixedLenNullInS ource Collation
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ----------------------------------- ----------- ----- ----- ----------------------------------- ----------------------------------- ----------------------------------- --------------------------------------------------------------------------------------------------------------------------------
ID int no 4 10 0 yes (n/a) (n/a) NULL
Items nvarchar no 8000 yes (n/a) (n/a) SQL_Latin1_Gene ral_CP1253_CI_A I


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.fie ld) 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
16607
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
9729
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 control. It outputs all text fields surrounded by quotes, and all numeric fields w/o quotes. All fields are separated with commas. This has been...
1
3659
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 trying to avoid this extra step. can access read in this file directly into a table as append data? thank you very much for any pointers
1
6674
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 processing. I am having no luck at all finding different routines written in C to read delimited files of any kind. I have a few ideas of how I might go...
5
6489
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 name of 1/4" Pipe gets truncated to 1/4 with csv Can I create a tab delimited file with Aceess and include the quote mark.
2
2557
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 tried everything to get the currency fields with the format I want after exporting but!! NO LUCK!!! This is what I have tried. In the source table...
3
9648
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... field1...field2...field3...
3
7101
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 export it into a text file. Is there any s/w out there I could use? I am not much of a programmer but I heard I could use VBA to get this done. Any help...
3
2837
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 to parse the fields with comma but also strip off the "" encapsulating some of the fields with php? The problem is the "" encapsulation is...
5
2314
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 be: 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA
0
7432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7456
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6022
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5076
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.