473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sql charindex split string

Hello

I am quite hopeless and of course a newbe.

The situation: Sql2k / query

I would like it ot break down the following string:

2004 Inventory:Ex.Plant Farm1:1st Cut:Premium:0094

Whereby:
Year = '2004 Inventory'
plant= 'Ex.Plant Farm1'
cut = '1st Cut'
grade = 'Premium'
lot# = '0094'

It is always seperate by ':', but it can be 5 fields or 4 or 3 and so
on

code to create the view:

CREATE VIEW dbo.TEST
AS
SELECT FullName, LEFT(FullName, CHARINDEX(':', FullName + ':') -
1) AS year, CASE WHEN LEN(FullName) - LEN(REPLACE(FullName, ':', ''))
0 THEN LTRIM(SUBSTRING(FullName,

CHARINDEX(':', FullName) + 1, CHARINDEX(':', FullName + ':',
CHARINDEX(':', Fullname) + 1) - CHARINDEX(':',
FullName) - 1)) ELSE NULL END AS Plant, CASE
WHEN LEN(FullName) - LEN(REPLACE(FullName, ':', '')) > 1 THEN
LTRIM(SUBSTRING(FullName,
CHARINDEX(':', FullName + ':', CHARINDEX(':',
FullName) + 1) + 1, CHARINDEX(':', FullName + ':', CHARINDEX(':',
Fullname) + 1) - CHARINDEX(':',
FullName+':') - 1)) ELSE NULL END AS [Cut]
FROM dbo.ItemInventory

Can anyone help me with this? I am stuck half the way and get for cut
the rest of the string: '1st Cut:Premium:0094'

Thanks!
Jul 23 '05 #1
5 42003
Willem (wi********@hotmail.com) writes:
I am quite hopeless and of course a newbe.

The situation: Sql2k / query

I would like it ot break down the following string:

2004 Inventory:Ex.Plant Farm1:1st Cut:Premium:0094

Whereby:
Year = '2004 Inventory'
plant= 'Ex.Plant Farm1'
cut = '1st Cut'
grade = 'Premium'
lot# = '0094'

It is always seperate by ':', but it can be 5 fields or 4 or 3 and so
on


Obviously someone fluked his data modelling. It's never a good idea
to store more than piece of data in one column. So when you need the
pieces it becomes messy.

Here is a query for your task. I opted as a number of nested derived
tables:

CREATE TABLE inventory (fullname varchar(60) NOT NULL)
go
INSERT inventory(fullname)
VALUES ('2004 Inventory:Ex.Plant Farm1:1st Cut:Premium:0094')
INSERT inventory(fullname)
VALUES ('2004 Inventory')
INSERT inventory(fullname)
VALUES ('2004 Inventory:Ex.Plant Farm1:1st Cut')
go
SELECT inventory, plant = nullif(plant, ''), cut = nullif(cut, ''),
grade = nullif(grade, ''), lot# = nullif(lot#, '')
FROM
(SELECT inventory, plant, cut,
left(rest, charindex(':', rest + ':') - 1) AS grade,
substring(rest, charindex(':', rest + ':') + 1,
len(rest)) AS lot#
FROM
(SELECT inventory, plant,
left(rest, charindex(':', rest + ':') - 1) AS cut,
substring(rest, charindex(':', rest + ':') + 1,
len(rest)) AS rest
FROM
(SELECT inventory,
left(rest, charindex(':', rest + ':') - 1) AS plant,
substring(rest, charindex(':', rest + ':') + 1,
len(rest)) AS rest
FROM
(SELECT left(fullname, charindex(':', fullname + ':') - 1) AS inventory,
substring(fullname, charindex(':', fullname + ':') + 1,
len(fullname)) AS rest
FROM inventory) AS a) AS b) AS c) AS d
go
DROP TABLE inventory

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Hi Erland

Really thanks for your answer. Finally found some time to look into
it.

