Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 parsing out values from a string

Author  Topic 

etietje
Starting Member

24 Posts

Posted - 2003-06-05 : 14:52:34
Hello everybody.

I have a table full of UPC definitions with the oz weight appended somewhere at the end of the description. I need to parse out only the number from the description. Here is a few examples of a description:
'LIEB BLK PITTED OLIVE 19.75 OZEB' -- I would need the 19.75
'+GRGNT YLW FSONN N/AV 80OZ' -- I would need the 80
'HI GINGR PWDR 2.25 OZ GNGR PWDR ' -- I would need the 2.25
'+SARL1 SHPDR BRD N/AV N/AV 24OZ' -- I would need the 24

As you can see, I can't look for only numeric characters, and the numbers are not always on the far right.

Does anybody have any suggestions on the best way to parse out only the numbers to the immediate left of the OZ?

Thank you all in advance.




X002548
Not Just a Number

15586 Posts

Posted - 2003-06-05 : 15:15:11
How about:



USE Northwind
GO

CREATE TABLE bkTable(col1 varchar(255))
GO

INSERT INTO bkTable(col1)
SELECT 'LIEB BLK PITTED OLIVE 19.75 OZEB' UNION ALL -- I would need the 19.75
SELECT '+GRGNT YLW FSONN N/AV 80OZ' UNION ALL -- I would need the 80
SELECT 'HI GINGR PWDR 2.25 OZ GNGR PWDR ' UNION ALL -- I would need the 2.25
SELECT '+SARL1 SHPDR BRD N/AV N/AV 24OZ' -- I would need the 24
GO


CREATE FUNCTION udf_LAST_SPACE
(@str varchar(8000))
RETURNS int
AS
BEGIN
DECLARE @Words INT, @Pos INT, @x Int, @y Int
SELECT @Words = 0, @Pos = 1, @x = -1

WHILE (@x <> 0)
BEGIN
SET @y = @x
SET @x = CHARINDEX(' ', @str, @Pos)
SET @Pos = @x + 1
SET @Words = @Words + 1
END

RETURN @y
END
GO

SELECT CONVERT(varchar(15),RIGHT(SUBSTRING(col1,1,CHARINDEX('OZ',col1)-1),
LEN(RTRIM(SUBSTRING(col1,1,CHARINDEX('OZ',col1)-1)))
- dbo.udf_LAST_SPACE(RTRIM(SUBSTRING(col1,1,CHARINDEX('OZ',col1)-1)))
))
, SUBSTRING(col1,1,CHARINDEX('OZ',col1)-1)
FROM bkTable
GO

DROP FUNCTION udf_LAST_SPACE
GO





Brett

8-)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-06-05 : 15:48:36
I don't need to state the obvious though that if "OZ" appears more than once, you'll have a problem..

And to take it one step further..


CREATE FUNCTION udf_OZ
(@str varchar(8000))
RETURNS varchar(15)
AS
BEGIN
DECLARE @xxx as varchar(15)
SELECT @xxx = CONVERT(varchar(15),RIGHT(SUBSTRING(@str,1,CHARINDEX('OZ',@str)-1),
LEN(RTRIM(SUBSTRING(@str,1,CHARINDEX('OZ',@str)-1)))
- dbo.udf_LAST_SPACE(RTRIM(SUBSTRING(@str,1,CHARINDEX('OZ',@str)-1)))
))

Return @xxx
END
GO

SELECT dbo.udf_OZ(col1) FROM bkTable

DROP FUNCTION dbo.udf_OZ
GO




Brett

8-)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-06-06 : 09:00:54
Did that help...it was a fun exercise...but you should normalize your data, and store it in a separate column

TGIF



Brett

8-)
Go to Top of Page

etietje
Starting Member

24 Posts

Posted - 2003-06-06 : 09:46:57
Brett,

I can't thank you enough. I ask for suggestions and you give me almost exactly what I need.

All I need to do now is modify this enough to account for cases where the description has a space before the OZ, and when their is no space between the description and the OZ size. Other than those two rare cases, this thing is golden.

Thank you again, I really appreciate it.

Go to Top of Page
   

- Advertisement -