I think I have to explain more about the situation.

The information is...from Quickbooks Enterprise and I use a small tool
http://www.synergration.com/accessbooksrt/default.htm

And at least a tool what pulls the information from quickbooks and put
it into
access or sql. In my case SQL. And synchronizing every 5 minutes.
That works fine so far.

The field ("FullName") I would like to break down or split is from
table "ItemInventory". This field is used in every table
(sales/purchase/item
and so on)

I would like it to setup our clients to use it through a sql-dsn and
create pivot tables in excel (based on the refreshed tables in sql).
Not sure yet, or to create asp pages. So I need actually to prepare
the
information in sql and from there I can use it on the clients.
(reason is...limitation of quickbooks...10 users)

So what is the best way to go? to create a table? but how will it be
updated?
And to be honest I feel that I need more basic knowledge of sql.
Right now I am actually struggeling and looking for the best way to
get the information prepared inside sql.

Could you please give me some direction?

thanks!!

Willem


Erland Sommarskog <es****@sommarskog.se> wrote in message news:<Xn*********************@127.0.0.1>...
Willem (wi********@hotmail.com) writes:
I am quite hopeless and of course a newbe.

The situation: Sql2k / query

I would like it ot break down the following string:

2004 Inventory:Ex.Plant Farm1:1st Cut:Premium:0094

Whereby:
Year = '2004 Inventory'
plant= 'Ex.Plant Farm1'
cut = '1st Cut'
grade = 'Premium'
lot# = '0094'

It is always seperate by ':', but it can be 5 fields or 4 or 3 and so
on


Obviously someone fluked his data modelling. It's never a good idea
to store more than piece of data in one column. So when you need the
pieces it becomes messy.

Here is a query for your task. I opted as a number of nested derived
tables:

CREATE TABLE inventory (fullname varchar(60) NOT NULL)
go
INSERT inventory(fullname)
VALUES ('2004 Inventory:Ex.Plant Farm1:1st Cut:Premium:0094')
INSERT inventory(fullname)
VALUES ('2004 Inventory')
INSERT inventory(fullname)
VALUES ('2004 Inventory:Ex.Plant Farm1:1st Cut')
go
SELECT inventory, plant = nullif(plant, ''), cut = nullif(cut, ''),
grade = nullif(grade, ''), lot# = nullif(lot#, '')
FROM
(SELECT inventory, plant, cut,
left(rest, charindex(':', rest + ':') - 1) AS grade,
substring(rest, charindex(':', rest + ':') + 1,
len(rest)) AS lot#
FROM
(SELECT inventory, plant,
left(rest, charindex(':', rest + ':') - 1) AS cut,
substring(rest, charindex(':', rest + ':') + 1,
len(rest)) AS rest
FROM
(SELECT inventory,
left(rest, charindex(':', rest + ':') - 1) AS plant,
substring(rest, charindex(':', rest + ':') + 1,
len(rest)) AS rest
FROM
(SELECT left(fullname, charindex(':', fullname + ':') - 1) AS inventory,
substring(fullname, charindex(':', fullname + ':') + 1,
len(fullname)) AS rest
FROM inventory) AS a) AS b) AS c) AS d
go
DROP TABLE inventory

Jul 23 '05 #3
Hi Erland

Really thanks for your answer. Finally found some time to look into
it.

I think I have to explain more about the situation.

The information is...from Quickbooks Enterprise and I use a small tool
http://www.synergration.com/accessbooksrt/default.htm

And at least a tool what pulls the information from quickbooks and put
it into
access or sql. In my case SQL. And synchronizing every 5 minutes.
That works fine so far.

The field ("FullName") I would like to break down or split is from
table "ItemInventory". This field is used in every table
(sales/purchase/item
and so on)

I would like it to setup our clients to use it through a sql-dsn and
create pivot tables in excel (based on the refreshed tables in sql).
Not sure yet, or to create asp pages. So I need actually to prepare
the
information in sql and from there I can use it on the clients.
(reason is...limitation of quickbooks...10 users)

So what is the best way to go? to create a table? but how will it be
updated?
And to be honest I feel that I need more basic knowledge of sql.
Right now I am actually struggeling and looking for the best way to
get the information prepared inside sql.

Could you please give me some direction?

thanks!!

Willem


Erland Sommarskog <es****@sommarskog.se> wrote in message news:<Xn*********************@127.0.0.1>...
Willem (wi********@hotmail.com) writes:
I am quite hopeless and of course a newbe.

The situation: Sql2k / query

I would like it ot break down the following string:

2004 Inventory:Ex.Plant Farm1:1st Cut:Premium:0094

Whereby:
Year = '2004 Inventory'
plant= 'Ex.Plant Farm1'
cut = '1st Cut'
grade = 'Premium'
lot# = '0094'

It is always seperate by ':', but it can be 5 fields or 4 or 3 and so
on


Obviously someone fluked his data modelling. It's never a good idea
to store more than piece of data in one column. So when you need the
pieces it becomes messy.

Here is a query for your task. I opted as a number of nested derived
tables:

CREATE TABLE inventory (fullname varchar(60) NOT NULL)
go
INSERT inventory(fullname)
VALUES ('2004 Inventory:Ex.Plant Farm1:1st Cut:Premium:0094')
INSERT inventory(fullname)
VALUES ('2004 Inventory')
INSERT inventory(fullname)
VALUES ('2004 Inventory:Ex.Plant Farm1:1st Cut')
go
SELECT inventory, plant = nullif(plant, ''), cut = nullif(cut, ''),
grade = nullif(grade, ''), lot# = nullif(lot#, '')
FROM
(SELECT inventory, plant, cut,
left(rest, charindex(':', rest + ':') - 1) AS grade,
substring(rest, charindex(':', rest + ':') + 1,
len(rest)) AS lot#
FROM
(SELECT inventory, plant,
left(rest, charindex(':', rest + ':') - 1) AS cut,
substring(rest, charindex(':', rest + ':') + 1,
len(rest)) AS rest
FROM
(SELECT inventory,
left(rest, charindex(':', rest + ':') - 1) AS plant,
substring(rest, charindex(':', rest + ':') + 1,
len(rest)) AS rest
FROM
(SELECT left(fullname, charindex(':', fullname + ':') - 1) AS inventory,
substring(fullname, charindex(':', fullname + ':') + 1,
len(fullname)) AS rest
FROM inventory) AS a) AS b) AS c) AS d
go
DROP TABLE inventory

Jul 23 '05 #4
Willem (wi********@hotmail.com) writes:
The information is...from Quickbooks Enterprise and I use a small tool
http://www.synergration.com/accessbooksrt/default.htm
That site is not availble from my corner of the world right now. Nor
I have ever before heard of Quickbooks. So it's very difficult for me
to give any useful answers.
And at least a tool what pulls the information from quickbooks and put
it into
access or sql. In my case SQL. And synchronizing every 5 minutes.
That works fine so far.


I would suggest that you should perform the split before you get into
the database. As you have experienced, this is not a funny thing to
do in SQL. Since I don't know anything about the source for the data,
nor about your tool, it's difficult to say how would you do it. All
I can say is that myself, I would have done it in Perl. :-)

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5
Below is a UDF to take a comma delim value and return in table format,
the split results.. Use like select * from dbo.Split('1,2,3,3',',')

CREATE FUNCTION dbo.Split(@sText varchar(8000), @sDelim varchar(20) = '
')
RETURNS @retArray TABLE (idx smallint Primary Key, value varchar(8000))
AS
BEGIN
DECLARE @idx int,
@value varchar(8000),
@bcontinue bit,
@iStrike int,
@iDelimlength int

IF @sDelim = 'Space'
BEGIN
SET @sDelim = ' '
END

SET @idx = 0
SET @sText = LTrim(RTrim(@sText))
SET @iDelimlength = DATALENGTH(@sDelim)
SET @bcontinue = 1

if(Len(@sText) = 0)
return

IF NOT ((@iDelimlength = 0) or (@sDelim = 'Empty'))
BEGIN
WHILE @bcontinue = 1
BEGIN

--If you can find the delimiter in the text, retrieve the first element
and
--insert it with its index into the return table.

IF CHARINDEX(@sDelim, @sText)>0
BEGIN
SET @value = SUBSTRING(@sText,1, CHARINDEX(@sDelim,@sText)-1)
BEGIN
INSERT @retArray (idx, value)
VALUES (@idx, @value)
END

--Trim the element and its delimiter from the front of the string.
--Increment the index and loop.
SET @iStrike = DATALENGTH(@value) + @iDelimlength
SET @idx = @idx + 1
SET @sText = LTrim(Right(@sText,DATALENGTH(@sText) - @iStrike))

END
ELSE
BEGIN
--If you canÆt find the delimiter in the text, @sText is the last
value in
--@retArray.
SET @value = @sText
BEGIN
INSERT @retArray (idx, value)
VALUES (@idx, @value)
END
--Exit the WHILE loop.
SET @bcontinue = 0
END
END
END
ELSE
BEGIN
WHILE @bcontinue=1
BEGIN
--If the delimiter is an empty string, check for remaining text
--instead of a delimiter. Insert the first character into the
--retArray table. Trim the character from the front of the string.
--Increment the index and loop.
IF DATALENGTH(@sText)>1
BEGIN
SET @value = SUBSTRING(@sText,1,1)
BEGIN
INSERT @retArray (idx, value)
VALUES (@idx, @value)
END
SET @idx = @idx+1
SET @sText = SUBSTRING(@sText,2,DATALENGTH(@sText)-1)

END
ELSE
BEGIN
--One character remains.
--Insert the character, and exit the WHILE loop.
INSERT @retArray (idx, value)
VALUES (@idx, @sText)
SET @bcontinue = 0
END
END

END

RETURN
END

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: M Wells | last post by:
Hi All, Just wondering if it's any more efficient to use the following SQL statement to return a partial match from a column as opposed to the second statement. SELECT * FROM Table1 WHERE...
2
by: Little PussyCat | last post by:
Hello, I need to be able to replace only the first occurance of a space character in a column. Reason being is the data in the column I am trying to replace seems to have umpteen space...
3
by: csomberg | last post by:
SQL Server 2000 Ya know, it is always the simplest stuff that gets ya !! I am having the hardest time getting a simple piece of code working. Must be brain dead today. Goal: Get the users...
0
by: prav78 | last post by:
hi there i have a field name(fil_srt_cond) with the values of cmpnt_name,ASC,1,2 cmpnt_stuff,DESC,2,3 i used the char index:- ...
1
by: chudson007 | last post by:
I have a field containing a string with '/' in it multiple times. How can I return the charindex of the last occurance of '/' in the string? Regards, Ciarán
1
by: db55 | last post by:
This script doesn't work. Why? UPDATE SET = LTRIM(SUBSTRING(, 1, convert(bigint, CHARINDEX(',', Comments)-1))) WHERE NOT( IS NULL) AND LEN() > 8 Basically, I'm trying to...
2
by: matthewwhaley | last post by:
What is the best way to essentially use the charindex(find) function if the value is could be more than one variable (A or B or C) I can't seem to get an "or", "if" or "select if" to work ...
7
by: iritchie | last post by:
Hello all, I am trying to write a script which breaks down a single address field into individual fields, with char(10) or a carriage return as the delimiter. "empltable" is the table I am...
1
by: jeremy | last post by:
Hello, I was hoping someone could help me with this SQL statement. The column 'options.optionDescrip' is a varchar field. Some values of the 'options.optionDescrip' contain commas, some do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.